LaTeX Nirvana

February 5, 2010

programming

I’m no fan of Word or OpenOffice for that matter, all that WYSIWYG stuff is not for me. For years now I’m using LaTeX for my editing needs. An added bonus for using LaTeX is that the output is stunning.

But for really nice looking output I wanted the following:

  • Fonts in images/graphics should match the font of the main document
  • TrueType fonts, instead of the standard TeX fonts. To avoid the “Heeh, that looks like a LaTeX document”-reactions;
  • UTF-8 support and international character support;
  • Easy integration of images (SVG based);
  • Automatic building.

I’m using the following tools for this:

  • Inkscape and pdfcrop for graphics generation;
  • make for building;
  • VI for editing (with syntax highlighting);
  • evince as PDF viewer;
  • xelatex for building the pdf.

General setup

As said earlier, I’m a fan of the memoir class as a replacement of the standard LaTeX classes. So I’m using it here again. When you are using xelatex (better UTF-8 support) you should also include the package xltxtra and xunicode.

Fonts

The package for TrueType font support is fontspec, to keep the math fonts working I had to include the package as \usepackage[cm-default]{fontspec}.

Note: fontspec mandates xelatex.

To use special characters I also found the pifont package. In my current working document I use the Liberation Fonts and Courier New:

\defaultfontfeatures{Scale=MatchLowercase}
\setmainfont[SmallCapsFont={* Caps}]{Liberation Serif}
\setsansfont{Liberation Sans}
\setmonofont[SmallCapsFont={Courier New}]{Courier New}
\fontsize{10}{10}

LaTeX will make TrueType fonts slightly larger than its core fonts, the Scale=MatchLowercase tries to remedy that.

Graphics

I’m using Inkspace to draw my graphics, an added bonus for using TrueType fonts in my LaTeX documents is that I can use the same fonts in the Inkspace drawings. So any text I add will be of the font Liberation Serif at 10 points.

Further more Inkscape has the nice feature that it can be used on the command line

$ inkscape --export-pdf=drawing.pdf drawing.svg

generates the pdf file. To make it fit in the document we need to crop the resulting pdf

$ pdfcrop drawing.pdf output.pdf && mv output.pdf drawing.pdf

And now we are left with a nice, cropped, pdf. See this link on how to include graphics in your LaTeX document, I just use:

\begin{figure}[h!b]
\includegraphics{drawing.pdf}
\caption{Fake caption line.}
\end{figure

Which will probably be extended somewhat to allow for cross references (search for \label and \ref).

Building

To help in the sometimes tedious building of LaTeX documents, I use make. For instance creating the pdf graphic files from the svg source files is done with the following Makefile:

pdfs := $(patsubst %.svg,%.pdf,$(wildcard *.svg))

%.pdf: %.svg
    inkscape --export-pdf=$@ $<
    pdfcrop $@ output.pdf && mv output.pdf $@

all: ${pdfs}

clean:  
    rm -f *.pdf

The only thing I need to do is to create a file with Inkscape, save it and run make.

For the document itself I’m using a similar, small Makefile.

.PHONY: fig

all:    fig go.pdf

go.pdf: go.tex go-setup.tex chapter*.tex ex*.tex src/*.go 
    xelatex go.tex && xelatex go.tex

fig:    fig/*.svg
    ( cd fig; make all )

clean:
    rm -f go.lol go.aux go.log map.log go.pdf
    ( cd fig; make clean )

Putting it all together

Next to the above Makefiles I have the following in the preamble:

\documentclass[a4paper,openany]{memoir}
\usepackage[cm-default]{fontspec}% provides font selecting commands
\usepackage{xunicode}% provides unicode character macros
\usepackage{xltxtra} % provides some fixes/extras
\usepackage[answerdelayed,lastexercise]{exercise}
\usepackage{pifont}
\usepackage{caption}
\usepackage{alltt}
\usepackage{url}
\usepackage{listings}
\usepackage{color}
\usepackage{graphicx}

\defaultfontfeatures{Scale=MatchLowercase}
\setmainfont[SmallCapsFont={* Caps}]{Liberation Serif}
\setsansfont{Liberation Sans}
\setmonofont[SmallCapsFont={Courier New}]{Courier New}
\fontsize{10}{10}
\graphicspath{{fig/}} % set default import pat

% among other \newcommands the follow environment for
% indented alltt (verbatim) text
\newenvironment{display}{%
  \def\FrameCommand{\hskip\parindent}%
  \MakeFramed {\advance\hsize-\width \FrameRestore}%
  \small
  \begin{alltt}
  }%
{\end{alltt}\endMakeFramed}

My master text go.tex used \input to include all other pieces of text, so there I have:

\chapter{Intro}
\input{chapter-intro.tex}

This keeps the main document file nice and clean and you can easily re-arrange entire chapters. After any significant change I just run make and view the resulting pdf file with evince, which is an awesome pdf viewer btw. It looks something like this:

Screenie of sample page

The very-much-work-in-progress pdf can be seen here.

LaTeX