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 Styles and Pics

In the previous two chapters, we learned about styles and used them with nodes, edges, labels, and pins. Now, we will take a closer look at styles and how to use them efficiently. Furthermore, we will deal with mini TikZ pictures that can be used as building blocks within a drawing.

Our main topics are the following:

  • Understanding styles
  • Defining and using styles
  • Inheriting styles
  • Using styles globally and locally
  • Giving arguments to styles
  • Creating and using pics

By the end of this chapter, you will know how to draw pictures more efficiently without repetitive syntax.

Technical requirements

As with every chapter, you need to have a LaTeX installation, including TikZ, or you can work online with Overleaf or work with the code at https://tikz.org/chapter-05.

The code is also available on GitHub at https://github.com/PacktPublishing/LaTeX-graphics-with-TikZ/tree/main/05-using-styles-and-pics.

In this chapter, we will use the positioning and scope TikZ libraries and the tikzlings package.

Understanding styles

We already customized nodes and edges using several key=value options within square brackets. Examples of keys are color, shape, width, and font.

We can define our own key=value sets. In TikZ, we commonly call such a set a style; and we give it a name. The name itself is also called a key in TikZ. The benefit for us is that such styles can contain a lot of various key=value settings and even code snippets.

Using styles is like working with macros in LaTeX. We can compare styles and macros in this way:

  • If we have code that we use several times, we create a macro in LaTeX. If we have graphical properties values that we use several times, we create a named style in TikZ.
  • Macros in LaTeX separate formatting from the content. Styles in TikZ separate graphical properties from the content of a drawing.
  • Macros and styles save us from repeating code and help structure our documents and drawings.

Named keys have properties, such as a style, and...

Defining and using styles

At first, we take the example of a node and its style. Let’s take this node, which we call A:

\node (A) {A};

Well, it simply prints an A in the default font, without any shape or color. We change that now: let’s have sans-serif and bold font, white text color, the shape of a circle, and color the circle like a blue ball:

\node [font = \sffamily\bfseries, text = white,
      shape = circle, ball color = blue] (A) {A};

That gives us a much fancier A:

Figure 5.1 – A fancy node

Figure 5.1 – A fancy node

That’s quite a lot of options for that node. If we have several nodes in a document, we don’t want to repeat this for every single node. In Chapter 3, Drawing and Positioning Nodes, we saw the every node/.style syntax for applying such a set of options to all nodes in a drawing. That doesn’t help us when we have different kinds of nodes in a drawing.

Let’s explore...

Inheriting styles

You noticed that we used the commands for sans-serif and bold font and white text color both in the vertex and number styles. To avoid repetition and to have a single point of definition, we can define a style used by both. Let’s call it mytext. Then, we can use it within the definitions of both vertex and number:

\tikzset{
  mytext/.style = {font=\sffamily\bfseries, text=white},
  vertex/.style = {mytext, shape=circle,
                   ball color = blue},
  number/.style = {mytext, draw, fill=red}}

That way, we can define fundamental styles for our drawings and create further specific styles based on them.

Similarly, we can define specific styles based on other styles, such as highlighting elements in a drawing. Here, we define a general highlight style and combine it with other styles:

\tikzset{highlight/.style = {draw...

Using styles globally and locally

Using the \tikzset command, you can define styles globally for your whole document. This is especially useful when you have several similar drawings in your document. For example, in a book about graph theory, you` probably want to have the same styles for vertices, edges, and labels in all drawings throughout the book, so it’s good to use \tikzset in the preamble.

In older documents, you will see the \tikzstyle command with the following form:

\tikzstyle{my style} = [options]

That command is deprecated and should not be used anymore, according to the TikZ creator, so bear this in mind when you see it in older code on the internet.

In situations where styles between drawings are different, it can be preferable to define styles locally, so they are only valid in a single picture. That can be done by setting the styles as options in the tikzpicture environment. For example, if you have a drawing where you want to have a particular...

Giving arguments to styles

Remember that in Figure 5.5, we defined the vertex style in the following way:

\tikzset{vertex/.style = {mytext, shape = circle,
  ball color = blue}}

We can introduce an argument when we intend to have different colors with the same style. One argument is easily supported; we can write the following, similar to arguments in macros:

\tikzset{vertex/.style = {mytext, shape = circle,
  ball color = #1}}

Now, we can change our code for Figure 5.5 to choose colors as arguments:

\node[vertex=blue] (A) {A};
\node[vertex=green, right = 4 cm of A] (B) {B};

So, #1 represents an argument in our style, and with style=value, we set that value for #1. We can specify a value that’s used when no value is given using the so-called .default handler:

\tikzset{vertex/.default=blue}

Now, we can write \node[vertex] for a blue node by default, and \node[vertex=green] for a green node.

We may write style={value} to avoid misunderstandings...

Creating and using pics

In LaTeX, we can write macros containing code that can be used repeatedly. How about using TikZ picture code repeatedly in a drawing? We cannot simply put one tikzpicture environment into another one. These pictures and their elements would interfere with each other’s styles and settings.

To solve this, TikZ provides a syntax for creating small pictures that can be used as building blocks in a TikZ drawing. The feature name is pic; let’s also call these short pictures pics.

A pic is a TikZ drawing code sequence, defined in a similar way to setting a style. To get practical, we will define a smiley pic based on the code for our self-made smiley in Chapter 2, Creating the First TikZ Images. The basic syntax is as follows:

\tikzset{smiley/.pic={ ... drawing commands ... }}

Like .style, .pic is also an example of a key handler.

We take our code for Figure 2.11 and put this code into the \tikzset command in the following way:

\tikzset...

Summary

By working through this chapter, you have gained expertise and a professional workflow with which to create TikZ pictures systematically. You can now define and apply your own styles to drawing elements.

In the next chapter, we will apply our knowledge to trees and graphs.

Further reading

The TikZ manual has the complete reference on keys, handlers, styles, and pics. Open it using texdoc tikz at the command line or visit https://texdoc.org/pkg/tikz.

The topics of this chapter are covered in depth in these sections in Part III:

Part VII, Section 87, Key management, explains keys and handlers. An online version is at https://tikz.dev/pgfkeys. It also describes the usage of style arguments in depth.

While this chapter gives you a quick start for easy comprehension, the TikZ manual is the complete reference.

There are further 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 €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