Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
LaTeX Graphics with TikZ

You're reading from  LaTeX Graphics with TikZ

Product type Book
Published in Jun 2023
Publisher Packt
ISBN-13 9781804618233
Pages 304 pages
Edition 1st Edition
Languages
Author (1):
Stefan Kottwitz Stefan Kottwitz
Profile icon Stefan Kottwitz

Table of Contents (18) Chapters

Preface 1. Chapter 1: Getting Started with TikZ 2. Chapter 2: Creating the First TikZ Images 3. Chapter 3: Drawing and Positioning Nodes 4. Chapter 4: Drawing Edges and Arrows 5. Chapter 5: Using Styles and Pics 6. Chapter 6: Drawing Trees and Graphs 7. Chapter 7: Filling, Clipping, and Shading 8. Chapter 8: Decorating Paths 9. Chapter 9: Using Layers, Overlays, and Transparency 10. Chapter 10: Calculating with Coordinates and Paths 11. Chapter 11: Transforming Coordinates and Canvas 12. Chapter 12: Drawing Smooth Curves 13. Chapter 13: Plotting in 2D and 3D 14. Chapter 14: Drawing Diagrams 15. Chapter 15: Having Fun with TikZ 16. Index 17. Other Books You May Enjoy

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 2023 Publisher: Packt ISBN-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.
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}