With pandoc 1.12.x and it’s new YAML metadata capabilities you could add all the information and all LaTeX-code you need in your markdown document like this:
---
title: Test
author: Author Name
header-includes: |
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[CO,CE]{This is fancy}
\fancyfoot[CO,CE]{So is this}
\fancyfoot[LE,RO]{\thepage}
abstract: This is a pandoc test . . .
...
# This is a test
Lorem ipsum....
That way you don't have to modify the template, simply calling will suffice.pandoc doc.md -o doc.pdf

If you want more control, you can add new metadata like this:
---
title: Test
author: Author Name
header: This is fancy
footer: So is this
geometry: margin=1in
abstract: This is a pandoc test . . .
...
# This is a test
Lorem ipsum....
To make this work, you have to modify the template () accordingly:pandoc -D latex > template.latex
% filename: template.latex
\documentclass[$if(fontsize)$$fontsize$,$endif$$if(lang)$$lang$,$endif$$if(papersize)$$papersize$,$endif$$for(classoption)$$classoption$$sep$,$endfor$]{$documentclass$}
\usepackage[T1]{fontenc}
% -----------------------
% Using abstracts
\usepackage{abstract}
% -----------------------
% Using fancy headers and footers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[CO,CE]{$header$}
\fancyfoot[CO,CE]{$footer$}
\fancyfoot[LE,RO]{\thepage}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[utf8]{inputenc}
$if(euro)$
\usepackage{eurosym}
$endif$
\else % if luatex or xelatex
\ifxetex
\usepackage{mathspec}
\usepackage{xltxtra,xunicode}
\else
\usepackage{fontspec}
\fi
\defaultfontfeatures{Mapping=tex-text,Scale=MatchLowercase}
\newcommand{\euro}{€}
$if(mainfont)$
\setmainfont{$mainfont$}
$endif$
$if(sansfont)$
\setsansfont{$sansfont$}
$endif$
$if(monofont)$
\setmonofont[Mapping=tex-ansi]{$monofont$}
$endif$
$if(mathfont)$
\setmathfont(Digits,Latin,Greek){$mathfont$}
$endif$
\fi
% use microtype if available
\IfFileExists{microtype.sty}{\usepackage{microtype}}{}
$if(geometry)$
\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}
$endif$
$if(natbib)$
\usepackage{natbib}
\bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
$endif$
$if(biblatex)$
\usepackage{biblatex}
$if(biblio-files)$
\bibliography{$biblio-files$}
$endif$
$endif$
$if(listings)$
\usepackage{listings}
$endif$
$if(lhs)$
\lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
$endif$
$if(highlighting-macros)$
$highlighting-macros$
$endif$
$if(verbatim-in-note)$
\usepackage{fancyvrb}
$endif$
$if(tables)$
\usepackage{longtable}
$endif$
$if(graphics)$
\usepackage{graphicx}
% Redefine \includegraphics so that, unless explicit options are
% given, the image width will not exceed the width of the page.
% Images get their normal width if they fit onto the page, but
% are scaled down if they would overflow the margins.
\makeatletter
\def\ScaleIfNeeded{%
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother
\let\Oldincludegraphics\includegraphics
{%
\catcode`\@=11\relax%
\gdef\includegraphics{\@ifnextchar[{\Oldincludegraphics}{\Oldincludegraphics[width=\ScaleIfNeeded]}}%
}%
$endif$
\ifxetex
\usepackage[setpagesize=false, % page size defined by xetex
unicode=false, % unicode breaks when used with xetex
xetex]{hyperref}
\else
\usepackage[unicode=true]{hyperref}
\fi
\hypersetup{breaklinks=true,
bookmarks=true,
pdfauthor={$author-meta$},
pdftitle={$title-meta$},
colorlinks=true,
citecolor=$if(citecolor)$$citecolor$$else$blue$endif$,
urlcolor=$if(urlcolor)$$urlcolor$$else$blue$endif$,
linkcolor=$if(linkcolor)$$linkcolor$$else$magenta$endif$,
pdfborder={0 0 0}}
\urlstyle{same} % don't use monospace font for urls
$if(links-as-notes)$
% Make links footnotes instead of hotlinks:
\renewcommand{\href}[2]{#2\footnote{\url{#1}}}
$endif$
$if(strikeout)$
\usepackage[normalem]{ulem}
% avoid problems with \sout in headers with hyperref:
\pdfstringdefDisableCommands{\renewcommand{\sout}{}}
$endif$
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em} % prevent overfull lines
$if(numbersections)$
\setcounter{secnumdepth}{5}
$else$
\setcounter{secnumdepth}{0}
$endif$
$if(verbatim-in-note)$
\VerbatimFootnotes % allows verbatim text in footnotes
$endif$
$if(lang)$
\ifxetex
\usepackage{polyglossia}
\setmainlanguage{$mainlang$}
\else
\usepackage[$lang$]{babel}
\fi
$endif$
$for(header-includes)$
$header-includes$
$endfor$
$if(title)$
\title{$title$}
$endif$
$if(subtitle)$
\subtitle{$subtitle$}
$endif$
\author{$for(author)$$author$$sep$ \and $endfor$}
\date{$date$}
\begin{document}
$if(title)$
\maketitle
$endif$
$for(include-before)$
$include-before$
$endfor$
$if(toc)$
{
\hypersetup{linkcolor=black}
\setcounter{tocdepth}{$toc-depth$}
\tableofcontents
}
$endif$
%-----------------------------------
% Adding the abstract
$if(abstract)$
\begin{abstract}
$abstract$
\end{abstract}
$endif$
$body$
$if(natbib)$
$if(biblio-files)$
$if(biblio-title)$
$if(book-class)$
\renewcommand\bibname{$biblio-title$}
$else$
\renewcommand\refname{$biblio-title$}
$endif$
$endif$
\bibliography{$biblio-files$}
$endif$
$endif$
$if(biblatex)$
\printbibliography$if(biblio-title)$[title=$biblio-title$]$endif$
$endif$
$for(include-after)$
$include-after$
$endfor$
\end{document}
This is also the place to create a title page. As you can see, the template is nothing but a LaTeX document with some , so it should be easy to customize to your liking.$variables$
Now just call pandoc with the template option:
pandoc -s -N --template=template.latex doc.md -o doc.pdf
You can find the documentation for templates here: https://pandoc.org/MANUAL.html#templates
YAML header
---
mainfont: Font-Regular.otf
mainfontoptions:
- BoldFont=Font-Bold.otf
- ItalicFont=Font-Italic.otf
- BoldItalicFont=Font-BoldItalic.otf
---
command line
$ pandoc in.md --pdf-engine=xelatex \
-V 'mainfont:Font-Regular.otf' \
-V 'mainfontoptions:BoldFont=Font-Bold.otf, ItalicFont=Font-Italic.otf, BoldItalicFont=Font-BoldItalic.otf'
-o out.pdf
Since my answer to the original question, a lot has changed. So here is an update on how to set fonts with pandoc. There are several ways to change the font in and they are quite well documented nowadays. Here is a brief overview:pandoc
You can specify any font on your system that can be used with fontspec
In the YAML header of your markdown file, set the variables like this for XeLaTeX:
---
mainfont: DejaVuSerif.ttf
sansfont: DejaVuSans.ttf
monofont: DejaVuSansMono.ttf
mathfont: texgyredejavu-math.otf
---
or, for LuaLaTex:
---
mainfont: DejaVuSerif
sansfont: DejaVuSans
monofont: DejaVuSansMono
mathfont: TeXGyreDejaVuMath-Regular
---
If you need more fine grained control, you can specify options:
---
mainfont: DejaVuSerif
mainfontoptions:
- Extension=.ttf
- UprightFont=*
- BoldFont=*-Bold
- ItalicFont=*-Italic
- BoldItalicFont=*-BoldItalic
---
Call pandoc with set to --pdf-engine or xelatex:lualatex
$ pandoc in.md --pdf-engine=xelatex -o out.pdf
You can also set the fonts on the command line:
$ pandoc in.md --pdf-engine=xelatex \
-V 'mainfont:DejaVuSerif.ttf' \
-V 'sansfont:DejaVuSans.ttf' \
-V 'monofont:DejaVuSansMono.ttf' \
-V 'mathfont:texgyredejavu-math.otf' \
-o out.pdf
or
$ pandoc in.md --pdf-engine=lualatex \
-V 'mainfont:DejaVuSerif' \
-V 'sansfont:DejaVuSans' \
-V 'monofont:DejaVuSansMono' \
-V 'mathfont:TeXGyreDejaVuMath-Regular' \
-o out.pdf
And again, with options:
$ pandoc in.md --pdf-engine=xelatex \
-V 'mainfont:DejaVuSerif' \
-V 'mainfontoptions:Extension=.ttf, UprightFont=*, BoldFont=*-Bold, ItalicFont=*-Italic, BoldItalicFont=*-BoldItalic' \
-V 'sansfont:DejaVuSans.ttf' \
-V 'monofont:DejaVuSansMono.ttf' \
-V 'mathfont:texgyredejavu-math.otf' \
-o out.pdf
Note, that wants the filename, while xelatex is content with the fontfamily name. You can use otfinfo to get more information on names and features of the font you are going to use.lualatex

Some packages like or dejavu-otf do the fontspec setup for you, but you have to modify the template in order for them to work.libertinus-otf
Get the default template and save it to some file:
$ pandoc -D latex > template.latex
Open the file and remove all the font configuration code and replace it with, e.g.:
\usepackage{dejavu-otf}
fontfamily for pdflatexIf you are using neither nor lualatex but xelatex, you can use:pdflatex
---
fontfamily: dejavu
---
or
$ pandoc in.md --pdf-engine=pdflatex \
-V 'fontfamily:dejavu' \
-o demo.pdf
---
mainfont: DejaVuSerif
sansfont: DejaVuSans
monofont: DejaVuSansMono
mathfont: TeXGyreDejaVuMath
---
or
$ pandoc in.md --pdf-engine=context \
-V 'mainfont:DejaVuSerif' \
-V 'sansfont:DejaVuSans' \
-V 'monofont:DejaVuSansMono' \
-V 'mathfont:TeXGyreDejaVuMath' \
-o out.odf
Pandoc does not support vertical lines in tables, see https://github.com/jgm/pandoc/issues/922 for some discussion.
A workaround is to modify the implementation of from the Pandoc preamble to always print vertical lines. For this the column specification longtable needs to be changed into @{}lll@{} through Pandoc.@{}|l|l|l|@{}
This can be done with the following steps:
@{} from the start of the specification using \StrGobbleLeft from the xstring package. Note that this counts as two characters, not three.@{} from the end using \StrGobbleRight.l with |l using \StrSubstitute.| at the end and surround by @{} left and right again.The code that handles the column specification in is a call to longtable (short for make preamble) with argument \@mkpream. You can use the functions from the #2 package to change the argument, store it in a new macro, and then call xstring with this new macro, as follows:\@mkpream
\StrGobbleLeft{#2}{2}[\pream]
\StrGobbleRight{\pream}{2}[\pream]
\StrSubstitute{\pream}{l}{|l}[\pream]
\@mkpream{@{}\pream|@{}}
This code can be added to the macro longtable using \LT@array from the \patchcmd package. This gives you vertical lines for the full table. Note that this does not work if you use the etoolbox package (then some more modifications are needed). By extension it also doesn't work if you use packages that load array internally, such as array.colortbl
The substitution now works only on left-aligned columns, you can add similar substitutions for center and right aligned columns.
Pandoc uses the package with booktabs, \toprule and \midrule instead of \endrule. Booktabs introduces a bit of extra vertical space, so when using vertical lines in the table they leave a gap, see Reducing the gap around the frames of longtable. That question also provides a solution, which is to set \hline and \aboverulesep to 0. This should be added to the Pandoc preamble.\belowrulesep
Then a horizontal line can be added after every row by redefining (the \LT@tabularcr macro for the end of a table row) to include a longtable. For this to work also \hline should be redefined to do nothing, as that line is now already printed by \midrule. You can also consider redefining \LT@tabularcr and \toprule, but if you don't change those you get a thick line effect from the combination of \bottomrule/\toprule and \bottomrule.\hline
If you want the vertical margins of the cells to be a bit larger you can add for example .\renewcommand{\arraystretch}{1.3}
Note that these kinds of modifications disable the normal use of and longtable, so they should not be used when writing a normal LaTeX document. However, for Pandoc it might be ok, because the LaTeX code produced by Pandoc has a restricted predefined format where the effect of modifications is more predictable.booktabs
MWE
---
author:
- Matthew Petty
affiliation: None
date: \today
title: Title of Document
subtitle: Subtitle of Document
header-includes:
- '\newcommand{\projectNumberCode}{CODE }'
- '\newcommand{\projectName}{Project Name }'
- '\newcommand{\coreSystemName}{Core Name }'
- '\newcommand{\bt}[1]{\fcolorbox{gray}{lightgray}{#1}}'
- '\defaultfontfeatures{Extension = .otf}'
- '\usepackage{fontawesome}'
- '\usepackage{tocloft}'
- '\usepackage{graphicx}'
- '\usepackage{hyperref}'
- '\usepackage{float}'
- '\usepackage{glossaries}'
- '\setglossarystyle{altlistgroup}'
- '\usepackage{xparse}'
- '\usepackage{lscape}'
- '\makenoidxglossaries'
- '\usepackage{etoolbox}'
- '\usepackage{xstring}'
- '\setlength{\aboverulesep}{0pt}'
- '\setlength{\belowrulesep}{0pt}'
- '\renewcommand{\arraystretch}{1.3}'
- '\makeatletter'
- '\patchcmd{\LT@array}{\@mkpream{#2}}{\StrGobbleLeft{#2}{2}[\pream]\StrGobbleRight{\pream}{2}[\pream]\StrSubstitute{\pream}{l}{|l}[\pream]\@mkpream{@{}\pream|@{}}}{}{}'
- '\def\midrule{}'
- '\apptocmd{\LT@tabularcr}{\hline}{}{}'
- '\makeatother'
documentclass: article
fontsize: 10pt
secnumdepth: 4
classoptions:
- a4paper
- portrait
geometry:
- top=2cm
- left=1cm
- right=1cm
- bottom=2cm
linkcolor: Blue
numbersections: true
---
\pagebreak
\tableofcontents
\setcounter{table}{0}
\listoftables
\pagebreak
# Introduction
## Purpose
This document is testing how to format tables in Markdown/Latex
Table: Test Table
| Heading 1 | Heading 2 | Heading 3 |
| :---------------------------------------------------------------------------------------------------------------------- | :------------------------- | :------------------------------------------------------------------------------------------------------------------------------------ |
| This is Contents 1. This is Contents 1. This is Contents 1. This is Contents 1. This is Contents 1. This is Contents 1. | This is Contents 2 | This is Contents 3. This is Contents 3. This is Contents 3. This is Contents 3. This is Contents 3. This is Contents 3. |
| This is Contents 1 | This is Contents 2 | This is Contents 3 |
| This is Contents 1 | This is Contents 2 | This is Contents 3 |
| This is Contents 1 | This is Contents 2 | This is Contents 3 |
| This is Contents 1 | This is Contents 2 | This is Contents 3 |
| This is Contents 1 | This is Contents 2 | This is Contents 3 |
\begin{center}END OF DOCUMENT\end{center}
\hrulefill
Result:

Update
Use the piped version suggested by user Christian Long:
pandoc -t latex mydoc.md | pandoc -f latex --data-dir=docs/rendering/ -o mydoc.docx
I know this is late in coming, but I'll be assuming people are still searching for solutions to this three years after the original question -- I know I was.
My solution was to use LaTeX as an intermediary between markdown and docx (actually, I was converting from org-mode, but same difference). So in your case, I believe a one-liner solution would be:
pandoc -f markdown -t latex -o mydoc.tex mydoc.md && \
pandoc -f latex -t docx --data-dir=docs/rendering/ -o mydoc.docx mydoc.tex
Which might get you closer to your goal. Of course, Pandoc has about hundred arguments it can handle, and there are probably ways to make this prettier. It has also gotten quite a few updates since you first posted your question.
I was able to generate the bibliography by adding --citeproc:
pandoc --bibliography=test.bib --citeproc -o test.pdf test.md
Almost everything set in the YAML metadata has only an effect through the pandoc template in use.
Pandoc templates may contain variables. For example in your HTML template, you could write:
<title>$title$</title>
These template variables can be set with the option. --variable KEY[=VAL]
However, they are also set from the document metadata, which in turn can be set either by using:
--metadata KEY[=VAL] option,--metadata-file option.The options inserts strings verbatim into the template, while --variable escapes strings. Strings in YAML metadata (also when using --metadata) are interpreted as markdown, which you can circumvent by using pandoc markdown's generic raw attributes. For example for HTML output:--metadata-file
`<script>alert()</script>`{=html}
See this table for a schematic:
| | --variable | --metadata | YAML metadata and --metadata-file |
|------------------------|-------------------|-------------------|-----------------------------------|
| values can be… | strings and bools | strings and bools | also YAML objects and lists |
| strings are… | inserted verbatim | escaped | interpreted as markdown |
| accessible by filters: | no | yes | yes |
To answer your question: the template determines what fields in the YAML metadata block have an effect. To view, for example, the default latex template, use:
$ pandoc -D latex
To see some variables that are set automatically by pandoc, see the Manual. Finally, other behaviours of pandoc (such as markdown extensions, etc) can only be set as command-line options (except when using a wrapper script).