[This article was first published on Software for Exploratory Data Analysis and Statistical Modelling, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Many presentations created using LaTeX beamer included mathematical equations and these can be easily included in a presentation and in this post we will consider using the tikz package to add various interesting elements to equations, such as lines between text on a slide and part of an equation.

The examples on this page have been inspired by the good examples on global nodes detailed on texample.net. The example can be adapted to apply to equations for various statistical models and we will consider the model for a general row-column experiment design.

To create this example we need to make tikz available in our tex file by adding the following code to the preamble:

``` usepackage{tikz} usetikzlibrary{arrows,shapes} ```

The second and third lines refer to particular elements of tikz that will be used in the example.

If we want to access nodes in different areas of our latex document we need to make use of the remember picture style and the easiest way to do this is to make a global declaration rather than on each specific picture:

``` tikzstyle{every picture}+=[remember picture] ```

The example will be the model for a row-column experiment design and we will have four elements in a bullet list that are linked by arrows to different parts of the equation, which in turn are highlighted in their own box.

We use the itemize environment to add the first bullet point:

``` begin{itemize} item Overall mean tikz[na] node[coordinate] (s1) {}; end{itemize} ```

At the end of the item we add a tikz node so that we can draw a line from the end of this line to one of the boxes in the equation. We also provide a name s1 so that it can be identified by tikz. This node uses a style to shift the location where the line starts from, that we need to define in our document using this code:

``` tikzstyle{na} = [baseline=-.5ex] ```

The next step is to create the equation and the nodes and background boxes for each element of the equation that will be linked to the bullet list. The code for the equation is shown here:

``` begin{equation} y_{ijk} = tikz[baseline]{ node[fill=blue!20,anchor=base,rounded corners=2pt] (d1) {\$mu\$}; } + tikz[baseline]{ node[fill=red!20,anchor=base,rounded corners=2pt] (d2) {\$r_{i}\$}; } + tikz[baseline]{ node[fill=green!20,anchor=base,rounded corners=2pt] (d3) {\$c_{j}\$}; } + tikz[baseline]{ node[fill=yellow!20,anchor=base,rounded corners=2pt] (d4) {\$t_{k}\$}; } + epsilon_{ijk} end{equation} ```

If we look closely at this code the latex for the equation itself is very straightforward and the complication comes from adding a tikz node to four of the elements of the equation. The nodes themselves have colours (blue/red/green/yellow) and rounded corners. They each have a label d1 to d4 so that tikz can draw lines between the bullet list and the elements of the equation.

We then finish off the bullet list with three more elements:

``` begin{itemize} item Effect of row \$i\$ tikz[na] node[coordinate] (s2) {}; item Effect of column \$j\$ tikz[na] node[coordinate] (s3) {}; item Effect of treatment \$k\$ tikz[na] node[coordinate] (s4) {}; end{itemize} ```

Each of the items in this bullet list have their own identifier s2 to s4 that we will use to draw arrows.

We create a separate picture environment for the four arrows linking the bullet list to the equation. Each element is defined as a path between two nodes with additional information about the shape of the arrow.

``` begin{tikzpicture}[overlay] path[->] (s1) edge [bend left] (d1); path[->] (s2) edge [bend right] (d2); path[->] (s3) edge [out=0, in=-90] (d3); path[->] (s4) edge [out=0, in=-90] (d4); end{tikzpicture} ```

There are many variants on this example that could be incorporated in a presentation.

Other useful resources are provided on the Supplementary Material page. Also head over to texample.net for more examples of using the tikz package.