[Tex/LaTex] Can’t make fancyhdr work with pandoc + yaml header

fancyhdrpandoc

I am trying to convert my document from Markdown to PDF using Pandoc. At the beginning of my Markdown file, I have included an YAML header. Every parameters seems to be processed, except header-includes.

The subject has already been addressed here : Adding headers and footers using Pandoc

I can't make it work the way it is explained on this post (or some other posts I have seen on internet, though, or even after reading Pandoc's documentation ).
I don't see what I did wrong.

Setup:

  • Mac OS 10.11
  • Texlive 2015
  • Pandoc 1.15

Beginning of markdown file:

---
title: Anonymise agents datas on MySQL for EQualOne
author: Adrien Desprez
abstract: This script is able to anonymise agents datas stored in MySQL portal.
date: July 14th 2016
geometry: margin=2cm
numbersections: yes
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhead[CO,CE]{This is fancy}
- \fancyfoot[CO,CE]{So is this}
- \fancyfoot[LE,RO]{\thepage}
---

# Prerequisites

* Linux distribution; tested on CentOS 6.7 and Fedora 23
* Python 2.6 or 2.7
* Python modules: yaml, MySQLdb

On the default latex template I can see that header-includes parameter should be processed:

 adrien@datmachine ☻ pandoc -D latex|grep -C 2 "header-includes"
 $endif$
 \date{$date$}
 $for(header-includes)$
 $header-includes$
 $endfor$

Then I run the following pandoc command:

pandoc -f markdown -t latex --standalone  --listings -H ~/Documents/listings-setup.tex  -o ~/Documents/readme.pdf ~/git/pro/exploit/scripts/offline/data/anonymise_agents_datas/README.md

The PDF does not contains any header or footer.
If I run in verbose mode and inspect the TeX file produced, there is no mention of usepackage{fancyhdr} on ~/Documents/readme.tex:

pandoc --verbose -f markdown -t latex --standalone  --listings -H ~/Documents/listings-setup.tex  -o ~/Documents/readme.tex ~/git/pro/exploit/scripts/offline/data/anonymise_agents_datas/README.md > output.log

The output.log file does not include any useful information for that matter.

What surprise me, is that pandoc process correctly every parameters I give him on the YAML header but the one called header-includes, despite the fact that this parameter is present on the latex default template.

What I am missing ?

Best Answer

I finally need to create a latex custom template. I have other customization needs that can be filled easily with the a custom template.

So, in order to create a latex custom template:

pandoc -D latex > default-template.tex # to store the default template
cp default-template.tex custom-template.tex # to create your own template

Then, edit custom-template.tex file.

To add headers with a condition, you can do this:

diff --git a/default-template.tex b/custom-template.tex
index 33277c0..629c7a2 100644
--- a/default-template.tex
+++ b/custom-template.tex
@@ -175,9 +175,13 @@ $if(author)$
\author{$for(author)$$author$$sep$ \and $endfor$}
$endif$
\date{$date$}
-$for(header-includes)$
-$header-includes$
-$endfor$
+$if(header-includes)$
+\usepackage{fancyhdr}
+\pagestyle{fancy}
+\fancyhead{}
+\fancyhead[RO,RE]{Your header content here}
+\fancyfoot[LO,LE]{Your footer content here}
+$endif$

$if(subparagraph)$
$else$

On the header of your Markdown file:

---
header-includes: yes
---

# Some title

Some content.

Then, generate your PDF using your latex custom template:

pandoc -f markdown -t latex --standalone --template custom-template.tex  -o outputfile.pdf inputfile.md

You can go further and report the content of the headers using the latex parameters and YAML meta-data.

$if(header-includes)$
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead{}
\fancyhead[$head-options$]{$head-content$}
\fancyfoot[$foot-options$]{$foot-content$}
$endif$

In your Markdown file:

---
header-includes: yes
head-options: RO,RE
foot-options: LO,LE
head-content: Your header content here
foot-content: Your footer content here
---

# Some title

Some content.

I have not tested this last possibility, though. May be you have to protect the string passed from the Markdown's YAML header to LaTeX. To be tested.

Related Question