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

Transforming
Coordinates and Canvas

In this chapter, we will deal with transformations. We already used a basic transformation in the code for Figure 9.0 when we used the yshift option to move a coordinate higher in the y-direction. We will now look thoroughly into moving, rotating, and scaling coordinates and apply this to our drawings.

In particular, we will explore the following topics:

  • Shifting nodes and coordinates
  • Rotating, scaling, and slanting
  • Transforming the canvas

Once you have mastered this chapter, you will be equipped with a toolset for minor adjustments and even complex relative positioning of coordinates, nodes, edges, or complex paths.

Technical requirements

At https://tikz.org/chapter-11, you can study and compile the complete code for this chapter’s examples. The GitHub link for downloading the code is https://github.com/PacktPublishing/LaTeX-graphics-with-TikZ/tree/main/11-transforming-coordinates.

This chapter doesn’t require additional TikZ libraries, though you may take a look at the tikz-ext package.

Shifting nodes and coordinates

We will start with shifting. This is another word for doing a translation, which means moving a coordinate or a node to another position in a straight line by a particular dimension or by another coordinate.

The following three options can be used for shifting:

  • xshift is a dimension for moving in the x-direction, adding this dimension to the x value of the coordinate.
  • yshift does the same but in the y-direction. Here, TikZ adds this dimension to the y value of the coordinate.
  • shift is a coordinate to be added; its x and y values will be added to the x and y values of the other coordinate. Here, no dimension is used. You can use dimensions, but you don’t have to. The coordinate must be given in curly braces.

The shifting value can be used as an option to a path, so it’s applied to every coordinate in the path. For example, the following command draws a line from (0,2) to (1,3):

\draw[yshift=2cm] (0,0) -- (1,1...

Rotating, scaling, and slanting

A rotation means rotating a coordinate, node, or path around another coordinate or an axis. We will look at this first.

The following options can be used to rotate coordinates and paths:

  • rotate is a value in degrees that rotates a coordinate or the coordinate system of a path around the origin. When used as an option for a node, the node’s center is considered the origin of the rotation.
  • rotate around takes an angle in degrees and a coordinate. This rotates around the given coordinate by that angle.

The following command draws a filled triangle that is rotated around the origin by 45 degrees:

\fill[orange, rotate=45] (0,1) -- (3,1) -- (2,2) --cycle;

You can compare it with the original triangle, filled with the color gray in the following figure:

Figure 11.1 – Rotating a triangle around the origin

Figure 11.1 – Rotating a triangle around the origin

This command also rotates the triangle by 45 degrees, but around the coordinate (0...

Transforming the canvas

Look at Figure 4.1, where we had an arrow between two nodes. The code for the arrow was the following:

\draw (tex) edge[->] (pdf);

When we want to draw a double arrow, a straightforward approach is to draw two such arrows and shift one up and one down. Let’s do this, and add some rotation to practice our new skills:

\draw (tex) edge[->,yshift= 0.1mm, rotate= 4] (pdf);
\draw (tex) edge[->,yshift=-0.1mm, rotate=-4] (pdf);

If you compile, you may be surprised: both the shift and rotation don’t have any effect; the arrow is the same in both cases.

In such a situation, we can transform the canvas instead of the coordinates. The canvas is our drawing area, like a sheet of paper, and a canvas transformation applies to everything: coordinates, text, line widths, everything. It happens on a lower level, with PDF or PostScript features, so we cannot track nodes or sizes at that time. Still, we can use it on a path to enforce a transformation...

Summary

Now you have the tools for shifting, rotating, and slanting coordinates, paths, and nodes. Using geometric transformations, you can now position objects and transform their orientation and shape in an advanced way.

In the next chapter, we will again focus on drawing. Specifically, we will learn about several ways to draw smooth curves that please the eye.

Further reading

The TikZ manual at https://texdoc.org/pkg/tikz covers transformations in Part III in the following sections:

  • Section 17.7, Transformations, tells you how to transform nodes. You can find it online at https://tikz.dev/tikz-shapes#sec-17.7.
  • Section 25, Transformations, deals with transformations in general. It is available online at https://tikz.dev/tikz-transformations.
  • Part IX, Section 99.4, Coordinate versus canvas transformations, explains the difference between coordinate and canvas transformations. The online link is https://tikz.dev/base-design#sec-99.4.

You can read the tikz-ext package documentation at https://texdoc.org/pkg/tikz-ext.

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