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

Using Layers,
Overlays, and Transparency

Every TikZ picture is a sequence of drawing commands. As with LaTeX documents, TikZ drawings work linearly. Naturally, we can draw over existing graphic elements; they would overlap, and the most recent drawing action would come on top. We may want something other than overprinting, so we will look at a solution in this chapter.

Until now, we have been drawing on a single canvas. Did you know we can have multiple canvases stacked on each other, such as a background canvas, a regular canvas, and a foreground canvas? These are called layers of an image.

In this chapter, you will learn how to utilize layers. Furthermore, you will learn to use see-through paths, such as transparent areas, so that you can see what is behind them.

These are our main topics:

  • Using transparency
  • Drawing on background and foreground layers
  • Overlaying LaTeX content with TikZ drawings
  • Positioning pictures on the background of a page

By...

Technical requirements

The source code of the chapter’s examples is available at https://tikz.org/chapter-09.

The code can be downloaded from GitHub at https://github.com/PacktPublishing/LaTeX-graphics-with-TikZ/tree/main/09-using-layers.

In this chapter, we will use the backgrounds, matrix, quotes, positioning, fit, and decorations.pathmorphing libraries, and the tikzducks, tikzmark, amsmath, blindtext, and atbegshi packages.

Using transparency

When you draw anything new that overlaps with previously drawn objects, it simply paints over it so that you cannot see what’s behind it anymore. The PDF standard supports partially transparent colors that allow seeing what’s behind them.

TikZ provides a simple interface: you can decide how transparent or opaque the colors of an object or path will be by specifying an opacity value between 0 and 1. Here, 0 means utterly opaque without transparency, and 1 means entirely transparent, like invisible.

A code is worth a thousand words, so let’s have an example. We will draw water, which is naturally transparent. And we’ll draw a duck, which is naturally in the water.

To have some waves in the water, we load the decorations.pathmorphing library that we used in the previous chapter:

\usetikzlibrary{decorations.pathmorphing}

TikZ has libraries and packages for everything useful and much silly stuff, so of course, there’s...

Drawing on background and foreground layers

When a drawing command overprints another object, and we don’t want this, we can usually arrange the order of commands. However, it’s not always possible. Consider our matrix example from the previous section: We had to draw the highlighting for the submatrices after the matrices because we used their cell coordinates as reference coordinates. We cannot change the drawing order here. Without transparency, the numbers in the cells would be overprinted by the yellow rectangle.

Now, we get to another solution for the overprinting problem: we use layers. Specifically, we use the background layer. We will apply it to our matrix example.

First, load the backgrounds library in the document preamble:

\usetikzlibrary{backgrounds}

Now, we put the nodes, which we created in the previous section, into a scope environment with the on background layer option:

\begin{scope}[on background layer]
  \node (m1) [submatrix...

Overlaying LaTeX content with TikZ drawings

Our matrix example was fine for creating a diagram. However, mathematicians will typeset matrices using amsmath matrix environments in LaTeX’s math mode.

In this section, we will explore how to draw within such text and math content created outside TikZ. There will be quite some code lines again; remember that you can view the complete example code at TikZ.org and on GitHub.

We will recreate our matrix example with standard LaTeX tools. First, we have to load the amsmath package. Then, in the document, we write an equation with pmatrix environments and a long extensible arrow. That’s straightforward LaTeXing. One thing will be new here: whenever we want to remember a current position, we insert the \tikzmark{x} command, where x stands for a coordinate name we choose. Let’s go:

\[
  \begin{pmatrix}
       1 & 2 & 3 \\
       ...

Positioning pictures on the background of a page

A regular TikZ picture appears right where we put it in the document text. We can use a center environment for centering it and a figure environment to have a caption and a reference label and to let it float to a suitable place in the document for better page breaks.

We can even put a TikZ picture anywhere on a page without affecting the document text, placing it in the background. As we already know, we can use the overlay option so that it doesn’t block any space. The remember picture option from the previous section is even more important here: once you set this, TikZ stores picture positioning information in the .aux file. In the next compiler run, that information is read from the .aux file and used for final positioning. That’s the reason why we need to compile a second time to have the final positioning.

We can consider the page like a node with a rectangular shape, having precisely the page dimensions. TikZ...

Summary

Having worked through this chapter, you now master overlapping situations and control the order of drawing commands in your source code versus the order of appearance in the output image.

Now, you can draw see-through content using transparent colors and draw on the background of pictures behind other objects without overprinting them.

You learned to utilize the tikzmark library, which is a very clever and helpful tool for tweaking your LaTeX articles, books, or presentation slides with TikZ drawing tools.

You can now draw on absolute positions on a page behind the regular LaTeX document text. In the next chapter, you will learn to calculate with coordinates, which helps a lot in positioning.

Further reading

The following sections in the TikZ manual at https://texdoc.org/pkg/tikz are relevant to this chapter:

The TikZ galleries (https://tikz.net and https://texample.net/tikz/examples) contain many examples using features of this chapter; those collections have layers, background, fadings, and transparency tags.

The tikzmark manual can be found at https://texdoc.org/pkg/tikzmark. The atbegshi manual is at https://texdoc.org/pkg/atbegshi.

The transpose of a matrix, for which we made...

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