Reader small image

You're reading from  LaTeX Graphics with TikZ

Product typeBook
Published inJun 2023
PublisherPackt
ISBN-139781804618233
Edition1st Edition
Tools
Right arrow
Author (1)
Stefan Kottwitz
Stefan Kottwitz
author image
Stefan Kottwitz

Stefan Kottwitz studied mathematics in Jena and Hamburg. He works as a network and IT security engineer both for Lufthansa Industry Solutions and for Eurowings Aviation. For many years, he has been providing LaTeX support on online forums. He maintains the web forums LaTeX and goLaTeX and the Q&A sites TeXwelt and TeXnique. He runs the TeX graphics gallery sites TeXample, TikZ, and PGFplots, the TeXlive online compiler, the TeXdoc service, and the CTAN software mirror. He is a moderator of the TeX Stack Exchange site and matheplanet. He publishes ideas and news from the TeX world on his blogs LaTeX and TeX. Before this book, he authored the first edition of LaTeX Beginner's Guide in 2011, and LaTeX Cookbook in 2015, both published by Packt.
Read more about Stefan Kottwitz

Right arrow

Drawing Trees and Graphs

With nodes, edges, and styles, we can already build complex pictures. To make our work easier, TikZ provides libraries with support for various common types of graphics.

Each library provides specific drawing commands and corresponding style options.

In this chapter, you will learn to work with TikZ libraries for the following topics:

  • Drawing trees
  • Creating mind maps
  • Producing graphs
  • Positioning in a matrix

By the end of the chapter, you will be able to efficiently produce such types of graphics.

Technical requirements

Apart from your local LaTeX installation, you can work with the code at https://tikz.org/chapter-06 or use Overleaf for online compiling.

The code examples are available on GitHub at https://github.com/PacktPublishing/LaTeX-graphics-with-TikZ/tree/main/06-drawing-trees-graphs-charts.

In this chapter, we will use the trees, graphs, matrix, and quotes libraries. Furthermore, we will use the hvlogos package for printing LaTeX-related logos.

Drawing trees

Trees are a specific kind of graph where any two vertices are connected by exactly one path, which consists of edges. We may encounter them in mathematics, such as in graph theory, in computer science, or in any drawing that illustrates parent-children relationships in a hierarchical way.

We already know the node and edge operations, and the child operation combines both of them. Specifically, it connects a parent node and one or more child nodes by edges, each from the parent to a child. Moreover, TikZ counts the children and determines an appropriate positioning for each of them.

A very simple example is this:

\node {A} child { node {1} edge from parent };

The output is this very simple tree:

Figure 6.1 – A simple tree

Figure 6.1 – A simple tree

edge from parent is a special path operation that adds an edge from the parent to the child. We can add options and nodes to this edge, as we know from Chapter 4, Drawing Edges and Arrows. For example,...

Creating mind maps

We already know about trees for organizing information hierarchically, usually growing top-down or left-to-right, with several levels. Imagine a tree with a root in the center, and the first level of children is circular around it. Each child again is surrounded by children of the next level.

Such a diagram is called a mind map and is famous for visualizing ideas. We have a central concept, from which child concepts branch out in various directions. Each child concept can again have children.

TikZ provides the mindmap library, which can display a root concept as a circle in the center and child concepts as smaller circles around it, connected by branches, which are edges.

Load it with \usetikzlibrary{mindmap}. Then, add the mindmap option to the tikzpicture environment and build a tree with children, like in the previous section. Just the nodes will have the concept style. That includes a concept color value that you can set.

But let’s take small...

Producing graphs

The syntax with child nodes and edges can feel lengthy, and having many curly braces may lead to small mistakes. TikZ provides a special syntax for graphs that is very concise.

To be able to use it, we have to load the graphs library with this command:

\usetikzlibrary{graphs}

This gives us a new command called \graph, which generates even complex graphs with short specifications. Here’s a quick example of how it looks, representing a classic LaTeX compiling process:

\begin{tikzpicture}[ nodes = {text depth = 1ex,
    text height = 2ex}]
  \graph { tex -> dvi -> ps -> pdf };
\end{tikzpicture}

That highlighted \graph command produces this image:

Figure 6.16 – A simple graph

Figure 6.16 – A simple graph

Note that we specified a text depth and height for all nodes because with the letter p in the node text, which extends below the baseline, the nodes would have different dimensions and would not be properly...

Positioning in a matrix

Diagrams and drawings, in general, often have a rectangular structure, with elements or text arranged vertically and horizontally, like on a grid. TikZ offers a matrix node style for such a placement. Here is a very simple example:

\node [matrix, draw] {
  \node{A}; & \node{B}; & \node{C}; \\
  \node{D}; & \node{E}; & \node{F}; \\
};

This gives us the following rectangular node, with nodes placed in a matrix grid:

Figure 6.21 – A simple matrix node

Figure 6.21 – A simple matrix node

The syntax is similar to LaTeX’s array and tabular environments: columns are separated by &, and rows end with \\. Also note that the last row must end with \\.

Each cell can contain a node or a small drawing, or it can be left empty. TikZ adjusts the size of the cells automatically, so it fits the content.

Since the matrix is a node, you can apply what you already know and add shape and style options to the node in square...

Summary

In this chapter, you learned about visualizing information in hierarchical structures such as trees, and you learned new short syntax elements for creating graphs and diagrams. Combine this with all you learned about nodes, edges, and styles, and you have become a master at drawing diagrams.

In the next chapter, we will learn more about advanced drawing techniques.

Further reading

The full reference for trees and graphs is the TikZ manual at https://texdoc.org/pkg/tikz in PDF format.

These sections are the most relevant for this chapter:

  • Part III, Section 21, Making Trees Grow, is the reference for all basics. The direct online link is https://tikz.dev/tikz-trees. Part V, Section 76, Tree Library, describes the tree library in depth. The online manual link is https://tikz.dev/library-trees.
  • Part III, Section 19, Specifying Graphs, is the graphs library reference and is available online at https://tikz.dev/tikz-graphs. Part IV, Graph Drawing, is a comprehensive part dedicated to algorithmic graph drawing, where TikZ computes the layout for you, available at https://tikz.dev/gd.
  • Part III, Section 20, Matrices and Alignment, explains the basic matrix features; its link is https://tikz.dev/tikz-matrices. Part V, Section 59, Matrix Library, is the reference for the additional styles, such as matrix of nodes, and is found at https...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
LaTeX Graphics with TikZ
Published in: Jun 2023Publisher: PacktISBN-13: 9781804618233
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Stefan Kottwitz

Stefan Kottwitz studied mathematics in Jena and Hamburg. He works as a network and IT security engineer both for Lufthansa Industry Solutions and for Eurowings Aviation. For many years, he has been providing LaTeX support on online forums. He maintains the web forums LaTeX and goLaTeX and the Q&A sites TeXwelt and TeXnique. He runs the TeX graphics gallery sites TeXample, TikZ, and PGFplots, the TeXlive online compiler, the TeXdoc service, and the CTAN software mirror. He is a moderator of the TeX Stack Exchange site and matheplanet. He publishes ideas and news from the TeX world on his blogs LaTeX and TeX. Before this book, he authored the first edition of LaTeX Beginner's Guide in 2011, and LaTeX Cookbook in 2015, both published by Packt.
Read more about Stefan Kottwitz