A numbered environment for example sentences

I want an environment for example sentences where sentences are numbered and can be referenced. This is common in publications in the area of linguistics and there are a few packages that do the trick. But here is my own.

First, we need a counter that provides the number for the sentence:

\newcounter{mySentence}

We reset sentence counter for each chapter:

\@addtoreset{mySentence}{chapter}

We print the sentence number as (chapter.sentence):

\renewcommand*{\themySentence}{(\thechapter.\arabic{mySentence})}

Then we define an environment for one example sentence, where the number will be printed in the beginning of the line and then the sentence follows:

\newcommand{\backskip}{\vspace{-0.4\baselineskip}}
\newenvironment{examplesentence}
{% start env
\backskip%
\stepcounter{mySentence}%
\begin{enumerate} \small%
\renewcommand{\theenumi}{\thechapter.\arabic{mySentence}}%
\renewcommand{\labelenumi}{\themySentence}%
\itemsep0pt \parskip0pt%
\item
}
{% end env
\end{enumerate}
\backskip
}

Next is an environment for more than one example sentence. Here the sentence number will be printed in the beginning of the line and the individual sentences get an a., b., c., in front of them:

\newenvironment{examplesentences}
{% start env
\begin{examplesentence}%
\begin{enumerate} \itemsep0pt \parskip0pt%
\renewcommand{\theenumii}{\alph{enumii}}%
\renewcommand{\labelenumii}{\alph{enumii}.}%
}
{% end env
\end{enumerate}
\end{examplesentence}
}

LaTeX package “wrapfig”

LaTeX is all nice and fancy if you write technical texts, where the pictures are floating in the text (mostly at the top and/or bottom of pages) and you reference them with numbers. But as I do all sorts of things with LaTeX, sometimes I want more “fun” texts which have pictures somewhere in the pages and text flowing around them.

For this purpose, I have now discovered the package wrapfig:

\usepackage{wrapfig} 

You can include a picture like this (this one floats left of the text with a width of 7em):

\begin{wrapfigure}{l}{7em}
\centering
\includegraphics[width=\linewidth]{AuthorOfArticle.png}
\end{wrapfigure}

You can control some of the appearance with different settings in the preamble (see the documentation at CTAN), e.g.,

\intextsep0.5ex

Overlays with Code Listings

You cannot include a lstlisting (package listings) in a only or visible command in LaTeX beamer. BUT you can define the listing beforehand and then include that inside the only or visible!

Example (from slides about recursion in Java):

\defverbatim{\Lst}{
\begin{lstlisting}
public int fakultaet(int n) {
   if ( n == 1 ) {
      return 1;
   } else {
      return n * fakultaet( n-1 ) ;
   }
}
\end{lstlisting}
}

\begin{frame}[fragile]
\frametitle{Aufgabe: Fakultät von $n$}

Definition:
\begin{itemize}
   \item \lstinline{fakultaet( 1 ) = 1}
   \item \lstinline{fakultaet( n ) = n * fakultaet( n-1 ) }
\end{itemize}

\bigskip

Java Code: 
\visible<2|handout:0>{\Lst}

\end{frame}

Discontinuous x axis with pgfplots

Having a discontinuous y axis is common and Stackoverflow has a few solutions for that. I wanted an x axis with a gap (values 0-10 plus value 20). So this is what I did.

I create an axis from 0 to 12 and give 12 the label “20”. I add an extra tick on the x-axis at about halfway between 10 and “12”, where I want the gap and make it thick and white – basically I want a break in the axis. Then over that break I draw the “label” of this tick, which is two vertical lines at an angle, symbolizing the discontinuity. The relevant part of the style:

xmin=0,
xmax=12.5, 
xticklabels={0, 2, 4, 6, 8, 10, 20},
extra x ticks={11.1},
extra x tick style={grid=none, tick style={white, very thick}, tick label style={xshift=0cm,yshift=.50cm, rotate=-20}},
extra x tick label={\color{black}{/\!\!/}},

And then I add the data with x-values 20 at x-coordinate “12”:

\addplot coordinates {
(0, 43.3) (1, 43.2) (2, 43.3) (3, 42.9) (4, 42.1) (5, 41.4) 
(6, 41.2) (7, 41.7) (8, 41.7) (9, 42.1) (10, 42.1) }; 
\pgfplotsset{cycle list shift=-1}
\addplot coordinates { (12, 43.8) };
\draw[dotted] (axis cs:10, 42.1) -- (axis cs:12, 43.8);

Adding the last point separately from the rest of the data serves the purpose that I can draw the dotted line by hand. cycle list shift=-1 causes the new “plot” to have the same style as the previous. There might be a way of doing this, but this works.

Hat tip: Stackoverflow, but I currently cannot find the question(s) and answer(s) that helped me solve this. Still, thank you, anonymous people.

Include pages from a pdf into a LaTeX beamer presentation

As you know, I do basically everything with LaTeX. But, I have colleagues who work with other tools and sometimes we exchange slides. Fortunately by now people have realized that I don’t like to get weird formats, so they send me pdfs. Yay!

It is actually really easy to include pages from a presentation in pdf format into a LaTeX beamer presentation. You will need the package pdfpages and then just write:

{
\setbeamercolor{background canvas}{bg=}
\includepdf[pages=3-8]{slides.pdf}
}

The first line is necessary, because it seems like otherwise the pdf slides end up being inserted behind the background of the slides, which doesn’t make so much sense to me, but anyway.

You can also include one pdf page into a beamer-slide (“frame”). This is useful if you want to edit the slide a bit, for example to hack your own footer back into the slide to get consistent page numbering:

