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 Edges and Arrows

In the previous chapter, we learned how to produce shapes with text called nodes. In this chapter, you will learn how to draw lines with text, called edges, to complete your knowledge about placing text in diagrams.

We will also discuss customizing edges, texts, and arrows.

In this chapter, we will cover the following key topics:

  • Connecting nodes by edges
  • Adding text to edges
  • Diving deeper into edge options
  • Drawing arrows
  • Using the to operation

By the end of this chapter, you will be able to draw colorful diagrams with text, arrows, and edge labels.

Technical requirements

You need to have a LaTeX installation, including TikZ, or you can compile the example code online with Overleaf or at https://tikz.org/chapter-04.

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

From time to time, you will see code snippets for an explanation. You can take the corresponding complete source code from TikZ.org or GitHub if you want to run such a snippet.

In this chapter, we will use the following TikZ libraries: positioning, quote, and arrows.meta. Furthermore, we will use the topaths library, which is loaded automatically by TikZ, so we don’t have to load it ourselves.

Connecting nodes by edges

In the previous chapter, we used lines with arrows to create the small diagram in Figure 3.3. In more complex diagrams, text labels on such lines and arrows are often desired. Such “smart” lines connecting nodes are called edges.

We will start with the first small example. We aim to illustrate the compiling process from a LaTeX .tex file to a PDF file. We will also add some colors to get more familiar with styling nodes.

  1. Start with this small LaTeX document, which loads TikZ and the positioning library and contains an empty tikzpicture environment for now:
    \documentclass[border=10pt]{standalone}
    \usepackage{tikz}
    \usetikzlibrary{positioning}
    \begin{document}
    \begin{tikzpicture}
    \end{tikzpicture}
    \end{document}
  2. In the tikzpicture environment, create a node, which we call tex, filled with orange color and white text:
    \node (tex) [fill=orange, text=white] {TEX};
  3. Put a second node on the right of the tex node, call it pdf, and fill it...

Adding text to edges

In diagrams, we often see that apart from text in diagram nodes, we can have text on the connecting lines or arrows. That’s an essential feature of TikZ’s edge operation.

Let’s continue our example from the previous section and add a text label to the edge. It will read pdflatex in a tiny typewriter font printed above the edge. This label is itself a node, so we insert this right after the edge:

node[font=\tiny\ttfamily, above] {pdflatex}

The full command becomes as follows:

\draw (tex) edge[->]
  node[font=\tiny\ttfamily, above] {pdflatex} (pdf);

Compile, and you get this picture:

Figure 4.2 – An edge with a text label

Figure 4.2 – An edge with a text label

Admittedly, this is a pretty verbose syntax. Luckily, TikZ provides a shorter way; this is called the quotes syntax because you can add edge label texts by enclosing the text in quotes as an option to the edge. That’s basically edge["text"]. We...

Diving deeper into edge options

Every edge operation builds its own path; that’s why edge supports general path options. Furthermore, an edge can have connection-specific options, such as defining a straight line or a curve.

So, we can divide edge styling options into the following:

  • Path options, including general TikZ options
  • Connection options

Let’s look at them separately.

Path options

Remember, a path is a series of lines or curves with nodes and other elements. All options that you can set to a path, you can set to an edge, such as line style options.

These options can be as follows, using example values for clarity instead of syntax grammar formality:

  • color=red: This draws the edge in a red color
  • ultra thin, very thin, thin, semi thick, very thick, ultra thick: These options define the thickness of the edge line
  • line width=3pt: This denotes that the edge shall have a thickness of 3 pt
  • loosely dotted, dotted, densely...

Drawing arrows

We already saw how to add basic arrow tips to lines and edges: we added the -> option to get a right arrow tip. However, the default arrow tips look a bit small and thin. Let’s see how to modify them.

The general syntax is \draw[starttip-endtip] or edge[start arrow-endarrow]. Previously, we had just > as the end tip and no start tip.

Quick examples are provided in the following list:

  • ->: Right arrow, <-: Left arrow
  • <->: Left and right arrow
  • ->>: Double right arrow, <<-: Double left arrow
  • -Triangle: Triangle-shaped arrow
  • -Stealth: Stealth-plane-shaped arrow
  • -LaTeX: Black triangle arrow with slightly bent sides, like \vec in LaTeX picture mode

You can combine one or several tops at an arrow’s start and end.

When you use arrows, always load the arrows.meta package:

\usetikzlibrary{arrows.meta}

The old TikZ arrows library should not be used anymore. It still exists; that’...

Using the to operation

Remember, TikZ calls the instructions on a path an operation. Examples were the node and edge operations. What we draw with edge is not part of the main path. So, each edge can have its own appearance regarding styles or arrows, which makes it very flexible.

While the main focus of the current chapter is on the edge operation, there’s another similar and handy one: the to operation. You will see it in examples on the internet and in the documentation, so let’s also discuss it here, briefly at least.

The to operation can also be used to draw lines, curves, and arrows between nodes. to works with the current path options, such as color and arrow style. On the other hand, edge inherits the main path options but can take more path options in addition, such as its own color and arrow style. Suppose you don’t need that many different path options. In that case, you can stick with to, which still understands the connection-specific options...

Summary

In this chapter, you learned how to use edges to connect nodes and how to add text to them. You also extended your knowledge about using styles.

With what you now know about TikZ nodes, edges, and arrows, you can create complex diagrams with text elements, coloring, and further customizations.

In the next chapter, you will get a deeper understanding of TikZ styles to create more impressive drawings with less effort.

Further reading

The topics of this chapter are covered in depth in the following sections of the TikZ manual at https://texdoc.org/pkg/tikz:

  • Part III, Section 16, Arrows, has a complete reference of all options and kinds of arrow tips with tabular overviews of the look of arrow tips and customizations. The online manual link is found here: https://tikz.dev/tikz-arrows.
  • Part III, Section 17.12, Connecting Nodes: Using the Edge Operation, describes the edge and quotes syntax. It’s part of the online manual section at https://tikz.dev/tikz-shapes.
  • Part V, Section 74, To Path Library, explains the options for the to operation that can also be used with edge. You can read it online at https://tikz.dev/library-edges.

There are online resources worth taking a look at:

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 $15.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