Home Business & Other LaTeX Graphics with TikZ

LaTeX Graphics with TikZ

By Stefan Kottwitz
ai-assist-svg-icon Book + AI Assistant
eBook + AI Assistant $35.99 $24.99
Print $44.99
Subscription $15.99 $10 p/m for three months
ai-assist-svg-icon NEW: AI Assistant (beta) Available with eBook, Print, and Subscription.
ai-assist-svg-icon NEW: AI Assistant (beta) Available with eBook, Print, and Subscription. $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime! ai-assist-svg-icon NEW: AI Assistant (beta) Available with eBook, Print, and Subscription.
What do you get with a Packt Subscription?
Gain access to our AI Assistant (beta) for an exclusive selection of 500 books, available during your subscription period. Enjoy a personalized, interactive, and narrative experience to engage with the book content on a deeper level.
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
Gain access to our AI Assistant (beta) for an exclusive selection of 500 books, available during your subscription period. Enjoy a personalized, interactive, and narrative experience to engage with the book content on a deeper level.
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Along with your eBook purchase, enjoy AI Assistant (beta) access in our online reader for a personalized, interactive reading experience.
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
ai-assist-svg-icon NEW: AI Assistant (beta) Available with eBook, Print, and Subscription. ai-assist-svg-icon NEW: AI Assistant (beta) Available with eBook, Print, and Subscription. BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime! ai-assist-svg-icon NEW: AI Assistant (beta) Available with eBook, Print, and Subscription.
eBook + AI Assistant $35.99 $24.99
Print $44.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
Gain access to our AI Assistant (beta) for an exclusive selection of 500 books, available during your subscription period. Enjoy a personalized, interactive, and narrative experience to engage with the book content on a deeper level.
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
Gain access to our AI Assistant (beta) for an exclusive selection of 500 books, available during your subscription period. Enjoy a personalized, interactive, and narrative experience to engage with the book content on a deeper level.
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Along with your eBook purchase, enjoy AI Assistant (beta) access in our online reader for a personalized, interactive reading experience.
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Chapter 2: Creating the First TikZ Images
About this book
In this first-of-its-kind TikZ book, you’ll embark on a journey to discover the fascinating realm of TikZ—what it’s about, the philosophy behind it, and what sets it apart from other graphics libraries. From installation procedures to the intricacies of its syntax, this comprehensive guide will help you use TikZ to create flawless graphics to captivate your audience in theses, articles, or books. You’ll learn all the details starting with drawing nodes, edges, and arrows and arranging them with perfect alignment. As you explore advanced features, you’ll gain proficiency in using colors and transparency for filling and shading, and clipping image parts. You’ll learn to define TikZ styles and work with coordinate calculations and transformations. That’s not all! You’ll work with layers, overlays, absolute positioning, and adding special decorations and take it a step further using add-on packages for drawing diagrams, charts, and plots. By the end of this TikZ book, you’ll have mastered the finer details of image creation, enabling you to achieve visually stunning graphics with great precision.
Publication date:
June 2023
Publisher
Packt
Pages
304
ISBN
9781804618233

 

Getting Started with TikZ

First, congratulations on using LaTeX! You have already chosen an excellent tool for writing, and now you are ready to add high-quality figures to your documents.

To work with this book, you should have a good understanding of LaTeX and know how to work with a LaTeX editor and compiler. If you are still learning about LaTeX, here are two recommended books:

  • The LaTeX Beginner’s Guide, Second Edition by Packt Publishing gives you a fast start; you can find more information about it at https://latexguide.org
  • The LaTeX Cookbook by Packt Publishing offers many ready-to-use examples for various document types. Look at the book’s website at https://latex-cookbook.net

This chapter shall be a quickstart. We will cover the following topics:

  • What is TikZ?
  • Alternative graphics packages
  • Benefits of TikZ
  • Installing TikZ
  • Working with TikZ documentation
  • Creating our first TikZ figure

We will briefly look...

 

Technical requirements

