I need to create a large document that when printed will be a couple hundred pages. I’d like to do this with something like Markdown. One of my requirements is that the document must have page numbers and a table of contents. Is there a way using Markdown or a similar Markup language to automatically generate a table of contents with associated page numbers? All the stuff I’ve sceen with Markdown so far can automatically generate a table of contents for HTML output but there are no associated page numbers.

  • 1
    Probably because HTML has no concept of page numbers. What is a page? – Bob Jun 2 '13 at 1:50
  • 2
    markdown doesn't have to be converted into HTML. – Jared Jun 2 '13 at 15:57
  • It sounds like what you're looking for is something like PDF, as opposed to something that ends up rendered in HTML/XHTML. – killermist Jun 2 '13 at 21:28
  • Note: to actually show a table of contents for html (without page numbers, naturally), you need both the --toc option AND the -s (standalone) option. I didn't find this documented anywhere.... – nealmcb Sep 7 '17 at 13:52

Sounds like a job for pandoc.

HTML has no concept of 'pages'; it would probably be possible to kludge something together with <div></div> tags, but I'd just use PDF:

pandoc --toc --chapters -o output.pdf input1.mkd input2.mkd

Pandoc can take an arbitrary number of inputs; it adds a newline to the end of each input and concatenates them. The above command will create a PDF designed to be bound as a book - each chapter (signified by a level 1 title) will start on an odd-numbered page. If you don't want this behaviour, use the following:

pandoc --toc -V documentclass=report -o output.pdf input1.mkd input2.mkd

If you want to save paper and don't mind about chapters just starting wherever the previous one ends (they will even start in the middle of a page), call pandoc without the --chapters option:

pandoc --toc -o output.pdf input1.mkd input2.mkd

You can generate a HTML with a table of contents, but with all the limitations that you listed. It is also possible to generate ODT and Microsoft DOC documents with pandoc, but I think they don't look very good, especially compared to the beautiful PDF generation.

Creating PDFs with pandoc requires a LaTeX engine to be installed; this is pretty trivial on Linux, where you'll have at least one in the repositories, but on Windows it might be a little harder (I wouldn't know, I've never actually tried). As such, it is possible to customise things to your liking with a custom LaTeX template - but I haven't got my head around that yet, so I currently stick with the (pretty nice) defaults.

  • Looks good and this is probably the solution I will go with. – Jared Jun 7 '13 at 13:35

After some google minutes I noticed that you need your own LaTeX-Template which can passed as an option to pandoc:

pandoc --toc --chapter --template=yourtemplate.tex -o output.pdf input1.mkd

However your template needs some modification on the LaTeX site. As a suggestion you can use:

As a starting point you can use the following template. I copied the default template https://github.com/jgm/pandoc-templates/blob/master/default.latex and added some extra stuff.

I am sure that you can remove most of the if-end-combinations. The template needs it to have more options for users.


\usepackage{fixltx2e} % provides \textsubscript
% use upquote if available, for straight quotes in verbatim environments
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\else % if luatex or xelatex
% use microtype if available
% Redefine \includegraphics so that, unless explicit options are
% given, the image width will not exceed the width of the page.
% Images get their normal width if they fit onto the page, but
% are scaled down if they would overflow the margins.
  \usepackage[setpagesize=false, % page size defined by xetex
              unicode=false, % unicode breaks when used with xetex
            pdfborder={0 0 0}}
\urlstyle{same}  % don't use monospace font for urls
% Make links footnotes instead of hotlinks:
% avoid problems with \sout in headers with hyperref:
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\VerbatimFootnotes % allows verbatim text in footnotes

\author{$for(author)$$author$$sep$ \and $endfor$}








Have you considered using LaTeX?

The other answers suggest using Pandoc, which looks like it can convert Markdown to LaTeX, but it might be easier to start right in LaTeX.

LaTeX is designed for the creation of print documents, and has things like page numbers and TOC generation built-in. Check out this basic tutorial: http://www.andy-roberts.net/writing/latex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.