In the next few blog posts we’re going to show you some of the interesting things you can do using the TikZ package. The TikZ package is a package that allows you to draw high quality and often quite complex diagrams. In this post we’re going to show you some of the basics and show you how to draw simple shapes and lines.

To get started with TikZ we need to load up the TikZ package:

\usepackage{tikz}


Now whenever we want to create a TikZ diagram we need to use the ‘tikzpicture’ environment.

\begin{tikzpicture}

<code goes here>

\end{tikzpicture}


## Basic Shapes

One of the simplest and most commonly used commands in TikZ is the \draw command. To draw a straight line we use this command, then we enter a starting co-ordinate, followed by two dashes before the ending co-ordinate. We then finish the statement by closing it with a semicolon.

\draw (0,0) -- (4,0);


We can then add more co-ordinates in like this to make it a square:

\draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- (0,0);


However this isn’t particularly good style. As we are drawing a line that ends up in the same place we started, it is better to finish the statement with the keyword ‘cycle’ rather than the last co-ordinate.

\draw (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle;


To simplify this code further we can use the ‘rectangle’ keyword after the starting co-ordinate and then follow it with the co-ordinate of the corner diagonally opposite.

\draw (0,0) rectangle (4,4);


We can also add lines that aren’t straight. For example, this is how we draw a parabola:

\draw (0,0) parabola (4,4);


To add a curved line we use ‘control points’. We begin with our starting co-ordinate, then use two dots followed by the keyword ‘controls’ and then the co-ordinates of our control points separated by an ‘and’. Then after two more dots we have the final point. These control points act like magnets attracting the line in their direction.

\draw (0,0) .. controls (0,4) and (4,0) .. (4,4);


We can then add a circle like this. The first co-ordinate is the circle’s centre and the length in brackets at the end is the circle’s radius.

\draw (2,2) circle (3cm);


This is how we draw an ellipse. This time the lengths in the brackets separated by an ‘and’, are the x-direction radius and the y-direction radius respectively.

\draw (2,2) ellipse (3cm and 1cm);


This is how we draw an arc. In the final bracket we enter the starting angle, the ending angle and the radius. This time they are separated by colons.

\draw (3,0) arc (0:75:3cm);


To customise the way these lines are drawn we add extra arguments into the \draw command. For example we can edit the circle we drew so that the line is red, thick and dashed.

\draw[red,thick,dashed] (2,2) circle (3cm);


## Grids

Very often when drawing diagrams we will want to draw a grid. To do this we use the \draw command followed by by some additional arguments. For example we specify the grid step size using step= and a length. We’ve also specified the colour ‘gray’ and told it to make the lines ‘very thin’. After these arguments we enter the co-ordinates of the bottom left corner, followed by the keyword ‘grid’ and then the co-ordinates of the top right corner.

\draw[step=1cm,gray,very thin] (-2,-2) grid (6,6);


If we want to remove the outer lines around this grid we can crop the size slightly like this.

\draw[step=1cm,gray,very thin] (-1.9,-1.9) grid (5.9,5.9);


## Colour Filling

Now lets add a shape onto our grid and colour it in. To do this we use the \fill command instead of the \draw command. Then in square brackets we enter a colour. For example this specifies a colour that is 40% blue mixed with 60% white. Then we just specify a closed shape as we would normally.

\fill[blue!40!white] (0,0) rectangle (4,4);


If we wanted to add a border around this shape we could change it to the \filldraw command and then alter the arguments so that we have both a fill colour and a draw colour specified.

\filldraw[fill=blue!40!white, draw=black] (0,0) rectangle (4,4);


If instead of one solid colour we want a colour gradient, we could change it to the \shade command. Then in the square brackets we specify a left colour and a right colour.

\shade[left color=blue,right color=red] (0,0) rectangle (4,4);


Instead of doing it from left to right we could do it from top to bottom.

\shade[top color=blue,bottom color=red] (0,0) rectangle (4,4);


Or we could even change it by specifying an inner and outer colour like this.

\shade[inner color=blue,outer color=red] (0,0) rectangle (4,4);


Finally we could also add a border to this by using the \shadedraw command and adding a draw colour.

\shadedraw[inner color=blue,outer color=red, draw=black] (0,0) rectangle (4,4);


## Axes

Let’s finish this post by adding some labeled axes to our grid. To do this we draw two normal lines both from (0,0), but we’ll make them thick and add arrowheads using a dash and a pointed bracket.

\draw[thick,->] (0,0) -- (4.5,0);
\draw[thick,->] (0,0) -- (0,4.5);


We can also label our axes using nodes. To do this we add the keyword ‘node’ into both draw statements next to the end co-ordinates, followed by an anchor specification in square brackets and the text in curly brackets. Every node we create in TikZ has a number of anchors. So when we specify the north west anchor for the x-axis node, we are telling TikZ to use the anchor in the top left hand corner to anchor the node to the co-ordinate.

\draw[thick,->] (0,0) -- (4.5,0) node[anchor=north west] {x axis};
\draw[thick,->] (0,0) -- (0,4.5) node[anchor=south east] {y axis};


To finish our axes we can add in ticks and numbering like this:

\foreach \x in {0,1,2,3,4}
\draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
\foreach \y in {0,1,2,3,4}
\draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};


