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

Filling, Clipping, and Shading

Now, we enter the book’s second part and start with advanced drawing techniques.

The first chapters explained drawing paths, geometric objects, and path elements such as nodes and edges. This chapter will focus on areas enclosed by a path for filling and clipping.

Specifically, we will deal with the following:

  • Filling an area
  • Understanding a path interior
  • Clipping a drawing
  • Reverse clipping
  • Shading an area

At the end of the chapter, you will be proficient in coloring and clipping and know two different ways to define areas with self-intersecting paths and unconnected path segments.

Technical requirements

You can work with the code at https://tikz.org/chapter-07 or use Overleaf apart from your local LaTeX setup with a complete TikZ package.

The code examples are available on GitHub at https://github.com/PacktPublishing/LaTeX-graphics-with-TikZ/tree/main/07-filling-clipping-shading.

In this chapter, we will use the shadings library.

We will use many colors in this chapter; the paper version of the book is grayscale. Therefore, it’s highly recommended to read the e-book version in PDF or Kindle format or read the TikZ.org chapter page to see the images in their full glory.

Filling an area

We already used the fill option in previous chapters. Now, we will take a closer look at filling. Until now, we filled node shapes and simple geometric areas, for example, with \node[fill] and \draw[fill]. There are command aliases:

  • \fill is equivalent to \path[fill]; we use it for filling without drawing a border
  • \filldraw is equivalent to \path[draw,fill] and \draw[fill]; in this case, we will get a border

Both commands take options for colors, such as \fill[yellow] for yellow filling and
\filldraw[fill=yellow,draw=red] for a yellow-filled area with a red border.

If a path encloses an area, TikZ closes it by connecting the last coordinate with the first coordinate, and then it fills it. Of course, it’s good to close the path ourselves by ending with the same coordinate as the path start coordinate. We can do that with a short generic statement by adding -- cycle to the path code, which means connecting finally to the start point. We will...

Understanding the path interior

Paths can be more complex than just a circle or convex polygon. Take a quick look at these three examples:

Figure 7.1 – Various paths

Figure 7.1 – Various paths

In Figure 7.1, we have different questions regarding how to color areas, such as the following:

  • In the triangle, do we consider the inner triangle as inside or outside? Can we fill the complete big triangle or just the space between them?
  • Can we color the full star shape or just the spikes?
  • Can we color small segments of that multiple-circle path?

This section will help us to answer these questions. Regarding the third point, the following section, Clipping a drawing, will provide us with a tool.

For different area selecting and coloring, TikZ implements two different interior rules, used in computer graphics. We will discuss them both now.

The nonzero rule

Let’s say we have a closed path. Naturally, that path has a direction from start to end...

Clipping a drawing

Clipping means cutting pieces from a drawing or a path. In other words, it means restricting a picture to a specific area, called the clipping area or clipping path. The clipping area can be a rectangle, a circle, or an arbitrary path.

First, an easy example. Let’s cut the corners of the filled triangle from Figure 7.6. A circle like this will clip it:

Figure 7.9 – A circle for clipping a triangle

Figure 7.9 – A circle for clipping a triangle

First, we define the clipping path:

\clip (0,0) circle (1.5);

Then, we proceed with our drawing:

\fill[orange] (90:2) -- (210:2) -- (330:2) -- cycle
              (90:1) -- (330:1) -- (210:1) -- cycle;

The result, as expected, is the following image:

Figure 7.10 – A clipped triangle

Figure 7.10 – A clipped triangle

Similar to fill, we can use clip as a command and an option:

  • \clip is equivalent to \path[clip]; we can use it to declare...

Reverse clipping

Sometimes, we may want the opposite of clipping – instead of cutting away everything that’s not in our clipping area, we would only cut out that part inside a particular area.

First, let’s look at regular clipping again. This time, we will color a different segment of our pair of intersecting rings from Figure 7.11, as shown in the following figure:

Figure 7.16 – Another segment to fill

Figure 7.16 – Another segment to fill

The code is a straightforward exercise of what we already know. We will choose the smaller circle on the left as a clipping path and draw the right ring. The code is as follows:

\begin{tikzpicture}[even odd rule]
  \clip (-1,0) circle (1.2);
  \fill[orange] (1,0) circle (1.2) (1,0) circle (2);
\end{tikzpicture}

Now to our challenge, coloring the other part of the right ring, as follows:

Figure 7.17 – Another segment to fill

Figure 7.17 – Another segment to fill

To also practice limiting the clipping...

Shading an area

Instead of filling an area with a single color, we can use several colors with a transition between them. TikZ provides several kinds of smooth transitions in different ways with the shade action.

Similar to fill, shade can be used as a command and an option:

  • \shade is equivalent to \path[shade]; use it for shading without drawing a border
  • \shadedraw is the same as \path[draw, shade] and \draw[shade]; it produces the shading and adds a border

We will look at several shading styles in the following few sections. The first three, axis, radial, and ball, are included with TikZ by default. To use the other shadings, load the corresponding library in your preamble with \usetikzlibrary{shadings}.

Often, you don’t need to choose a style explicitly. Depending on your color options, TikZ can automatically determine one, so it’s pretty intuitive.

We will look at examples with some randomly chosen colors.

Axis shading

Axis shading...

Summary

This chapter was challenging, but now that you have completed it, you know how to define and restrict areas for filling in your drawings with a color or interpolated colors.

In the next chapter, our drawings will not get more accessible but more advanced, and they will also get fancier.

Further reading

In the TikZ manual, available at https://texdoc.org/pkg/tikz in PDF format, these sections are relevant:

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}