Skip a style for a bar in a bar plot

Let’s say you add five data series to a bar plot and they would get the colors blue – red – brown – gray – purple. Now suppose you have another plot with only four data series, but you would like them to have the colors blue – red – gray – purple, because they are similar to the series 1, 2, 4 and 5 in the first plot. You also don’t want to change the order. What can you do?

The style (colors, markers, etc) for a dataseries are determined by the cycle list in pgfplots. This is a series of style definitions that are applied to your data series one after the other. You can of course define one cycle list for each of the plots and assign the colors the way you want:

\pgfplotscreateplotcyclelist{my five bars}{%
solid,fill,blue, \\%
solid,fill,red, \\%
solid,fill,brown, \\%
solid,fill,gray, \\%
solid,fill,purple, \\%
}
\pgfplotscreateplotcyclelist{my four bars}{%
solid,fill,blue, \\%
solid,fill,red, \\%
solid,fill,gray, \\%
solid,fill,purple, \\%
}

\begin{tikzpicture}
\begin{axis}[cycle list name=my five bars,...]
... add the five data series ...
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[cycle list name=my four bars,...]
... add the four data series ...
\end{axis}
\end{tikzpicture}

But you always need to remember to change both versions. Fortunately there is an easier way! You can shift the index of the cycle list:

\begin{axis}[cycle list name=my five bars,...]
... add first two data series ...
\pgfplotsset{cycle list shift=1} % Skips one style
... add the other two data series ...
\end{axis}
\end{tikzpicture}

Done!

Plot legend in figure caption

If you have a plot and add names to the data series with \addlegendentry{}, a legend is added to the plot that specifies the names for the lines or bars. This is an example plot from my thesis:

\begin{figure}
\begin{tikzpicture}
\begin{axis}[myplot]
\addplot plot coordinates {(1, 0.17) (2, 0.13) (3, 0.09) (4, 0.06) (5, 0.01) (6, 0.01)};
\addlegendentry{System1} % for legend
\addplot plot coordinates {(1, 0.13) (2, 0.16) (3, 0.16) (4, 0.14) (5, 0.14) (6, 0.13)};
\addlegendentry{System2} % for legend
\end{axis}
\end{tikzpicture}
\caption{Results (Precision) at different $k$ for the two systems}
\end{figure}

You can influence the placement and appearance of the legend and it looks really professional. Most of the time that will be exactly what you want. But not always. I wanted the legend to be embedded in the text of the caption (long story why). And this is possible and even quite simple! You just need to define a label for the data series and when you refer to that label, a picture of the line and marker is drawn as the reference.

\begin{figure}
\begin{tikzpicture}
\begin{axis}[myplot]
\addplot plot coordinates {(1, 0.17) (2, 0.13) (3, 0.09) (4, 0.06) (5, 0.01) (6, 0.01)};
\label{tikz:System1}
%\addlegendentry{System1} % remove to get rid of legend
\addplot plot coordinates {(1, 0.13) (2, 0.16) (3, 0.16) (4, 0.14) (5, 0.14) (6, 0.13)};
\label{tikz:System2}
%\addlegendentry{System2}
\end{axis}
\end{tikzpicture}
\caption{Results (Precision) at different $k$ for the two systems \ref{tikz:System1} System 1 and \ref{tikz:System2} System 2}
\end{figure}

Pgfplots is cool!

Hat tip: Stackexchange (the question is about something else)

Handouts from slides

Creating handouts from slides created with LaTeX beamer is simple. Just specify the option “handout” in the documentclass:

\documentclass[handout]{beamer}

In your slides, you can use the marker presentation and handout to give specific instructions that apply only when the pdf is created in presentation mode or in handout mode. So for example you can have different templates for your slides [there are spaces around the < because otherwise they are not displayed in this blog... delete them when you write real LaTeX]:

\mode < presentation >
{
\usetheme{Madrid}
}
\mode < handout >
{
\usetheme{Szeged}
\usecolortheme{beaver}
}