This clever piece of code uses two ‘for each’ loops to systematically go along the axes adding the ticks and numbers. In each one, the variable x or y takes on all of the numbers in the curly brackets, each in turn and executes the \draw command.

This concludes our discussion on basic drawing in TikZ. If you want to play around with the document we created in this post you can access it here. In the next post we’ll look exporting TikZ code from GeoGebra. Please do keep in touch with us via Facebook, Twitter & Google+.

Generating TikZ Code from GeoGebra

Creating Flowcharts with TikZ

Circuit Diagrams Using Circuitikz

Creating Mind Maps Using TikZ

Posted by Josh Cassidy on 27 Aug 2013

In the last post we looked at animating our slides using overlay specifications. In this post we’re going to look at editing the way our presentation looks using themes. We’ll also look at printing handouts.

## Presentation Themes

In the first video we set up our presentation with the ‘Boadilla’ theme like this:

\usetheme{Boadilla}


There are lots of different predefined presentation themes available for us to use. Here are a few of them. This is the ‘Bergen’ theme:

There are also themes that include navigation bars, for example the ‘Antibes’ theme:

We could also use a theme that includes a table of contents sidebar, like the ‘Hannover’ theme:

The ‘Singapore’ theme is one that includes what beamer calls a ‘Mini Frame Navigation’:

Finally there are also themes like ‘Warsaw’ that have the section and subsection titles at the top of each frame:

## Colour, Font, Inner & Outer Themes

The presentation themes control everything about how the presentation looks. However beamer gives us more flexibility by allowing us to change the colour theme, font theme, inner theme and outer theme, all separately. This is really handy as it means if we like a theme like ‘Warsaw’ but, for example, want to change the colour theme, then we can load the ‘Warsaw’ presentation theme and then override the colour theme with one of beamer’s predefined colour themes. We change the colour scheme using the \usecolortheme command followed by the colour theme in curly brackets. Note that these themes are different to the presentation themes we used earlier. For example, the ‘crane’ colour theme:

The inner theme dictates the style of the title and part pages, the itemize, enumerate, description, block, theorem and proof environments as well as figures, tables, footnotes and bibliography entries. For example we could also load up the ‘rectangles’ inner theme. We do this using the \useinnertheme command. This has made our table of contents and lists use rectangles as bullet points:

The outer theme dictates the style of the head and footline, the logo, the sidebars and the frame title. We can specify this theme using the \useinnertheme command. As we’re using ‘Warsaw’, by default we are using the ‘shadow’ outer theme, but we could change this to the ‘tree’ theme if we wanted to change the top navigation bar to a tree like structure:

Finally, I could also change the font theme using the \usefonttheme command and a predefined font theme like the ‘serif’ theme.

All the information about the different themes available can be found in the documentation.

## Handouts

Now let’s briefly look at creating handouts for our presentation. To do this we add the keyword ‘handout’ into square brackets in the document class command. We then use the ‘pgfpages’ package to help us print multiple slides on a page. After loading the package we use the \pgfpagesuselayout command. In the curly brackets we specify how many frames we want on a sheet. In the square brackets we specify the paper size and how much border shrink we want:

\documentclass[handout]{beamer}
\usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]


If we wanted to put four frames on a sheet we could simply change the 2 to a 4 and then add the ‘landscape’ keyword into the square brackets.

\pgfpagesuselayout{4 on 1}[a4paper,border shrink=5mm,landscape]


This concludes our discussion on themes and handouts and also our series on creating presentations with beamer. If you want to play around with the presentation we created in this series you can access it here. Do have a look at the video series we produced alongside these blog posts here. Please do keep in touch with us via Facebook, Twitter & Google+.

pt 1 - Getting Started

pt 2 - Lists, Columns, Pictures, Descriptions & Tables

pt 3 - Blocks, Code, Hyperlinks & Buttons

pt 4 - Overlay Specifications

Posted by Josh Cassidy on 22 Aug 2013

In the last post we looked at adding some more interesting content into our presentation. In this post we’re going to look at animating our slides.

## The Pause Command

Often when when doing a presentation we’ll want to reveal parts of a frame one after the other. The simplest way to do this is to use the \pause command. For example, by entering the \pause command before every entry in a list we can reveal the list point by point:

\begin{frame}
\frametitle{List}
\begin{itemize}
\pause
\item Point A
\pause
\item Point B
\begin{itemize}
\pause
\item part 1
\pause
\item part 2
\end{itemize}
\pause
\item Point C
\pause
\item Point D
\end{itemize}
\end{frame}


This brings us on to the difference between a frame and a slide. A single frame is defined as what we build up in a single frame environment, whereas a slide is a single page in the resulting pdf. This means that a frame can be made up of multiple slides. For example, this frame with the list that we’ve just animated is made up of seven slides, but the frame number in the bottom right hand corner of each slide remains unchanged for all of the seven.

## Overlay Specifications

