Mini-titlepages

Another strange problem. I am teaching LaTeX and I usually have slides that say “write this” and “get this”, e.g., write \textbf{bold text} and get bold text.

So I wanted to do the same for the part where I teach beamer. Of course my slides are beamer slides, so I was curious what would happen if I simply include a title page or a table of content inside a minipage. Yes, it works, it inserts the part with the title respectively the table of contents. Of course what is missing is the frame layout, so I had to do a bit of adjusting there. But in the end I have a miniature version of the actual title page and table of content right there in the slide. This is how it works:

First, I used a minipage to get the frame aspect ratio right:

\begin{minipage}[c][0.7\textwidth][c]{\textwidth}
\end{minipage}

Around that a resizebox so that it comfortably fits onto my page below the code that explains how to do it, and an fbox to get some “frame feeling”.

\fbox{\resizebox{0.4\textwidth}{!}{
\begin{minipage}[c][0.7\textwidth][c]{\textwidth}
\end{minipage}
}}

And basically that’s it for the title page, only a bit more space below the text:

\fbox{\resizebox{0.4\textwidth}{!}{
\begin{minipage}[c][0.7\textwidth][c]{\textwidth}
\titlepage
\vfill
\end{minipage}
}}

For the table of contents I had to re-make the frame layout with the navigation bars and the frametitle. The theme is Madrid, which is pretty simple, so I only had to do a blue bar on top and another one at the bottom. For that I used a beamercolorbox which I guess is also used in the theme itself. The values for height and the rest are pure trial and error. I didn’t include the title, author, date etc. at the bottom, although that shouldn’t be too difficult.

\fbox{\resizebox{0.4\textwidth}{!}{
\begin{minipage}[c][0.7\textwidth][c]{\textwidth}
\vspace{-0.55\baselineskip}
\begin{beamercolorbox}[dp=1ex, ht=3ex, wd=1.065\textwidth]{frametitle}
\usebeamerfont{frametitle}Outline
\end{beamercolorbox}
\vspace{\baselineskip}
\vfill
\tableofcontents
\vfill
\begin{beamercolorbox}[ht=1.5ex, wd=1.065\textwidth]{frametitle}
\end{beamercolorbox}
\vspace{-0.6\baselineskip}
\end{minipage}
}}

Maybe not useful, but really cool to have on the slides!

Inputenc and fontenc

Your typical LaTeX document imports two packages right at the beginning:

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

What do they do?

fontenc choses an output font for encoding characters. This is important for special characters to have correct hyphenation, ligatures and kerning. Also if you use special characters and want to copy them out of the pdf, when the package is missing you will get wrong results. For example, an “ä” will become “a” plus diacritics instead of just one single letter. Also some other characters may give unexpected results. T1 contains characters for most European languages.

inputenc allows the user to input special characters (like ä, ß, ñ) directly instead of escaping them. This is very nice if you are writing in a language other than English. The value should match the encoding with which the file has been written. If you have a file encoding that’s different from what you specify for inputenc, you will get errors similar to “input character XYZ is not defined”. You should always write all of your documents in UTF-8 nowadays, so that would be the usual value.

It is best to call fontenc first and then inputenc (though I have read conflicting information on that and I haven’t really seen a good reason for it).

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).