# pandoc

## An itemised list of the esoteric difficulties involved in bullet points

pandoc (centre) amongst the markup formats, markdown (bottom right) MS office (bottom left), HTML (top left), latex (top right) etc.

The default parser IMO for markdown and a swiss army knife of conversion for other text markup formats.

Various useful things are built on pandoc, including blogdown, quarto etc.

## installing pandoc

I install pandoc via homebrew. If you are using RStudio, you already have an installation inside RStudio. You can access that installation by letting your shell know about the path to it. On macOS this looks like

export PATH=\$PATH:/Applications/RStudio.app/Contents/MacOS/pandoc

conda, the python package manager, will obediently install it also. The default version was ancient last time I checked, though. Consider using the conda forge version.

conda install -c conda-forge pandoc

You can also install it by, e.g. a linux package manager but this is not recommended as it tends to be an even more elderly version and the improvements in recent pandoc versions are great. You could also compile it from source, but this is laborious because it is written in Haskell, a semi-obscure language with hefty installation requirements of its own. There are probably other options, but I don’t know them.

## pandoc tricks

John MacFarlane’s pandoc tricks are the canonical tricks, as John MacFarlane is the boss of pandoc.

You want fancy mathematical macros, or a latex preamble? Something more elaborate still?

Modify a template to include a custom preamble, e.g. for custom document type. Here’s how you change that globally:

pandoc -D latex > ~/.pandoc/templates/default.latex

Or locally:

pandoc -D latex > template.latex
pandoc --template=template.latex …

If you only want some basic macros a document type alteration is probably overkill. Simply prepend a header file

pandoc -H _macros.tex chapter_splitting.md -o chapter_splitting.pdf

NB Pandoc will expand basic LaTeX Macros in even HTML all by itself.

There are many other pandoc template tricks.

## Cross references and citations

As discussed also in my citation guide, I use pandoc-citeproc. See also the relevant bit of the pandoc manual.

Cross references are supported by pandoc-crossref or some combination of pandoc-fignos, pandoc-eqnos etc.

You invoke that with the following flags (order important):

pandoc -F pandoc-crossref -F pandoc-citeproc file.md -o file.html

The resulting syntax in markdown is

$x^2$ {#eq:label}

for labels and, for references,

@fig:label
@eq:label
@tbl:label

or

[@fig:label1;@fig:label2;…]
[@eq:label1;@eq:label2;…]
[@tbl:label1;@tbl:label2;…]

etc.

Annoyingly, RMarkdown, while still using pandoc AFAICT, does this slightly differently,

See equation \@ref(eq:linear)

$$a + bx = c (\#eq:linear)$$

Citations can either be rendered by pandoc itself or passed through to some BibTeX nightmare if you feel that the modern tendency to regard diacritics and other non-English typography as an insidious plot by malevolent agencies.

Citekeys per default look like BibTeX, and indeed BibTeX citations seem to pass through.

\cite{heyns_foo_2014,heyns_bar_2015}

They are rendered in the output by an in-built pandoc filter, which is installed separately:

The preferred pandoc-citeproc format seems to be something with an @ sign and/or occasional square brackets

Blah blah [see @heyns_foo_2014, pp. 33-35; also @heyns_bar_2015, ch. 1].
But @heyns_baz_2016 says different things again.

This is how you output it.

# Using the CSL transform

pandoc -F pandoc-citeproc --csl=apa.csl --bibliography=bibliography.bib \
-o document.pdf document.md
# or using biblatex and the traditionalist workflow.

pandoc --biblatex --bibliography=bibliography.bib \
-o document.tex document.md
latexmk document

If you want your reference section numbered, you need some magic:

## References

::: {#refs}
:::

aside: CSL is close to being good for use on websites, but has a flaw: They do not support links, in the sense that there is no general way in the standard to tell a CSL renderer where to put links. There is a hack that may support your use case, although it is not ideal for mine. This is not same as saying links are impossible; it rather means that if you want something different you need to write your own CSL processor with with some idiosyncratic URL handling built in, which presupposes that you have access to the source code of whatever tool you use and would like to spend time maintaining a fork of it. Fundamentally, the creators of this tool imagine that we are only using it for writing stuff to be printed out on paper.

## Tables

Too many types. I usually find the pipe tables easiest since they don’t need me to align text. They look like this

| Right | Left | Default | Center |
|------:|:-----|---------|:------:|
|   12  |  12  |    12   |    12  |
|  123  |  123 |   123   |   123  |
|    1  |    1 |     1   |     1  |

## Figures, algorithms, etc

panflute-filters is a bunch of useful filters stuff:

pandoc-figures
figures with captions and backmatter support
pandoc-tables
tables with captions, backmatter support, csv support
pandoc-algorithms
support for tex algorithm packages
pandoc-tex
replace arbitrary tex templates

The scripting API includes Haskell, and an embedded lua interpreter, SDKs for other languages, and a free massage voucher probably. The intermediate representation can be serialised to JSON so you can use any language that handles JSON, if you are especially passionate about some other langua e.g. python, or any text data processing trick.

## Conversion tricks

### presentations

This is invoked for you magically by various scientific workbooks which support presentation backends.

### reStructuredText to Markdown

Pandoc’s reStructuredText reader exists but is not great. One option is to go via HTML, e.g.

rst2html.py --math-output=MathJax document.rst | pandoc -f html -t
markdown -

This will mangle the mathematical equations.

pandoc -f rst -t markdown document.rst