Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandoc: use variables in custom latex preamble

Tags:

format

pandoc

I have the file test.md which contains:

---
footertext: some text for the footer
headertext: this is in the header
---

here is the text body.

And the file format.tex which contains:

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[L]{$headertext$}
\fancyfoot[L]{$footertext$}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\setlength{\headsep}{0.25in}

I run the command:

pandoc -H format.tex test.md -o test.pdf

You can see what I want to do. I am trying to get the text "this is in the header" to show up in the header, but it does not, it only shows the string "headertext" (same problem for footer).

What am I doing wrong?

Edit: OK, I think I understand. Apparently variables are only available in templates, not in included begin or end code blocks (like I am using), or in the md itself. So new question: Why is this? It is unintuitive, inconvenient, and poorly documented.

like image 956
Christopher Brown Avatar asked Sep 02 '25 04:09

Christopher Brown


1 Answers

You can easily modify a pandoc template. Access the default template with

pandoc -D latex > new_template.latex

Paste the content of your format.tex in the preamble. You should use $if$ to check if the variable exists before using it if you want to use this template for more than one document :

\usepackage{fancyhdr}
\pagestyle{fancy}
$if(headertext)$\fancyhead[L]{$headertext$}$endif$
$if(footertext)$\fancyfoot[L]{$footertext$}$endif$
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\setlength{\headsep}{0.25in}

Then compile with :

pandoc test.md -o test.pdf --template=new_template.latex
like image 96
scoa Avatar answered Sep 05 '25 00:09

scoa