By the time you read this the revolution that Donald Knuth has caused in typesetting by creating TeX has made all of our lives so much easier. And I believe we are not enough aware of it. Producing high-quality typesetting and beautiful typography, including mathematics, tables of contents, glossaries, … It is all possible with a few backslashes and curly brackets.
Over the last years a similar breakthrough has been occurring in the creation of graphics. This is no longer the territory of either big software suites or obscure programs. The current open-source software related to TeX has matured greatly and is now capable of what was previously only within reach of the happy few with specific knowledge and tools. This article is about how I have been experiencing the aforementioned breakthrough, hoping to convince you of trying some of the currently available tools yourself.
PGF/TikZ
Central in the TeX & graphics workflow is PGF/TikZ. Its first version was released in 2007 and in its current 2.10 release it is considered by myself and many others to be the indispensible tool for most of our graphics creating. It also provides the glue between many other things, which I will discuss later on.
To quote the manual, and explaining the name: PGF means “Portable Graphics Format” and TikZ stands for “TikZ ist kein Zeichenprogramm”. This means that you, just like writing TeX is in a sense programming your text, the drawing consists of programming your graphics. This may sound hard and counterintuitive, but the pros are plenty:
- it produces vector graphics, with excellent scaling etc.;
- reuse and integrating work by others is easy;
- modifications are done by editing the source code, no other software is required;
- everything is consistent: the fonts and colours are the same throughout your text;
- …
Like Leslie Lamport said in his book LaTeX: A Document Preparation System:
Making Greek letters is as easy as
$\pi$
.
This describes perfectly what a revolution TeX has caused in the area of typesetting. No more typewriters, handwritten mathematics or expensive publisher-only technology. The same can be said about PGF/TikZ:
Drawing an orangle circle is as easy as
\tikz \fill[orange] (1ex,1ex) circle (1ex);
.
If you don’t stick to drawing colored circles and decide to go all the way, you can get these examples from TeXample.net:
Figure 1 The TeXtronics oscilloscope
Figure 2 A complete graph on 16 nodes
And a little example I created myself, automating the drawing of beating frequencies in TeX:
\newcommand\frequencydiagram[5]{ \begin{tikzpicture}[scale = 0.85, domain = 0:10, samples = 5000] \draw[very thin] (-0.1,-2.1) grid (10.1,2.1); \draw[thick, ->] (-0.2,0) -- (10.2,0); \draw[thick, ->] (0,-2.2) -- (0,2.2) node[above] {amplitude}; \draw[fill] (0,0) circle (2pt) node [below left] {$0$}; \draw[fill] (pi,0) circle (2pt) node [below] {$\pi$}; \draw[fill] (2*pi,0) circle (2pt) node [below] {$2\pi$}; \draw[fill] (3*pi,0) circle (2pt) node [below] {$3\pi$}; \draw[color = yellow] plot function{sin(#1*#2*x)} node[right] {\SI{#4}{\hertz}}; \draw[color = red] plot function{sin(#1*#3*x)} node[right] {\SI{#5}{\hertz}}; \draw[semithick, color = blue] plot function{2*sin(#1*(#3+#2)*x/2)*cos(#1*(#3-#2)*x/2)} node[right] {sum}; \end{tikzpicture} } \frequencydiagram{8}{1}{1.125}{8}{9}
Code 1 Drawing beating frequencies
which gives
Figure 3 Beating frequencies
gnuplot
But PGF/TikZ doesn’t live on its own. gnuplot
has been around for a while, providing a commandline approach to drawing plots ever since 1986! It integrates well with many computer algebra systems and programming languages, and more importantly: it cooperates with PGF/TikZ! You can use gnuplot
to generate data, that way you get the power of gnuplot
in data handling and calculations while PGF/TikZ manages the actual output. Or if you want to go all the way you can let gnuplot
output to TeX code using the tikz
terminal and use all of its own options while ensuring a tight integration with your document through its TeX awareness!
Again an example from TeXample.net:
Figure 4 Possible output from the tikz
terminal in gnuplot
matlab2tikz
and pgfplots
Another tool for the integration of plots is matlab2tikz
. Using this Matlab script you can export Matlab figures to TikZ code, as the name suggests. So in a way it is similar to the tikz
terminal in gnuplot
. The output includes many of the options you can set in Matlab, so changing your workflow consists of running the matlab2tikz
script after creating the plot and including the output. Which is even easier than outputting to some graphics format, converting to another and then including the plot! I cannot think of a reason not to use this if Matlab is part of your workflow.
What underlies all these external tools is pgfplots
: an excellent package capable of producing plots directly from TeX. A little example from my own catalogue:
Figure 5 The numerical continuation of a surface
The code (but not the data file) for this is
\begin{tikzpicture} \begin{axis}[view/h=100] \addplot3[only marks, mark=*, mark size = 1pt, blue] file {data/pseudoarc-naive-surface.dat}; \end{axis} \end{tikzpicture}
Code 2 Plotting a 3d plot is as easy as writing $\pi
tikz-cd
The last tool I want to discuss is the brand new package tikz-cd
, just a few months old but already indispensible in my daily use of TeX. At last it is possible to create commutative diagrams in an intuitive and flexible way, with integration in equation environments and the possibility to use all of TikZ’ power if you want. A little example from my own documents:
Figure 6 A (simple) commutative diagram but written with great ease using tikz-cd
The code to produce this is
\begin{tikzcd} R(\Gamma) \arrow{r} \arrow{d} & R(\Gamma\times\Gamma) \arrow{d} \arrow[hook]{rd} & \\ R(\Gamma\times\Gamma) \arrow{r} \arrow[hook]{rd} & R(\Gamma\times\Gamma\times\Gamma) \arrow[hook]{rd} & R(\Gamma)\otimes_k R(\Gamma) \arrow{d} \\ & R(\Gamma)\otimes_k R(\Gamma) \arrow{r} & R(\Gamma)\otimes_k R(\Gamma)\otimes_k R(\Gamma) \\ \end{tikzcd}
Code 3 An example of a commutative diagram using tikz-cd
And these are not the only packages built on PGF/TikZ, there are many others just like pgfplots
and tikz-cd
, for a list I refer you to ctan.org/tex-archive/graphics/pgf/contrib. Name it, and it should be possible!
Remarks
It should be obvious I am a big fan of the PGF family, but of course there are tools available like Metapost, Asymptote, PSTricks, … I wrote this article from my own experience which doesn’t include these programs, but judging from what they can do as seen on tex.stackexchange.com they should be considered as good as the PGF family, the adagium “the right tool for the right job” applies here.
Conclusions
By this time I hope it is clear that so much is possible, thanks to the excellent work of so many people. We are capable of producing little gems of graphics ourselves, thereby enrichting our already beautiful typography using TeX. This is a revolution that has been ongoing for the last couple of years, rendering everything accessible to the public. And I hope you are convinced to try some of it yourself for your next paper, course notes, thesis or book.