About swk

I am a software developr, data scientist, computational linguist, teacher of computer science and above all a huge fan of LaTeX. I use LaTeX for everything, including things you never wanted to do with LaTeX. My latest love is lilypond, aka LaTeX for music. I'll post at irregular intervals about cool stuff, stupid hacks and annoying settings I want to remember for the future.

Writing accents and quotes that look like accents and quotes in LaTeX

I had a very strange problem. In my slides for teaching LaTeX I wanted to tell people that they have to write ``test'' to get “test” (in correct quotes). But if you write ` in LaTeX, you get… a single quote sign! So I tried verbatim, \verb|`| and \texttt{`} – which all gives something that looks like a LaTeX quote sign.

After quite some frustration I found the following:

\`{} gives ` (grave accent)
\'{} curiously gives ´ (acute accent)

The straight quote is the hardest, after reading about many creative ways at stackexchange, I chose the easiest: Include package textcomp. Then

\textquotesingle gives ' (single straight quote)

What a mess!

Single landscape page in a LaTeX document

You can get a complete LaTeX document to be in landscape format by adding the ‘landscape’ parameter to the document class:

\documentclass[landscape]{scrartcl}

But sometimes you want to have only one single page in landscape format while the rest of the document should be your normal portrait format. This is very simple to achieve with the package ‘pdflscape’. Include the package in the preamble:

\usepackage{pdflscape}

Then when you are at the point where you would like to insert your landscape page put all the content inside a ‘landscape’ environment.

\begin{landscape}
Hello world!
\end{landscape}

A new page will be started at that point, so it might cause half-empty pages, so think about where to insert your landscape page. But anyway, you should only do such things for big tables or figures preferredly in the appendix.

Align the top of a TikZ picture with the text

Usually the bottom of a tikzpicture is aligned with the baseline of the text. If you want to change that, you can use the option baseline on the picture. This lets the bottom of the tikzpicture be 5 ex below the baseline of the text:

\begin{tikzpicture}[baseline=-5ex]
\draw [fill=blue!20!white] (0,0) circle [radius=1.5];
\draw [red, very thick, dashed] (1,0) -- (0,0) -- (0,1);
\end{tikzpicture}

Instead of manually calculating the height of the drawing you want to include, you can use the provided current bounding box which is the rectangle containing all of the elements in the picture. This is a node with the usual anchors, i.e., north, south, east, west. To align the top of the box with the baseline of the text, use the following (note the parentheses, it’s a reference to a node!):

\begin{tikzpicture}[baseline=(current bounding box.north)]
...

Now probably you don’t want to align your picture with the baseline but with the top of the text. You can play around with the tikzpicture baseline further by introducing a yshift. For example a lowercase x is 1 ex high, so to align the top of the tikzpicture with the top of a lowercase x use a yshift of 1 ex (for uppercase letters or t, f, etc. try 1.6 ex). Be careful to copy all the different {( and [!:

\begin{tikzpicture}[baseline={([yshift={-1ex}]current bounding box.north)}]
...

Useful tweaks for LaTeX beamer posters

To increase the distance between an itemize item and the text:

\setbeamertemplate{itemize item}{\rule[0.5ex]{0.5ex}{0.5ex}~}

To make description items bold:

\setbeamertemplate{description item}{\textbf{\insertdescriptionitem}}

Nicer blocks with rounded edges and a bold title:

\setbeamerfont{block title}{family=\bf}
\setbeamertemplate{blocks}[rounded] 

Pie charts with LaTeX TikZ

Define a new command to insert a pie slice:

\newcommand{\slice}[4]{
  \pgfmathparse{0.5*#1+0.5*#2}
  \let\midangle\pgfmathresult

  % slice
  \draw[thick,fill=black!10] (0,0) -- (#1:1) arc (#1:#2:1) -- cycle;

  % outer label
  \node[label=\midangle:#4] at (\midangle:1) {};

  % inner label
  \pgfmathparse{min((#2-#1-10)/110*(-0.3),0)}
  \let\temp\pgfmathresult
  \pgfmathparse{max(\temp,-0.5) + 0.8}
  \let\innerpos\pgfmathresult
  \node at (\midangle:\innerpos) {#3};
}

Then define the slices in the order you want to have them and with the percentages and labels. You can start at a different point in the circle by setting the counter ‘d’ to a different value before the loop, e.g. \setcounter{d}{25}.

\begin{tikzpicture}[scale=3]
\newcounter{c}
\newcounter{d}
\foreach \p/\t in {66/, 17/Equative, 10/Difference, 7/}
  {
    \setcounter{c}{\value{d}}
    \addtocounter{d}{\p}
    \slice{\thec/100*360}
          {\thed/100*360}
          { \small \p\%}{\t}
  }
  \node[label=0.5:Ranked] at (1,0.6) {};
  \node[label=0.5:Superlative] at (1,-0.3) {};
\end{tikzpicture}

I didn’t like the automatic placement of two labels, that is why I gave ‘Ranked’ and ‘Superlative’ an empty label in the loop and placed them by hand later on.

The original is from Texample, uploaded by Robert Vollmert.

Set height of a minipage

The width of a minipage is a mandatory parameter. But hidden in the optional parameters is a way to set a specific height for a minipage.

The first parameter is the position of the minipage relative to the baseline. Possible values are ‘t’ (top of the minipage is level with the line), ‘c’ (center of the minipage is level with the line) or ‘b’ (bottom of the minipage is level with the line).

The second parameter is the height. The minipage will have exactly this height. If the text inside the minipage is longer, it will spill out of the box, the height is not adjusted. If the text is shorter, the remainder of the box will be empty.

The last parameter is the vertical position of the text inside the minibox. Possible values are ‘t’ (top-aligned), ‘c’ (centered), and ‘b’ (bottom-aligned).

\begin{minipage}[t][5cm][t]{0.5\textwidth}
test
\end{minipage}

LaTeX a5 paper size

In theory, the option ‘a5paper’ should give you a page of A5. Unfortunately, this option only sets the are in which LaTeX typesets, not the physical output pdf size. You need to load the ‘geometry’ packet to achieve this:

\documentclass[a5paper]{scrartcl}
\usepackage[pass]{geometry}

LaTeX citations as used in the NLP community

If you read NLP literature, you will find literature refernces of the form “The first work on this task was done by Smith and Miller (2006). Similar techniques are used in information retrieval (Doe and Norman, 2010).”

This is quite different from what LaTeX usually provides – numbered citations like [1] with ‘plain’ or cryptic letter-number combinations like [SM06] with ‘alpha’. The closest you can get out of the box is ‘apalike’ which would give you [Smith and Miller, 2006].

So what to do?

1. Option: Use a bibliography style provided by some NLP conference, e.g., from NAACL 2013. They will generally offer \newcite to get Smith and Miller (2006) and \cite to get (Doe and Norman, 2010).

2. Option: Use natbib which offers \cite to get Smith and Miller (2006) and \citep to get (Doe and Norman, 2010). Additionally, natbib can do much more, e.g., you can add text into the parenthesis.

Minimal example:

\documentclass[a4paper]{scrartcl}
\usepackage{natbib}
\bibliographystyle{apalike}
\begin{document}
The first work on this task was done by \cite{SmithMiller2006}.
Similar techniques are used in information retrieval \citep{DoeNorman2010}.
\bibliography{literatur}
\end{document}

So, why not use both, some aclstyle and natbib together? Well… they are not compatible (or at least I was not able to make it work).

Precision-Recall-Curves and Mean Average Precision

Precision-recall curves are often used to evaluate ranked results of an information retrieval system (e.g., a search engine). The principle is easy, for every search result, check the precision and recall you have until now (precision/recall at k). If you plot this in a graph with recall on the x-axis and precision on the y-axis, you end up with something like this (blue line):

The essential shape is always the same. Why? Let’s say we have looked at k results which corresponds to a point with a precision and a recall value. What can happen when we go to result k+1? The result can be correct, then recall will increase and precision as well – the curve goes up and right. Or it can go wrong, then recall stays the same and precision drops – the curve goes straight down.

The red line is the interpolated precision, meaning we define precision at some arbitrary level to be the maximum precision reached at any later recall level. Essentially, we flatten the "teeth" of the curve. The difference can be pretty big (see in the plot at recall around 0.2), we can even "skip" a tooth.

What would the curve look like for a perfect system? Meaning a system that only returns correct results? It would be 1.0 for every recall level. A system that never returns a correct result? 0 for every recall level.

What should the value be for precision at recall 0? If we interpolate, the answer is clear: the highest precision value at some later recall level. This does not have to be 1.0 – it could happen that the first result is wrong, the second correct, then we have P=0.5 at k=2 and it might only drop from there.


Sancho McCann. It’s a bird… it’s a plane… it depends on your classifier threshold. 2011.
Christopher D. Manning, Prabhakar Raghavan and Hinrich Schütze. Introduction to Information Retrieval. Cambridge University Press, 2008. (Chapter 8)

CV in LaTeX with moderncv

It’s easy:

\documentclass[11pt,a4paper]{moderncv}
[...] % load your usual packages

% moderncv themes
\moderncvcolor{grey} % color options 'blue' (default), 'orange', 'green', 'red', 'purple', 'grey' and 'black'
\moderncvstyle{casual} % style options are 'casual' (default), 'classic', 'oldstyle' and 'banking'

% Your contact data
\firstname{Max}
\familyname{Mustermann}
\address{Thisstreet 1}{12345 Exampleville}
\phone{ (01\,23)~45\,67\,89\,00}  
\email{me@myself.de}    
\photo[64pt]{myself}

\begin{document}
\maketitle

\section{Education}
\cventry{year--year}{Degree}{Institution}{City}{\textit{Grade}}{Description}
[...]
\end{document}

I had the problem that when you specify months and years instead of only years, the left margin is too small. You can adjust that with these two lines:

\setlength{\hintscolumnwidth}{0.25\textwidth}
\AtBeginDocument{\recomputelengths}

Also, the languages take up a lot of space, you can just put it into two columns to save space. This is better than the cvdoubleitem in my opinion, because you are more flexible to change the order. I suppose you can still add the comment, if you need to, try it out.

\begin{multicols}{2}
\cvitem{Language 1}{Skill level}
[...]
\end{multicols}