# Intro

Once in a while I write a letter, and for some time I’ve been doing so with LaTeX, and more recently using the scrlttr2 class from the KOMA-Script project.

In this article, we’ll look at a few complicated ways to make this process simpler.

One of the tricks will be to use pandoc. This will allow us to use lightweight markup languages, and write to a custom scrlttr2 pandoc template.

The other two tricks we’ll use, are from the KOMA-Script manual, regarding letter class options, and an address file.

# Pandoc template

For reference, there is the pandoc manual.

Let’s make a pandoc template file called scrlttr2.latex, with nothing but the text $body$ in it.

To see how this works, try the following.

echo "hello world" | pandoc --template=scrlttr2 -t latex

You can see what the default pandoc template looks like via pandoc -D latex.

Considering what my original scrlttr2 .tex file contained, I put together the following template. You can put this in your current working directory, but better might be to move it to ~/.pandoc/templates/.

% scrlttr2.latex
\documentclass[letterpaper,UScommercial9,parskip=half,foldmarks=p]{scrlttr2}

\setkomavar{fromname}{Bartholemew JoJo Simpson}
Flat Land
}
\begin{document}
\begin{letter}{$for(address)$$address$$sep$\\
$endfor$
}

\opening{$opening$}

$body$

\closing{Sincerely,}

\end{letter}
\end{document}

This allows us to compose with for example markdown,

---
- Big Bird
- 123 Sesame St.
opening: Dear Big Bird,
---
<!-- test.md -->

We are writing to let you know your shipment has arrived. In it you
will find NOTHING!

Have a nice day!

Now, let’s create a Makefile for easier use,

# Makefile
pdf:
pandoc test.md --template=scrlttr2 -o test.pdf

tex:
pandoc test.md --template=scrlttr2 -o test.tex

Now in the shell, simply executing make should make the pdf.

If you’re using emacs, you might use the following binding to call make. Calling with a prefix, C-u C-c c, you can modify the command. If you need to change completely, default-directory, etc, use M-x compile.

;; ~/.emacs
(global-set-key (kbd "C-c c") 'recompile)

Let’s now move on to the tricks from the scrlttr2 section of the KOMA manual.

# Letter Class Options

While these files can go in current working directory, some may be more appropriately saved to ~/Library/texmf/tex/latex/. (I discovered this location while reading Creating letters with KOMA-Script scrlttr2 and Org-mode.)

Consider the following improvement over our template file, by removing this block,

\documentclass[letterpaper,UScommercial9,parskip=half,foldmarks=p]{scrlttr2}

\setkomavar{fromname}{Bartholemew JoJo Simpson}
Flat Land
}

and replacing with the following,

\documentclass[MyLetter]{scrlttr2}

For this to work, we create the following file,

% MyLatex.lco
\LetterOptionNeedsPapersize{MyLetter}{letter}

\KOMAoptions{parskip=half}
\KOMAoptions{foldmarks=p}

\setkomavar{fromname}{Bartholemew JoJo Simpson}
Flat Land
}

If you felt that was an improvement, you may feel the following is as well.

With an address file, we can use aliases to refer to complete addresses.

We next consider this improvement, in our markdown file. We will replace the following yaml metadata

address:
- Big Bird
- 123 Sesame St.

with the simpler

adr: BIRD

To make this work, we modify our pandoc template and add an adr file.

In our pandoc template, we replace the following,

\begin{letter}{$for(address)$$address$$sep$\\
$endfor$
}

\input{contacts.adr}
\begin{letter}{\$adr$}

% contacts.adr
\adrentry{Bird}{Big}{123 Sesame St.}{}{}{}{}{BIRD}

That’s it!

(A benefit of this terse address specification, you can use #+ADR: BIRD in org-mode files. Then you have benefits of org-mode such as commenting sections out has a nicer syntax than in markdown. If I recall correctly, pandoc does not currently work with multiline #+ADDRESS: specifications.)

# In closing

In closing, I mention a few improvements I might consider going forward, as well as a few links I’d like to look for ideas out of.

One improvement I’d like to make, is improving the template conditionals in the pandoc template. For example, template should allow for either of address or adr variable to be set.

Also considering if some refactoring or lifting of variables from the tex or lco files, up to the pandoc variables, would be convenient for some cases.

A few links I’d like to read more at, as a potential source of ideas.