×

To see the corresponding video for this blog post click here.

In this post we’re going to be looking at creating flowcharts in TikZ. To get started we need to load up the TikZ package, the ‘shapes.geometric’ TikZ library and the ‘arrows’ library.

\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}

The tikzstyle command

Now before we start the document we need to define the basic components of a flow chart. To do this we use the \tikzstyle command. First let’s define the block we’re going to use for start and stop blocks. We’ll name it ‘startstop’ using curly brackets immediately following the command, then we add an equals sign before a set of square brackets. In the square brackets we enter all the formatting information. For this block we’ll specify a rectangle with rounded corners. We’ll give it a minimum width of 3cm and a minimum height of 1cm. We’ll also ensure the text gets centred and we’ll set both a draw and a fill colour. In this example we’ve set the fill colour to a colour that is 30% red mixed with 70% white.

\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black, fill=red!30]

Next we’ll specify an input or output box. This time we want the block to be a parallelogram. To achieve this we ask for a trapezium and then alter the angles. The rest is very similar.

\tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=blue!30]

Next we’ll add a TikZ style for process blocks using a rectangle and a style for decision blocks using a diamond.

\tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=orange!30]
\tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=green!30]

Finally we’ll define a style for the arrows. For this we set the line thickness to ‘thick’, add an arrow head and specify the stealth arrow head.

\tikzstyle{arrow} = [thick,->,>=stealth]

Nodes

Now we are ready to start building our flow chart. To do the we use the ‘tikzpicture’ environment. We’ll create our flowchart blocks using nodes and the tikzstyles we defined earlier. Nodes are very powerful as we can easily position them, make them draw a shape, heavily format them and give them some text. In square brackets at the end of the begin command we specify a node distance of 2cm. This is so that the nodes we use to build the blocks are automatically spaced 2cm apart from their centres.

\begin{tikzpicture}[node distance=2cm]

<TikZ code>

\end{tikzpicture}

To add a node we use the \node command. We then add a label for the node in parenthesis. This label is how we refer to the node in the rest of the code. Then in square brackets we add the name of the tikzstyle we want the node to conform to, along with any other formatting options. Then in curly brackets we add the text we want to appear in the block before closing the statement with a semicolon.

\node (start) [startstop] {Start};

If we now compile the code you’ll see our start block has appeared as expected.

start

Now let’s add an input block in below the start block. This time we need to tell the node where to position itself. To do this we enter ‘below of’ followed by an equals sign and a node label into the square brackets. We could also use ‘above of’, ‘right of’ or ‘left of’ if we wanted the block to appear somewhere else. We’ll tell it to position itself below the start block.

\node (in1) [io, below of=start] {Input};

Now lets add in a process block and a decision block.

\node (pro1) [process, below of=in1] {Process 1};
\node (dec1) [decision, below of=pro1] {Decision 1};

If we compile the code you’ll notice that the gap between the green decision block and the orange process block isn’t as big as the other gaps.

gap

This is because the decision block, being a diamond, is taller than the other blocks. Therefore we can manually adjust its position using the ‘yshift’ variable. If we enter yshift=-0.5cm it will move the decision block vertically down by 0.5cm which should make the gap more regular.

\node (dec1) [decision, below of=pro1, yshift=-0.5cm] {Decision 1};

gap2

Now lets add in two process blocks coming out of the decision block, one below it and one to the right of it. Again we’ll need to alter the positioning using ‘yshift’ for the block below and ‘xshift’ for the block to the right. Let’s finish off adding the blocks by adding in an output block and a stop block.

\node (pro2a) [process, below of=dec1, yshift=-0.5cm] {Process 2a};
\node (pro2b) [process, right of=dec1, xshift=2cm] {Process 2b};
\node (out1) [io, below of=pro2a] {Output};
\node (stop) [startstop, below of=out1] {Stop};

noarrows

Arrows

