### Partner Sites

 Why should I be using TeX for graphics?
(11 votes, average: 4.82 out of 5)
LaTeX - Graphics, Figures & Tables
Written by Pieter Belmans
Tuesday, 19 June 2012 19:00

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.

- A contribution to the LaTeX and Graphics contest -

## 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:

1. it produces vector graphics, with excellent scaling etc.;
2. reuse and integrating work by others is easy;
3. modifications are done by editing the source code, no other software is required;
4. everything is consistent: the fonts and colours are the same throughout your text;
5. ...

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.

About the author Pieter Belmans (or pbelmans) is the author behind On music, computing and math, a weblog that despite its name is mostly about (La)TeX. But everything he does with TeX and its nephews is directly related to these three subjects though, as shown in this article. His contributions to the world of TeX mainly concern Pygments and its integration using minted (ua-pygments-style, ugent-pygments-style, gap-pygments-lexer), beamer themes (ugent-beamer-theme), various modifications and contributions to the localization of bibliographies in biblatex and babelbib and a Dutch LaTeX course. When not doing TeX he is a graduate student in mathematics.

Justin
0

Thanks for a great overview of the graphics packages available for TeX. TikZ especially is a great tool. However, you really need to mention METAPOST (a descendant of METAFONT). METAPOST diagrams are not writtien in TeX as TikZ, but you could argue it makes the creation of techinical diagrams even easier than TikZ, due to its linear equation solver. Knuth has said he's redone all the figures in his Art of Computer Programming books using METAPOST, in fact. Still, it's not a language for the faint of heart. You need to read the METAFONTbook to make sense of METAPOST.
Name *
Email (For verification & Replies)
Code
Pieter
0

You are of course right in noting that METAPOST is equally worthy! But in the next-to-last section titled Remarks, I do mention METAPOST (and others). Maybe the capitalization is wrong, but HTML didn't let me do the nice font trick necessary to write METAPOST the way it should be written :).
Name *
Email (For verification & Replies)
Code
Shiraj
0

Thank you for the fast reply. It does work now (wxt and x11 terminal)I rpeimeolcd GNUPLOT but with a few chances to your howto.(1) I needed to install emacs (no idea why that checkinstall aborted without it)(2) I had to run .\prepare without the flags with-readline=gnu with-lua (but tikz-output does work) (otherwise checkinstall aborted)(3) needed the flag -fstrans=no by checkinstallthe package manager (which I used to install the .deb) remarked that my .deb is of bad quality I just ignored it.all works fine now on ubuntu 11.04p.s. very nice page here with a lot of interesting posts
Name *
Email (For verification & Replies)
Code
Vijay
0

I don't think the beat frequency image was actually generated by its associated code. The figure has a typo: the sum is labeled "som"! Excellent article. I'm so very happy to have found Tikz, and I'm interested in poking around at the matlab integration, soon.

Thanks for the article!
Name *
Email (For verification & Replies)
Code
Pieter
0

You are right, the figure is a screenshot from my slides which were intended for a Dutch audience, I translated the code (som is Dutch for sum). Aside from that and the colors the code should produce the same plot.
Name *
Email (For verification & Replies)
Code
Name *
Email (For verification & Replies)
Code

### Latest Forum Posts

﻿

Re: Citation in the teaser - before maketitle
21/10/2014 09:14, Johannes_B

Re: Bibliography Reference Numbers not appearing
21/10/2014 08:27, Johannes_B

Re: Bibliography Reference Numbers not appearing
21/10/2014 04:22, jaman

Re: Bibliography Reference Numbers not appearing
21/10/2014 04:07, jaman

Re: Margins
20/10/2014 22:42, Afshin