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 and Positioning Nodes

Text elements of TikZ pictures are called nodes. This feature gives you excellent control over placing and arranging text in graphics, and you can combine it with additional drawing elements.

In this chapter, you will learn how to draw nodes with various shapes containing text and how to position them.

We will deal with the following topics:

  • Understanding nodes
  • Using shapes and anchors
  • Positioning and aligning nodes
  • Adding labels and pins
  • Spacing within and around nodes
  • Putting images into nodes

By the end of this chapter, you will be ready to draw your first images with text elements.

Technical requirements

You need either a local LaTeX installation on your PC or an online compiler such as Overleaf or the book’s website compiler. You can find all code examples for this chapter at https://tikz.net/contents/chapter-03.

The code is also available on GitHub at https://github.com/PacktPublishing/LaTeX-graphics-with-TikZ/tree/main/03-drawing-and-positioning-nodes.

Again, we sometimes just show code snippets to not spend too much book space on repetitive commands, such as \begin{tikzpicture} and \end{tikzpicture}. Every code snippet is available as a fully compilable document at TikZ.org and GitHub; you can use those code examples for exercises.

In this chapter, we will use the shapes and positioning libraries included in TikZ and the tikzpeople and enumitem packages.

Understanding nodes

In TikZ, a node is a piece of text that can have a specific shape. By default, nodes have a rectangular shape, but we can choose between many other shapes, such as circles, ellipses, polygons, stars, clouds, and many more. Using shapes other than rectangles and circles requires loading the shapes library. So, from now on, we will add this line to our TikZ documents:

\usetikzlibrary{shapes}

Let’s start with elementary examples. We can place a simple piece of text on the coordinates x=4 and y=2 with the following command:

\draw (4,2) node {TikZ};

It gives us just the word TikZ at the position (4,2). When we want TikZ to also draw the border, we add the draw option to the node:

\draw (4,2) node[draw] {TikZ};

We can choose a border color, fill it with a color, and choose a text color, for example:

\draw (4,2) node[draw, color=red, fill=yellow, text=blue] {TikZ};

What started as simple text now looks like this:

Figure 3.1 – A node with colors
...

Using shapes and anchors

While rectangle and circle node shapes are available by default, others require loading the shapes package, as we did in the previous section.

We will explore many of them now.

A rectangle shape

A rectangle node has anchors in all compass directions, as we can see here, with a node named (n):

Figure 3.6 – Rectangle shape with anchors

Figure 3.6 – Rectangle shape with anchors

In addition to these, we have a few more anchors available:

  • center: The middle of the node, which is the default anchor.
  • base: At the baseline of the node text and centered horizontally. It is helpful for the vertical alignment of text nodes. The base west and base east anchors are at the baseline height and on the west and east sides, respectively.
  • text: At the left of the text baseline.
  • mid: At half-height of the lower x and centered horizontally. It is also helpful for vertically aligning nodes with text that may have different heights and depths. Also, here,...

Spacing within and around nodes

We saw that rectangular node borders just fit nicely around the text. To understand how a circular node border fits around the node text: imagine a rectangle node for this text, and then the circle node border circumscribes that rectangle.

You can set a node option called inner sep to get more or less distance between the node text and border. To get more spacing around the border so the anchors are farther away, you can set an optional value called outer sep. It’s written in the following way:

\node[draw,rectangle,inner sep=1cm,outer sep=1cm] {n};

It is better to see it in a picture, so take a look with a default spacing node next to the n node:

Figure 3.10 – Spacing within and around a node

Figure 3.10 – Spacing within and around a node

We can set horizontal (x) and vertical (y) distance separately; they are called xsep and ysep. With example values of 1cm and 0.5cm, the code changes to the following:

\node[draw,rectangle, inner xsep=1cm,inner...

Positioning and aligning nodes

We have already learned how to place nodes at coordinates and use anchors for that. Let’s explore more options.

Using anchors and relative positioning

First, perhaps you noticed that positioning based on anchors can feel counterintuitive: to place a node above an object (north of it), we use the south anchor.

For example, here we draw a node above a circle:

\draw circle [fill, radius=2pt] node [anchor=south] {text};

The output of that command is shown in the following picture:

Figure 3.13 – A node above a circle

Figure 3.13 – A node above a circle

For more intuitive positioning, TikZ offers other statements. We can write the same line in this way:

\draw circle [fill, radius=2pt] node [above] {text};

That gives the same output as in Figure 3.13, and feels more natural.

In that spirit, these are the new relative positioning options:

  • above: Similar to anchor=south
  • below: Similar to anchor=north
  • left: Similar...

Adding labels and pins

We can add labels to nodes with a handy syntax that looks like this:

\node[label=direction:text] at (coordinate) {text};

Note that if we don’t specify a coordinate value, the node will be at the current position in the path. Paths begin at the origin (0,0) by default if no coordinate value is specified. Knowing this, we will omit the coordinate value in the following examples, so our nodes will be at (0,0).

Again, it’s good to see it in a picture. Let’s have a ball node with labels, where every label is scaled down by two.

For this, we will first have a brief look at the style syntax, as it’s already convenient here. Until now, we set the key=value pairs as options for nodes or other elements. To not repeat ourselves, we can set these options for all elements in a drawing by using a single option on the tikzpicture environment:

\begin{tikzpicture}[every node/.style={key=value}]

The dot is part of the syntax we thoroughly...

Putting images into nodes

We all know about fancy Visio and PowerPoint diagrams. In these, we have fancy node shapes, which are called icons or stencils, with many of them available.

While TikZ gives us a library of various shapes that we can customize, we can even use arbitrary images as nodes that we combine with a shape.

I’m working as a network engineer and producing complex network diagrams in my field of work. So, I will describe my approach.

Renowned hardware manufacturers, such as Cisco and Hewlett Packard often provide icon and stencil libraries for use with Visio, PowerPoint, Inkscape, or any drawing program. We can use the same in TikZ. So, we can go to a vendor download page, such as https://www.cisco.com/c/en/us/about/brand-center/network-topology-icons.html. There we can find image collections in various formats, such as .vss for Visio, .pptx for PowerPoint, .jpg for general use, and .eps in Encapsulated PostScript format (EPS).

The best choice here...

Summary

Now, we have learned about the concept of nodes in TikZ. Using nodes, we can now add text to our drawings with complete control over its placement and alignment. Furthermore, we learned how to put shapes around our text and how to use external images within TikZ nodes.

Nodes will be the building blocks of your TikZ diagrams and drawings, so mastering this chapter was an important step forward.

In the next chapter, we will see how to connect nodes with edges and arrows.

Further reading

The TikZ manual explains nodes in Part III, Section 17, Nodes and Edges. You can open the manual by typing texdoc tikz at the command prompt or online at https://texdoc.org/pkg/tikz as a PDF document. You can read that chapter in an online HTML version of the manual at https://tikz.dev/tikz-shapes.

Furthermore, the TikZ manual has a comprehensive reference of shapes in Part V, Section 71, Shape Library, also online at https://tikz.dev/library-shapes. It shows all shapes with anchors and many customizations.

The tikzpeople package documentation is available at https://texdoc.org/pkg/tikzpeople.

The home page for the epstopdf tool is https://tug.org/epstopdf/; there, you can find links to download it and documentation.

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