The pause command is useful but isn’t very versatile. To get more flexibility we use what beamer calls ‘overlay specifications’. These specifications can be added to compatible commands using pointed brackets after the command name. For example I can add them to the \item command in a list structure like this.

\begin{frame}
\frametitle{More Lists}
\begin{enumerate}[(I)]
\item<1-> Point A
\item<2-> Point B
\begin{itemize}
\item<3-> part 1
\item<4-> part 2
\end{itemize}
\item<5-> Point C
\item<6-> Point D
\end{enumerate}
\end{frame}


The numbers inside the pointed brackets tell LaTeX which slides the item should appear on. For example, in this list we’ve told each list item which slide number it should first appear on and then told them to appear on all subsequent slides in the frame using the dash. Here’s an example of a more complicated overlay:

\item<-2,4-5,7>


This makes the item appear on slides 1,2,4,5 & 7.

There are a number of commands that enable us to use overlays on text. The main one is the \onslide command which can be configured to achieve a few different outcomes, details of these can be found in the documentation.

\begin{frame}
\frametitle{Overlays}
\onslide<1->{First Line of Text}

\onslide<2->{Second Line of Text}

\onslide<3->{Third Line of Text}
\end{frame}


When we simply give this command text as an argument, it acts in the same way as the \uncover command making the text fully appear only on the specified slides. On unspecified slides the text is covered, so will still take up space but won’t be visible.

To make the text transparent on unspecified slides we use the \setbeamercovered command and enter the keyword ‘transparent’ above the code where we want it to have an effect:

\setbeamercovered{transparent}


Please be aware that this command will affect all of the code following it, so if we want to change it back to the default setting later in the presentation we can simply use the same command again but with the keyword ‘invisible’.

Another command we can use is the \visible command which does the same as \uncover except it leaves the space blank on unspecified slides instead of transparent even if we’ve set the transparency as we did a moment ago. The \invisble command does the exact opposite to of the \visible command. The \only command does the same as the \visible command except it doesn’t take any space up. This means that if we change the \onslide commands to \only commands and get rid of the dashes in the overlay specifications our three lines of text will appear in the same place on the frame in turn.

\begin{frame}
\frametitle{Overlays}
\only<1>{First Line of Text}

\only<2>{Second Line of Text}

\only<3>{Third Line of Text}
\end{frame}


## Overlays and Text Formatting

There are a number of commands to do with text formatting that are compatible with overlay specifications. These commands are simply ignored on slides not declared in the specification and will therefore just print the text as normal. Here are some examples:

\textbf<2>{Example Text}

\textit<2>{Example Text}

\textsl<2>{Example Text}

\textrm<2>{Example Text}

\textsf<2>{Example Text}

\textcolor<2>{orange}{Example Text}

\structure<2>{Example Text}


Which will produce text like this on the first slide:

And then like this on the second slide:

First the \textbf command which makes the text bold, then \textit which puts the text in italics, then \textsl which make it slanted, \textrm which uses the roman font family, \textsf which uses the sans serif font family but this doesn’t change anything because we are already using this font. Next the \textcolor command which puts it in the specified colour, then \alert which puts the text in red by default and finally the \structure command which formats the text in a way that indicates the presentations structure.

## Overlays and Environments

Overlay specifications often work with environments as well. For example we could animate the environments on the maths blocks page like this:

\begin{frame}
\frametitle{Maths Blocks}
\begin{theorem}<1->[Pythagoras]
$a^2 + b^2 = c^2$
\end{theorem}
\begin{corollary}<3->
$x + y = y + x$
\end{corollary}
\begin{proof}<2->
$\omega +\phi = \epsilon$
\end{proof}
\end{frame}


Notice that with environments we put the overlay specification after the curly brackets instead of before.

You will also notice that turning the transparency setting on earlier in the document has affected the overlays here.

## Overlays and Tables

Finally we may want to animate a table so that the rows appear slide by slide. To do this we use the \onslide command like this. We also need to make sure we reset the \setbeamercovered{} command to the default for this to work:

\setbeamercovered{invisible}
\begin{frame}
\frametitle{Tables}
\begin{table}
\begin{tabular}{l | c | c | c | c }
Competitor Name & Swim & Cycle & Run & Total \\
\hline \hline
John T & 13:04 & 24:15 & 18:34 & 55:53 \onslide<2-> \\
Norman P & 8:00 & 22:45 & 23:02 & 53:47 \onslide<3->\\
Alex K & 14:00 & 28:00 & n/a & n/a \onslide<4->\\
Sarah H & 9:22 & 21:10 & 24:03 & 54:35
\end{tabular}
\caption{Triathlon results}
\end{table}
\end{frame}


This concludes our discussion on animating our presentation. In the next post we’ll look at the different themes available in beamer and we’ll look at printing handouts. Please do keep in touch with us via Facebook, Twitter & Google+.

pt 1 - Getting Started

pt 2 - Lists, Columns, Pictures, Descriptions & Tables

pt 3 - Blocks, Code, Hyperlinks & Buttons

pt 5 - Themes and Handouts

Posted by Josh Cassidy on 20 Aug 2013