To finish off our flowchart we need to add the arrows in. To draw an arrow we use the \draw command and then specify the tikzstyle we prepared for arrows using square brackets. We then enter the label of the node we want the arrow to start from, followed by two dashes and then the label corresponding to the node we want the arrow to terminate at. The labels need to be in parenthesis and we need to make sure we close the statement with a semicolon. Lets add arrows in between the start block and the input block, the input and process 1, process 1 and decision 1, decision 1 and process 2a and between decision 1 and process 2b.

\draw [arrow] (start) -- (in1);
\draw [arrow] (in1) -- (pro1);
\draw [arrow] (pro1) -- (dec1);
\draw [arrow] (dec1) -- (pro2a);
\draw [arrow] (dec1) -- (pro2b);

somearrows

As we have arrows coming out of a decision block we need to add some text to these two arrows. To do this we use more nodes, however this time we don’t need to use the \node command, we just type the word node in after the two dashes and then the text in curly brackets.

\draw [arrow] (dec1) -- node {yes} (pro2a);
\draw [arrow] (dec1) -- node {no} (pro2b);

If we now compile the code you’ll see the text has been added but not in a very helpful place.

badlabels

To fix this we specify which of the node’s anchors TikZ should use to fix the nodes to the lines. To do this we use square brackets immediately after the keyword ‘node’ and then enter ‘anchor=’ followed by the anchor. For the ‘yes’ node we’ll use the east anchor and for the ‘no’ node we’ll use the south anchor.

\draw [arrow] (dec1) -- node[anchor=east] {yes} (pro2a);
\draw [arrow] (dec1) -- node[anchor=south] {no} (pro2b);

goodlabels

Now let’s draw the final arrows in.

\draw [arrow] (pro2b) -- (pro1);
\draw [arrow] (pro2a) -- (out1);
\draw [arrow] (out1) -- (stop);

alllabels

You’ll also notice that the arrow from process 2b to process 1 is diagonal and therefore doesn’t look right. To improve this we can swap the first dash for a bar symbol which will make the arrow go in a vertical direction before going in a horizontal direction.

\draw [arrow] (pro2b) |- (pro1);

bar

Text Width

The final thing we should discuss is the text width. At the moment all our text fits nicely inside our shapes. However, if for example, we add some more text to process 2a, you’ll see the shape just extends horizontally until the text fits.

\node (pro2a) [process, below of=dec1, yshift=-0.5cm] {Process 2a text text text text text text text text text text};

2a

This now becomes a bit messy. To improve it we can specify the text width for these nodes by entering text width= followed by a length into our tikzstyles.

\tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, text width=3cm, draw=black, fill=orange!30]

textwidth

This concludes our post on creating flow charts with TikZ. If you want to play around with the flowchart we created in this post you can access it here. In the next post we’ll look at drawing electrical circuits. Please do keep in touch with us via Facebook, Twitter & Google+.

Basic Drawing Using TikZ

Generating TikZ Code from GeoGebra

Circuit Diagrams Using Circuitikz

Creating Mind Maps Using TikZ

Posted by Josh Cassidy on 29 Aug 2013

To see the corresponding video for this blog post click here.

In this video we’re going to look at using GeoGebra to generate TikZ code to use in our LaTeX documents. GeoGebra is a great tool for creating and displaying mathematical diagrams. You can get a copy of GeoGebra from the GeoGebra website, www.geogebra.org.

Let’s start by creating a simple diagram in GeoGebra. First we’ll add a circle using the ‘Circle with Centre through Point’ tool.

g1

Next we’ll add a polygon inside our circle using the ‘Polygon’ tool.

g2

Then we’ll measure an angle inside this polygon using the angle tool.

g3

Now we’ll add a straight line going through two points on the circle using the ‘Line through Two Points’ tool.

g4

We’ll finish up by turning the grid on. To do this we select the ‘Move’ tool, right click on the background and select the ‘Grid’ option.

