[Tex/LaTex] How to adapt a standard LaTeX template for Pandoc (in a Docker container, not using RStudio/knitr )

markdownpandoctemplates

I want to use Pandoc to convert markdown text files to beautifully formatted PDFs using LaTeX templates. I want to be able to do this reproducibly across unknown future machines, so I am using the official Pandoc Docker image to do it through a Docker container, as recommended here. I have successfully got it working with the Eisvogel Pandoc Latex template

— but I am perplexed by how to use a standard LaTeX template, such as those freely available at http://www.latextemplates.com/ (specifically let's say this one) or offered by academic journals (such as this one), given my particular configuration with Markdown, Pandoc, and Docker (which can be reproduced precisely from its git repository).

Specifically I think I have two questions:

  1. The 'standard templates' mentioned above don't exactly seem to me to be templates — they include their example text with their LaTeX markup. How do I extract whatever necessary to map this on to the Markdown + Pandoc LaTeX template paradigm?

  2. They use multiple .tex files and/or accompanying .cls files. How do I get these into the right place for a containerized Pandoc process to find?

This question addresses a similar issue, but is specifically interested in using RStudio.

Best Answer

1.

A pandoc latex template is just a regular latex file with some placeholder variables in it. You can print the default template with pandoc -D latex > default.latex.

This means, that you can use any existing latex file and just add those variables to make a template.

Let's take this minimal latex document mwe.tex:

\documentclass{article}
\begin{document}
Here is some text.
\end{document}

Now you can replace Here is some text with $body$ and execute echo "Here is some text" | pandoc --template mew.tex -s --to latex and you will see, that$body$ is replaced with the text.

That way, you can make a pandoc template out of any latex file. Just replace the texts and commands in the latex file with pandoc variables. Of course you need to load all the packages that are used by pandoc! You can consult the default template to see, what pandoc usually requires.

How the templating engine works in detail and which variables are available is documented in the manual: https://pandoc.org/MANUAL.html#templates

2.

Where to store the files? Pandoc finds files in the current directory and in the data-directory. pandoc -v will print the data-dir on your system. If your data-dir is $HOME/.local/share/pandoc, then the templates will be found under: $HOME/.local/share/pandoc/templates