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

Decorating Paths

So far, we have created our drawings with paths consisting of straight or bent lines, various shapes, and text. TikZ offers us tools to make paths fancier. With a few options, we can change the path so that it has a bumpy or wavy appearance or one with a zigzag line. We can add markers such as arrows to paths and let the text flow along paths.

In this chapter, we will work on the following topics:

  • Pre- and post-actions for using a path multiple times
  • Understanding decorations
  • Variations of decoration commands, options, and libraries
  • Exploring the available decoration types
  • Nesting decorations
  • Adjusting decorations

Once you’ve learned the tools of this chapter, you will be able to add fancy details to your drawings and know how to add braces, markers, and curvy text to scientific illustrations.

Technical requirements

All the code examples are available at https://tikz.org/chapter-08. You can download the code from GitHub at https://github.com/PacktPublishing/LaTeX-graphics-with-TikZ/tree/main/08-decorating-paths.

In this chapter, we will use the decorations library. Specifically, we will load the decorations.pathmorphing, decorations.pathreplacing, decorations.text, decorations.markings, decorations.shapes, and decorations.fractals sub-libraries.

We will see a lot of examples in this chapter. Therefore, we will often show just code snippets or single commands. To see complete examples and to run the code, you can visit this chapter’s page at TikZ.org.

Pre- and post-actions for using a path multiple times

We can have an arbitrarily complex path we want to use several times. TikZ provides an efficient way that spares us repeating coordinates.

Let’s look at the following path from Figure 7.4 from the previous chapter:

\draw[orange, line width=3mm]
  (90:2) -- (210:2) -- (330:2) -- cycle;

This draws an orange triangle:

Figure 8.1 – A simple triangle path

Figure 8.1 – A simple triangle path

Let’s say we want to draw this path several times with different colors and line widths to get a nice striped effect. The straightforward way to do this is by repeating the \draw command, like so:

\draw[red, line width=5mm]
  (90:2) -- (210:2) -- (330:2) -- cycle;
\draw[orange, line width=3mm]
  (90:2) -- (210:2) -- (330:2) -- cycle;
\draw[yellow, line width=1mm]
  (90:2) -- (210:2) -- (330:2) -- cycle;

Those three commands give us the following triangle in a mix of red, orange...

Understanding decorations

The TikZ syntax can be pretty verbose. That’s particularly the case with decorations. Even adding arrow tips along a path can be wordy. As this is an excellent example of what TikZ decorations can do for us, let’s try this.

Let’s create an arrow from (0,0) to (2,0). We can do this with the following code:

\draw[-stealth] (0,0) -- (2,0);

We have an arrow tip at the end of the path, but we also want to have arrow tips along the way. First, we must load the decorations.markings library:

\usetikzlibrary{decorations.markings}

Then, we must choose the decorate option as postaction, with a decoration type of markings, in steps of 0.2 between positions 0.2 and 1:

\draw[-stealth, postaction = decorate,
  decoration = {markings,
  mark = between positions 0.2 and 1 step 0.2
  with {\arrow{stealth}}}]
    (0,0) -- (2,0);

The syntax will be explained in detail later in this chapter...

Exploring the available decoration types

TikZ has decorations that change a path, also called morphing a path. This will be our next topic.

Other decorations replace a path with symbols; we will see them later in this section.

To summarize and compare, we will provide some figures containing several drawings or paths. The figure caption will include the options used for each path, always in order from top to bottom. You can find the code for each figure on GitHub and on TikZ.org on the page for this chapter.

Morphing paths

Morphing means modifying a path to become, for example, a zigzag or jagged line. We will distinguish between linear and curvy morphing. We have to load the corresponding library in the preamble by using \usetikzlibrary{decorations.pathmorphing}.

The decorations of this library have optional values, such as these:

  • amplitude: Determines how much the changed path goes above and below the original path
  • segment length: The length of such a decoration...

Adjusting decorations

If a decoration doesn’t fit perfectly, there are options to adjust it. You can raise it by a positive or negative dimension using the raise option, such as in decoration = {brace, raise=5pt}.

The mirror option mirrors the decoration along the path. For example, decoration = {brace, mirror} would put the brace on the other side of the path.

We can also start a decoration later or end it earlier. These options keep a straight line of 5mm until the decoration starts:

pre=lineto, pre length = 5mm

If you have a curvy path, such as with arcs, you may prefer this:

pre=curveto, pre length = 5mm

Using the post option instead of pre, and post length, will do the same for the end of the path.

If you look closely at Figure 8.7 and Figure 8.9, you will see that I used such pre and post options for the path at the top so that it looks better and more symmetric. You can see this in the code on this chapter’s page at TikZ.org. This is the bump...

Summary

In this chapter, you learned about repeated effects on paths. Apart from adding decorative effects, you can use ticks, braces, and arrows in scientific drawings, such as for mathematics or physics, or zigzags and coils in mechanical engineering.

You also learned how to work with pre- and post-actions on paths to draw something before or after a particular path. In the next chapter, we will explore such a concept for the whole picture: we will draw on the background and the foreground of pictures. Furthermore, we will use transparency to be able to see what is in the background.

Further reading

The TikZ manual at https://texdoc.org/pkg/tikz provides additional information on the topics that were covered in this chapter:

At https://tikz.net/tag/decorations and https://texample.net/tikz/examples/feature/decorations, you can learn how decorations are beneficial for drawings in science, such as mathematics, mechanics, optics, and electrical engineering.

The Koch snowflake is explained at https://en.wikipedia.org/wiki/Koch_snowflake.

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