g5

Now to export this as TikZ code we open the file menu, hover over ‘Export’ and click on ‘Graphics View as PGF/TikZ’.

export

We then tell GeoGebra how much of the grid we want included in our ‘tikzpicture’ by altering the x and y minimum and maximum points. You’ll see a blue box represent this area on the grid. Next we check the format is set to LaTeX article class and then click the generate button.

generate

Now if we hit ‘Copy to Clipboard’ we can then paste it into an empty tex file. You’ll notice that it has generated a preamble where it loads up the TikZ package and a TikZ library, it sets the page style to ‘empty’ and it also defines a new command:

\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\pagestyle{empty}
\newcommand{\degre}{\ensuremath{^\circ}}

Then it begins the document and defines some colours before opening a ‘tikzpicture’ environment:

\definecolor{qqwuqq}{rgb}{0,0.39,0}
\definecolor{zzttqq}{rgb}{0.6,0.2,0}
\definecolor{xdxdff}{rgb}{0.49,0.49,1}
\definecolor{qqqqff}{rgb}{0,0,1}
\definecolor{cqcqcq}{rgb}{0.75,0.75,0.75}

If we compile the code we’ll see it appear in the document. As it’s generated from TikZ code rather than an image, it’s very high quality.

compile

We could also turn the TikZ picture into a figure to give us more control over things like positioning. To do this we simply enclose the ‘tikzpicture’ environment in the ‘figure’ environment. We can then add a placement specifier, a caption and a label:

\begin{figure}[!h]

<tikzpicture environment>

\label{circle1}
\caption{TikZ from GeoGebra}
\end{figure}

Now if we want to include this figure in an existing document we can copy over everything in the figure environment. We also need to make sure we copy over the relevant parts of the preamble if they’re not in our existing docs preamble already. Therefore we’ll copy over the \usepackage command and \usetikzlibrary command as well as the \newcommand definition. Finally we also need to copy over the colour definitions.

figure

If we go back to GeoGebra we can alter the way our diagram looks by right clicking on it and changing the object properties. For example we can change colours, point styles, line styles and line thickness.

colour

Another useful thing we can do with GoeGebra is export to Tikz in a beamer format so that we can add diagrams into presentations. To do this we export like before except we select the ‘LaTeX (beamer class)’ option.

beamer

If we now copy this into a blank tex file and compile the document you’ll notice it has generated a number of slides which build up the diagram piece by piece:

b1 b2 b3

b4 b5 b6

b7 b8 b9

This is due to the \onslide overlay specifications that GeoGebra has automatically generated for us. For example this \onslide command tells beamer to show the circle from slide four onwards:

\onslide<4->{
  \draw [color=ffqqtt] (2,1) circle (2.82cm);
}

We can edit these overlay specifications to make the diagram come together in whatever way we want. For example we may want to make the circle appear on the same slide that point B appears. To do this we change this \onslide command from <4-> to <3-> and then, as the circle was the only item to appear on slide four, we knock the numbering of all overlay specifications above four down by one. Again if you want to add this frame into another beamer presentation you can just copy over all the relevant parts as we did with the document earlier.

This concludes our discussion on using GeoGebra to generate TikZ code. In the next post we’ll look at creating flow charts. Please do keep in touch with us via Facebook, Twitter & Google+.

Basic Drawing Using TikZ

Creating Flowcharts with TikZ

Circuit Diagrams Using Circuitikz

Creating Mind Maps Using TikZ

Posted by Josh Cassidy on 28 Aug 2013

To see the corresponding video for this blog post click here.

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

line

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

square

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

parabola

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

curve

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

circle

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

ellipse

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

arc

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

redthickdashed

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

grid1

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

grid2

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

fill

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

filldraw

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

shade1

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

shade2

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

shade3

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

shadedraw

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

plainarrows

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};

nodes

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$};

foreach

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