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

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...

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