You need to have a LaTeX distribution installed on your computer, for example, TeX Live (https://tug.org/texlive), MacTeX (https://tug.org/mactex), or MiKTeX (https://miktex.org). A full installation is recommended. It may take up to 8 GB of space on your hard disk, but you don’t need to worry about missing packages.

Alternatively, you can use Overleaf (https://overleaf.com). It’s an excellent online LaTeX editor and compiler; that’s why it requires a permanent internet connection to be able to use it.

All code examples of this chapter are available on GitHub at https://github.com/PacktPublishing/LaTeX-graphics-with-TikZ/tree/main/01-Getting-Started-with-TikZ.

At https://tikz.org, you can also find all code examples from this book. You can edit and compile to PDF directly on that website, so you can even use a smartphone or tablet to work with this book’s code.

 

What is TikZ?

The inventor of TikZ, Till Tantau, created the name as a recursive acronym in German. TikZ stands for TikZ ist kein Zeichenprogramm, which translates to TikZ is not a drawing program. It’s Tantau’s jokey way of emphasizing that you cannot expect to draw with it like with a pen or just mouse clicks, such as with Microsoft Paint on Windows, Paintbrush on a Mac, Adobe Illustrator Draw, or the free Inkscape vector graphics editor.

Simply said, TikZ is a set of TeX commands for drawing graphics. Just like LaTeX is code that describes a document, TikZ is code that describes graphics and looks like LaTeX code. With TikZ, you write \draw [blue] circle (1cm); to get a blue circle with a 1 cm radius in your PDF document.

The origin of TikZ is called PGF, which stands for Portable Graphics Format and is a set of graphics macros that can be used with pdfLaTeX and the classic DVI/PostScript-based LaTeX. Today, we consider TikZ as the frontend and PGF as the backend...

 

Alternative graphics packages

Before we start, let’s quickly look at where we come from and what else is out there.

The LaTeX picture environment

LaTeX itself defines some basic graphics commands. We can use a picture environment for this.

To get an idea of how it works, let’s have a quick look at a minimal example:

\setlength{\unitlength}{1cm}
\begin{picture}(1,1)
  \put(0,0){\circle{1}}
  \put(-0.5,0){\line(1,0){1}}
  \put(-0.3,0.06){text}
\end{picture}

The output is the following:

Figure 1.1 – A classic LaTeX picture drawing

Figure 1.1 – A classic LaTeX picture drawing

Here, we did the following:

  1. We set a base length. All numbers in drawing commands are seen as multiples of this base unit length.
  2. We used \put(x,y){…} to put something at the Cartesian coordinate position x, y.
  3. We wrote \circle{x} to get a circle with a diameter of x times the unit length.
  4. We used \line(x,y){z} to get a line in vector direction...
 

Benefits of TikZ

Compared to classic drawing programs where you click with the mouse on shapes and toolbars and drag and drop graphic elements, TikZ is very different. With TikZ, you program graphics with code.

That means your graphics will be the following:

  • Precise: You get the exact placement of graphic and text elements using anchors, baselines, alignment, relative positioning, and implicit coordinate calculations.
  • Consistent: TikZ blends in perfectly with LaTeX. You can use LaTeX fonts, symbols, formulas, colors, and macros within your drawing, and your drawing details will precisely match your LaTeX document design. That would be different if you imported some externally made images.
  • High-quality: TikZ generates scalable PDF images that look fine when you zoom in or out. There are no blurry or pixelated images.
  • Efficient: Similar figures mean similar code and similar styles; it’s all reusable, and when you adjust global styles, you change the appearance...
 

Installing TikZ

As you already have LaTeX installed, you just need to ensure that the pgf and xcolor packages are installed. You can install them in three main ways, which are discussed in the following sub-sections.

With a vanilla TeX distribution

If you installed LaTeX from DVD or via the internet from an original TeX distribution, use its package manager to install the pgf and xcolor packages. These are the three principal TeX distributions, along with installation details:

  • TeX Live: Start TeX Live Manager (tlmgr or tlshell), then search and install the pgf and xcolor packages
  • MiKTeX: Use the MiKTeX package manager (mpm) to install pgf and xcolor
  • MacTeX: Use TeX Live Utility to install pgf and xcolor

When you want to update TikZ later on, run your regular TeX distribution updates, and pgf will be updated as well.

With an operating system TeX installation

If your LaTeX installation comes from your operating system repositories, which is usually...

 

Working with the TikZ documentation

The LaTeX and TikZ installations contain documentation. You can access it in the following two ways:

  • Windows: Run the cmd app via the Start menu
  • Apple macOS or Linux: Open a terminal window

Then, type this command:

texdoc tikz

Your PDF viewing app will open and display the TikZ manual in all its 1,300-page size and glory. But don’t feel intimidated, for the following reasons:

  • It’s good to have so many features so well documented in a huge reference with an extensive index, hyperlinks, full-text search, and of course, a lot of examples.
  • Hundreds of pages are about the PGF backend, the basic layer, and the system layer on the driver level. You don’t need that as a user.
  • It describes all additional libraries and utilities.
  • It contains five tutorials.

I hope that in the future, this manual will be split into a TikZ manual, a PGF backend reference for developers, and tutorials.

...
 

Creating our first TikZ figure

Our first goal is to create a TikZ drawing that is the same as Figure 1.1, which we made in the classic LaTeX picture mode, to get a feeling of the TikZ basics.

To be able to use TikZ, you need to perform the following three steps:

  1. Load the tikz package in your document preamble:
    \usepackage{tikz}
  2. TikZ provides additional features with separate libraries. Here, we load the quotes library for adding annotations with an easy quoting syntax that we will use in the drawing:
    \usetikzlibrary{quotes}
  3. Use a tikzpicture environment for the drawing. The first code snippet we saw in this chapter, for the picture environment, will look like this with TikZ:
    \begin{tikzpicture}
      \draw circle (0.5);
      \draw (-0.5,0) to ["text"] (0.5,0);
    \end{tikzpicture}

This results in the following output:

Figure 1.2 – Our first TikZ drawing

Figure 1.2 – Our first TikZ drawing

We draw a circle with a radius of 0.5 cm at the default...

 

Summary

In this chapter, we learned what TikZ is, how to install it, and how to access the documentation. We had a first glimpse at the syntax and created our first TikZ figure of the book. Rest assured, there are many more to come!

In the coming chapters, we will learn more about TikZ commands, objects, and styles, to create more fancy drawings. In the next chapter, we’ll learn about the essential toolbox, with coordinates, paths, colors, lines, circles, and other curves and shapes.

 

Further reading

The TikZ manual is an excellent and comprehensive reference book. We will refer to it many times. You can find the manual at https://texdoc.org/pkg/tikz in PDF format and at https://tikz.dev in handy reflowable HTML sections, which makes it easier to read on a smartphone or tablet.

The LaTeX Graphics Companion by Michel Goossens, Frank Mittelbach, et al. is a comprehensive book about creating LaTeX graphics. With the first edition published in 1997 and the second and latest edition published in 2007, it doesn’t cover the newest developments, even though there’s a reprint of the second edition from 2022. When I bought the reprint and noticed that TikZ is completely missing in that book, I decided to write the book you hold in your hand now.

PSTricks: Graphics and PostScript for TeX and LaTeX by Herbert Voss is a great reference book specifically about PSTricks with many examples.

MetaPost: Grafik für TeX und LaTeX by Walter Entenmann is a...

About the Author
  • 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.

    Browse publications by this author
Latest Reviews (3 reviews total)
The ebook is very large. This makes it hard to access on any device without substantial memory. Perhaps there is a remedy for this problem.
The book is excellent to get a good understanding about the concept behind TikZ and pgf. I used LaTex and TikZ in a way of copy and paste from different websites but had no deeper understanding of TikZ. The TikZ manual is excellent, of course, but as a beginner you get lost in the level of details provided. Mr.Kottwitz book fills the gap of an easy-to-learn introduction to TikZ and will enable me to work successful with the TikZ manual.
LaTeX Graphics with TikZ
Unlock this book and the full library FREE for 7 days
Start now