You can have things only on the handout or only on the slides:

\visible<1->{This is the same for handout and slides.}
\visible<1-|handout:0>{This won't be in the handout.}
\visible<1|handout:2>{This is on the same slide for the presentation,
but on a different slide for the handout.}
\visible<2|handout:1>{This is on a different slide for the presentation,
but on the same slide for the handout.}

Typesetting text in math mode (2)

In a previous post (Typesetting text in math mode) I advertised the use of \mbox to write text in mathematical formulas. This works when you are in the "standard size", but looks funny if you have subscripts because the sizes are off:

$ 50 \mbox{ apples}_{\mbox{yellow}} \times 
100 \mbox{ apples}_{\mbox{red-green}} 
= \mbox{lots of apples}^{\mbox{to eat}} $

looks like
50 \mbox{ apples}_{\mbox{yellow}} \times 100 \mbox{ apples}_{\mbox{red-green}} = \mbox{lots of apples}^{\mbox{to eat}}

In these cases (and also in the standard cases but there it looks the same), you can use the command \text which will come out in the right font size. In addition to just \text, there is also \textbf (bold face), \textit (italics) and \texttt (typewriter).

$ 50 \text{ apples}_{\text{yellow}} \times 
100 \textit{ apples}_{\texttt{red-green}} 
= \textbf{lots of apples}^\text{to eat} $

looks like
50 \text{ apples}_{\text{yellow}} \times 100 \textit{ apples}_{\texttt{red-green}} = \textbf{lots of apples}^\text{to eat}

Note: Most of the time \text should just work in math mode without any packages, but for some distributions you need to explicitly load the package amstext or amsmath.

List your publications before the bibliography

Usually, in academic texts you cite stuff and at the end there is the bibliography that contains the full entries for all things referenced in the text. But there are some situations where you want to list some complete bibliography entries beforehand, somewhere in the text. For example you may want a list of prior work somewhere near the beginning of a grant proposal or a list of things published during the grant period somewhere at the end, but separate from the bibliography. Of course, you can write this list by hand, but where would be the fun in that?

And of course there is a LaTeX package for that, bibentry. You include the package with your bibliography style in the preamble. You can include it together with natbib.

\bibliographystyle{apalike} % or any other style you like
\usepackage{natbib} % optional, but combination is possible
\usepackage{bibentry}

Then, also in the preamble, you "turn off" the regular bibliography with \nobibliography. After that you can create your list of stuff somewhere in the document, but you will not have a bibliography at the end. Which is probably not what you want. So to additionally be able to include the references in the usual way, use this snippet:

\nobibliography*
\let\oldthebibliography=\thebibliography
\let\endoldthebibliography=\endthebibliography
\renewenvironment{thebibliography}[1]{%
   \begin{oldthebibliography}{#1}%
   \setlength{\parskip}{0ex}%
   \setlength{\itemsep}{0ex}%
}%
{%
   \end{oldthebibliography}%
}

The citing commands (\cite, \citep, etc.) and what they produce are unchanged, but now you can use \bibentry at any point in the text to create the full bibliographic entry. The formatting will be the same as for the references in the bibliography:

Parts of this work have been published in: \bibentry{Kessler2014}

Overlays and verbatim

Another weird LaTeX problem. I have a piece of code on my slide and the result it gives. I want to change the code slightly and visualize the change in the result. Normally in LaTeX beamer slides, I would use overlays like this:

Query:
\begin{verbatim}
some code
\alt<2>{slightly changed code on slide 2}{original code on slide 1}
some more code
\end{verbatim}

Result:
this item is the same in both
\visible<1>{this one is only there for the original code}
\visible<2>{this one is only there for the changed code}

So far, so good. The code is in a verbatim environment, so I have cannot put the overlay around the line I want to change, but that’s fine, let’s make it an alternative around the whole verbatim part. But, unfortunately, the problem is that you cannot put a verbatim environment inside of overlays (learn why). So you have to hack it. This is the code I want, the line with FILTER is the one I only want to have on the second slide:

\begin{verbatim}
SELECT ?book ?author ?releasedate
WHERE {
   ?book dbo:author ?author .
   {
      ?book dbp:releaseDate ?releasedate . 
   } UNION {
      ?book dbp:pubDate ?releasedate . 
   }
   FILTER (?releasedate > 1950)
}
\end{verbatim}

Like in my post on using verbatim inside of verbatim, I have to end the verbatim environment prematurely, skip back over the space and then I can include the overlay inside of verb.

\begin{verbatim}
SELECT ?book ?author ?releasedate
WHERE {
   ?book dbo:author ?author .
   {
      ?book dbp:releaseDate ?releasedate . 
   } UNION {
      ?book dbp:pubDate ?releasedate . 
   }
\end{verbatim}
\vspace{-0.5\baselineskip}
\verb|  |\visible<2>{\texttt{FILTER (?releasedate > 1950 )}}\\
\verb|}|
\\

Not the most elegant way, but it works…

Overlays for bar charts

Yesterday I posted about creating bar charts with TikZ and pgfplots.

Today I want to present a command to make the bars of one data series (i.e., one of my systems) appear one after the other on a beamer LaTeX slide.

This is the code to put into your preamble:

\newcounter{MyNextSlide}
\newcounter{MyNextNextSlide}
\newcommand{\addplotoverlay}[5][]{
\setcounter{MyNextSlide}{#5}
\stepcounter{MyNextSlide}
\setcounter{MyNextNextSlide}{\theMyNextSlide}
\stepcounter{MyNextNextSlide}
\alt<#5->{\only<#5->{\alt<\theMyNextSlide->{\alt<\theMyNextNextSlide->{
\addplot+ [ybar,#1] coordinates {#2 #3 #4}; 
}{
\addplot+ [ybar,#1] coordinates {#2 #3}; 
}}{
\addplot+ [ybar,#1] coordinates {#2}; 
}}}{
\addplot+ [ybar,#1] coordinates {(PI,0)}; % + don't show zero values in plot
}
}

Usage (‘first slide’ refers to the slide on which value 1 should first appear, it will stay and the slide afterwards will add value 2, the slide after that will add value 3):

\addplotoverlay [color or other options] {value 1}{value 2}{value 3}{first slide}

This depends on there being three data points in a data series and I have hardcoded the x coordinate PI. You’ll probably need to adjust this before you are able to do something useful with this code.

Bar charts in LaTeX with TikZ

I have four systems to compare (baseline, minimal, window, syntax) on three different tasks (let’s call them PI, AI and AC). I want a bar chart (similar to this example). We of course use TikZ and pgfplots and there is ybar to get a bar chart. The outer bars are cut off, so we need to add a little space on both sides with enlarge x limits. We can play around with the axes, the height and the width of the plot and the legend, but you can look at other examples for this, I’ll focus on two things here.

First, I would like to have the three tasks side by side with a nice name. In TikZ we can use symbolic x coordinates for this, we just give them some names and can then use them like any other x coordinate, e.g., to put a data point at (PI, 50). We can give the coordinates labels that are nicer to read with xticklabels. Usually there will be ‘ticks’ (i.e., markers on the x axis) somewhere randomly, to get only for each x-axis label/task, use xtick=data.

symbolic x coords={PI, AI, AC},
xticklabels={Pred. ident., Arg. ident., Arg. class.},
xtick=data,

Second, I would like to have the numbers above the bars with one decimal place. We can get the numbers with these two lines (the first one gives the numbers, as they are too big the second line adjusts the font size):

nodes near coords={\pgfmathprintnumber[fixed zerofill,fixed,precision=1]{\pgfplotspointmeta}}
every node near coord/.append style={font=\tiny}

To get rid of zeros, we can replace the second line with

every node near coord/.append style={
      check for zero/.code={
        \pgfmathfloatifflags{\pgfplotspointmeta}{0}{
           \pgfkeys{/tikz/coordinate}
        }{}
      }, 
      check for zero, font=\tiny},

So this is my final axis style:

\pgfplotsset{resultsplot/.style={
axis x line*=bottom, 
axis y line=left, 
ybar,
symbolic x coords={PI, AI, AC},
xticklabels={Pred. ident., Arg. ident., Arg. class.},
xtick=data,
enlarge x limits=0.2,
nodes near coords={\pgfmathprintnumber[fixed zerofill,fixed,precision=1]{\pgfplotspointmeta}},
every node near coord/.append style={
      check for zero/.code={
        \pgfmathfloatifflags{\pgfplotspointmeta}{0}{
           \pgfkeys{/tikz/coordinate}
        }{}
      }, check for zero, font=\tiny},
area legend,
legend style={at={(0.5,-0.12)},
anchor=north,legend columns=-1},
}
}

And now we can get the actual graph that uses this axis style. Each plot represents a different system (the numbers are F1 scores):

 
\begin{tikzpicture}
\begin{axis}[resultsplot]
\addplot+ [ybar,green] coordinates {(PI, 67.8) (AI, 30.6) (AC, 20.2)};
\addlegendentry{Baseline}
\addplot+ [ybar,blue] coordinates {(PI, 78.6) (AI, 21.2) (AC, 16.5)};
\addlegendentry{Minimal system}
\addplot+ [ybar,orange] coordinates {(PI, 80.0) (AI, 44.2) (AC, 36.6)};
\addlegendentry{Window}
\addplot+ [ybar,red] coordinates {(PI, 80.1) (AI, 54.2) (AC, 44.8)};
\addlegendentry{Syntax}
\end{axis}
\end{tikzpicture}

Have fun!

Writing verbatim inside verbatim

More strange problems, still teaching LaTeX. So I wanted to teach people how to get text or code formatted exactly as they type it with the verbatim environment. This is the part of code I wanted to use as an example:

\begin{verbatim}
text inside a      verbatim environment
is printed \emph{exactly} as     you\\
type      it ! % no comment!
\end{verbatim}

What I usually do is I have a block that shows the code they have to write and then the result. And of course for showing the code they need to write, I use verbatim. So can you include verbatim inside verbatim?

Well, yes you can, but of course you cannot include \end{verbatim}, as this would be interpreted as ending the verbatim environment. So I had to add this separately afterwards, using \verb (inline verbatim). Voila:

\begin{block}{What you write}
\begin{verbatim}
\begin{verbatim}
text inside a      verbatim environment
is printed \emph{exactly} as     you\\
type      it ! % no comment!
\end{verbatim}
\verb|\end{verbatim}|
\end{block}

Now there’s only a small problem, namely that there is some spacing after the environment, so it seems like there is an empty line between the end of the verbatim text and the \end{verbatim}. For this example it doesn’t matter, I use it to show them that empty lines are also printed exactly as they are given.

But next example is how to include verbatim inside a beamer frame. The example code I wanted to show inside the usual verbatim environment:

\begin{frame}[fragile]
   \begin{verbatim}
      test
   \end{verbatim}
\end{frame}

So you see that here I don’t really want an empty line between \end{verbatim} and \end{frame}. So my final hacky solution jumped back up over that space after the environment:

\begin{block}{What you write}
\begin{verbatim}
\begin{frame}[fragile]
   \begin{verbatim}
      test
\end{verbatim}
\vspace{-0.9\baselineskip}
\verb|   \end{verbatim}|\\
\verb|\end{frame}|
\end{block}

Which works fine, but the more easy solution is probably this one (or use lstlisting or any other code-environment):

\begin{block}{What you write}
\verb|\begin{frame}[fragile]|\\
\verb|   \begin{verbatim}|\\
\verb|      test|\\
\verb|   \end{verbatim}|\\
\verb|\end{frame}|
\end{block}