A Pandoc template for converting markdown letters to LaTeX PDFs

2019-10-10

On my current trend of loving LaTeX, I have been writing letters which I needed to print and send in the post in LaTeX. But, this seemed like overkill, especially since letters are almost always plain text and lots of aspects are reused such as the address header. Markdown is what I use to write plain text notes, but LaTeX formatted PDFs look pretty, so I came up with a template for converting Markdown formatted letters into PDFs, via pandoc, with LaTeX. The template lives in ~/.pandoc/templates/letter.tex and can be used like this:

pandoc letter.md --template=letter.tex -o letter.pdf

The template is below. It’s also my very first foray into templating LaTeX with pandoc, so it might seem quite simplistic:

\documentclass{letter}

% Define page geometry
\usepackage[margin=2.5cm]{geometry}

% Set font
\usepackage[T1]{fontenc}

% Image handling
\usepackage{graphicx}

% Push Closing to left margin
\longindentation=0pt

\signature{\includegraphics[width=0.2\textwidth]{/Users/user/docs/short_sig.png} \\ $if(author)$$author$$else$John L. Godlee$endif$} % Your name for the signature at the bottom

$if(address)$
\address{$for(address)$$address$$sep$\\$endfor$}
$else$
\address{This is \\ an address \\ Liverpool \\ LV12 P46 \\ johngodlee@gmail.com} 
$endif$

$if(date)$
\date{$date$}
$endif$

\begin{document}

\begin{letter}{} % Name/title of the addressee

$if(opening)$
\opening{\textbf{$opening$}}
$else$
\opening{\textbf{Dear Sir or Madam,}}
$endif$

$body$

\vspace{2\parskip} % Extra whitespace for aesthetics
$if(closing)$
\closing{$closing$}
$else$
\closing{Yours sincerely,}
$endif$
\vspace{2\parskip} % Extra whitespace for aesthetics

\end{letter}

\end{document}

When writing the Markdown document, YAML header material adds details like the address and the sign-off:

---
author: John Godlee
closing: Yours faithfully,
opening: Dear all,
date: 36th Octobry 1849
address:
- 157 Market Road
- Sofa City
- United Kingdom
...

This is the body of my letter it will say some stuff that hopefully will be formatted in multiple gorgeous paragraphs. I have a pen a laptop a sticky note an apple a computer screen a phone a handkerchief a desk a mouse a keyboard.

$if(address)$ is an if statement that tests whether the YAML variable address exists. If it does, each element of address is placed inside a LaTeX \address{} command, separated by \\ to trigger a new line, using $for(address)$ and $endfor$ as the Pandoc equivalent of a for loop. The other $if(..)$ statements work in the same way.