Generate document with table of contents containing page numbers using Markdown or something similar

markdownmultimarkdown

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.

Best Answer

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.