Pandoc Tricks

kleinbottle edited this page Jan 13, 2017 · 13 revisions

Here's some tricks that is allowed by pandoc but not obvious at first sight.

From Markdown, To Markdown

using pandoc -f markdown... -t markdown... can have surprisingly useful applications. As a demo, this file is generated by

pandoc -t markdown_github --atx --normalize --reference-location=block --toc -s -o

Be careful of @ though, you need to escape it in pandoc since it is treated as citation in pandoc.


As shown in issue #2814, it can be used to clean up / normalize your markdown file.

TOC generation

e.g. you have a long markdown file in GitHub and want to have a TOC, you can use pandoc -t markdown_github --toc -o

This a useful workaround to update the TOC of very long documents, but —beware!— if you use this trick for writing over the input file, you'll end stacking TOCs — each new Table of Contents being generated above the previouly built ones, and indexing them too. This technique is useful when working with different source and output files.

Also, you can add a title to the TOC using the toc-title variable, but only if you use a markdown template — as explained ahead.

Using Markdown Templates

Did you know that you can use pandoc template with markdown too?

Ask pandoc to write-out the default template for markdown:

pandoc --print-default-template=markdown > template.markdwon

And now let's peek at the template we got:







As you can see, there's plenty of conditional statements to play with, allowing for additional control over the output markdown file.

You can also use the toc-title template variable to tell pandoc to add a title on top of the generated TOC. Change the template’s toc block like this:

# $toc-title$



And now invoke pandoc like this:

pandoc --toc -V toc-title:"Table of Contents" --template=template.markdown -o

And you'll see in the file an auto-generated Table of Contents with a # Table of Contents title over it.

NOTE: if you also include some extra markdown contents with the --include-before-body option (eg: the contents of the included file will go before the TOC (at least, with the template used in this example) and any headings it contais will not be included in the TOC — ie: the TOC only indexes what comes after the $toc$ template tag. This is useful if you'd like to include an Abstract before the TOC.

Math in Pure Markdown

The manual said:

Note: the --webtex option will affect Markdown output as well as HTML.

This can be used to put math in pure markdown. e.g. you want to put math directly in the in GitHub.

For example, in the

# Important Discovery!


Try it!

Run this:

pandoc --atx-headers --webtex= -s -o

Then the output becomes:

Important Discovery!


Try it!

Convert Between the 4 Table Syntaxes in Pandoc

Say, in your source markdown file

| testing     | pandoc            | tables  |
| simple cell | no multiline cell | and     |
| so          | on                | no list |

In command line,

pandoc -t markdown-simple_tables-multiline_tables-pipe_tables -s -o

In the output

| testing                  | pandoc                   | tables                   |
| simple cell              | no multiline cell        | and                      |
| so                       | on                       | no list                  |

Repeated Footnotes Anchors and Headers Across Multiple Files

If you use auto-identifiers for the headers, and there are different headers of the same name across different folders, you'd want to catenate them together, and pandoc do this for you:

pandoc ...

But if there are repeated footnotes anchors on both files, you need --file-scope, which will parsed each files individually (so the footnotes anchors are "local" to the individual file):

pandoc --file-scope ...

What if the 2 files has both problems? i.e. headers of same name (hence the same id by the auto-identifier) and footnotes of the same anchors appears across the files. Either approach gives you problem.

In this case, you can use "to markdown from markdown" to write an intermediate markdown file using --file-scope, which handles the colliding footnote anchor for you, and then generate from that intermediate markdown file, the auto-identifiers will handle the headers for you:

pandoc --file-scope -o
pandoc ...

Template Snippet

If you wrote a template snippet that do not form a complete template. The -H, -B, or -A option would not help because pandoc would put your snippet as is and wouldn't process it as a template. i.e. The snippet is included after the template is processed.

A trick mentioned by @cagix in jgm/pandoc-templates#220 is this:

pandoc --template=template_snippet -o processed_snippet
pandoc -H processed_snippet -o document.pdf

The first line will process your template snippet according to the properties of the document, but since your snippet (probably) do not have $body$, the body would not be in the output. Now the snippet is processed and can then be included through -H as is in the 2nd line.

Left-aligning Tables in LaTeX

Based on this pandoc-discuss exchange and this TeX StackExchange topic, it is possible to left-align all tables in a document (in the PDF output from LaTeX) with this single invocation in the YAML header block of the markdown document:

  - \usepackage[margins=raggedright]{floatrow}

This applies to all floats, and fine-grained control may be achieved with the options outlined in the documentation for the floatrow LaTeX package.