{
\setbeamercolor{background canvas}{bg=}
\begin{frame}[t]
\includepdf[pages=3]{slides.pdf}

\vspace{0.81\paperheight} % go down to where we want the footer

\hspace*{0.31\paperwidth} % space to the left
\begin{minipage}{0.6\paperwidth} % insert my footer
\tiny\colorbox{white}{~\insertshortauthor: \insertshorttitle} 
\hfill \insertframenumber ~/ \inserttotalframenumber
\end{minipage}

\end{frame}
}

Encoding question mark in TikZ

I’m trying to draw the question mark that is sometimes displayed when there are encoding issues: �

T-shirt Sch... Encoding

This is my solution:

\tikz[baseline=(wi.base)]{
\node[fill=black,rotate=45,inner sep=.1ex,text height=1.8ex,text width=1.8ex] {};
\node[font=\color{white}] (wi) {?};
}

Marking significance in a bar plot

And still on the topic of LaTeX presentations, this time trying to plot a symbol over a bar to indicate significance.

This is how it works:

\node[xshift=\pgfkeysvalueof{/pgf/bar shift},anchor=south] at (axis cs:Xcoord1,0.47) {$\bullet$}; 

You need to put this code directly after the point where the data series has been plotted. Example:

\begin{tikzpicture}
\begin{axis}[xtick=data,axis x line*=bottom,axis y line=left,symbolic x coords={Xcoord1, Xcoord2}]

\addplot [ybar,seagreen] coordinates {(Xcoord1, -0.027) (Xcoord2, 0.436)}; 
\node[xshift=\pgfkeysvalueof{/pgf/bar shift},anchor=south] at (axis cs:Xcoord2,0.47) {$\bullet$}; 
\addlegendentry{System 1}

\addplot+ [ybar,blue] coordinates  {(Xcoord1, 0.331) (Xcoord2, 0.095)}; 
\node[xshift=\pgfkeysvalueof{/pgf/bar shift},anchor=south] at (axis cs:Xcoord1,0.36) {$\bullet$};
\addlegendentry{System 2}

\addplot+ [ybar,orange] coordinates {(Xcoord1, 0.222) (Xcoord2, 0.441)}; 
\node[xshift=\pgfkeysvalueof{/pgf/bar shift},anchor=south] at (axis cs:Xcoord1,0.25) {$\bullet$};
\node[xshift=\pgfkeysvalueof{/pgf/bar shift},anchor=south] at (axis cs:Xcoord2,0.47) {$\bullet$};
\addlegendentry{System 3}
\end{axis}
\end{tikzpicture}

Overlays for bar charts (take 2)

A while back I posted about using overlays for bar charts to show one value at a time. For my latest presentation I had a similar but slightly different wish: show all values for one system at a time, one system after the other.

Easily done, I just adapt the code from my previous post to show all values at the same time:

\newcommand{\addplotoverlay}[3][]{
\alt<#3->{
\addplot+ [ybar,#1] coordinates {#2}; 
}{
\addplot+ [ybar,#1] coordinates {(Xcoord1,0)}; % + don't show zero values in plot
}
}

This is specific to my plot, Xcoord1 is one of my symbolic x-coordinates in the plot. Other than that, the code is completely independent from the used coordinates and the number of them, which makes it more flexible than my old stuff.

Usage (this will let seagreen bars at the given coordinates appear on slide 2):

\addplotoverlayrank[seagreen]{(Xcoord1, 0.331) (Xcoord2, 0.095)}{2}

LaTeX ‘correct’ and ‘wrong’ symbols with TikZ

A symbol for a checkmark to indicate something is correct:

\newcommand{\correct}{$\color{green}\tikz\fill[scale=0.4](0,.35) -- (.25,0) -- (1,.7) -- (.25,.15) -- cycle;$}

A symbol for a cross to indicate something is wrong:

\newcommand{\wrong}{$\mathbin{\tikz [x=1.4ex,y=1.4ex,line width=.2ex, red] \draw (0,0) -- (1,1) (0,1) -- (1,0);}$}%

You’ll need TikZ for this.

LaTeX presentation background picture

In one slide of a presentation I wanted to have a background picture and overlay it with several text blocks one after the other to have the effect of the text “coming out of” the background. It is tricky to align things in LaTeX beamer, especially if you want to have them on top of each other, so this is my solution: Two minipages that cover the whole slide on top of each other.

A slide is more or less 7cm high (depending a bit on your template). There probably is a length defined for that, but I was too lazy to look for it so I took the actual value. The width of the slide is of course \textwidth. I use vertically centered alignment for the minipage, but that is up to you (see the post Set height of a minipage for the options you can give to minipage).

The way it now works is the following. Create one minipage of full width and height. Use this to display the background image. Then jump back the full height and create a second minipage of full width and height to display the text inside of that. This is the code for my slide:

\begin{minipage}[c][7cm][c]{\textwidth}
\centering
\includegraphics[width=0.8\linewidth]{img/Reviews}
\end{minipage}

\vspace{-7cm}
\begin{minipage}[c][7cm][c]{\textwidth}
\centering

\visible<2->{
\colorbox{white}{\fbox{\textcolor{blue}{I was impressed by the fast shutter speed of D3200.}\only<3->{\textcolor{darkgreen}{~(\emph{positive})}}}}
}

\vspace{1cm}
\visible<4->{
\colorbox{white}{\fbox{\textcolor{blue}{The autofocus was \textbf{not} so reliable.}\only<5->{\textcolor{red}{~(\emph{negative})}}}}
}
\end{minipage}