LaTeX/Introducing Procedural Graphics

In the Importing Graphics chapter, you learned that you can import or link graphics into LaTeX, such as graphics that you have created in another program or obtained elsewhere. In this chapter, you will learn how to create or embed graphics directly in a LaTeX document. The graphics is marked up using commands similar to those for typesetting bold text or creating mathematical formulas, as the following example of embedded graphics shows:

There are several packages supporting the creation of graphics directly in LaTeX, including picture, xy-Pic and PGF/TikZ, described in the following sections.

Compared to WYSIWIG tools like Xfig or Inkscape, this approach is more time consuming but leads to much better results. Furthermore, the output is flawlessly integrated into your document (no contrast in size nor fonts).

See the Importing Graphics for more details on graphics importation and some attempts to circumvent to integration issue.

Overview
The picture environment allows programming pictures directly in LaTeX. On the one hand, there are rather severe constraints, as the slopes of line segments as well as the radii of circles are restricted to a narrow choice of values. On the other hand, the picture environment of LaTeX2e brings with it the \qbezier command, "q" meaning quadratic. Many frequently-used curves such as circles, ellipses, and catenaries can be satisfactorily approximated by quadratic Bézier curves, although this may require some mathematical toil. If a programming language like Java is used to generate \qbezier blocks of LaTeX input files, the picture environment becomes quite powerful.

Although programming pictures directly in LaTeX is severely restricted, and often rather tiresome, there are still reasons for doing so. The documents thus produced are "small" with respect to bytes, and there are no additional graphics files to be dragged along.

Packages like epic, eepic or <tt>pstricks</tt> enhance the original picture environment, and greatly strengthen the graphical power of LaTeX.

While the former two packages just enhance the picture environment, the <tt>pstricks</tt> package has its own drawing environment, <tt>pspicture</tt>. The power of <tt>pstricks</tt> stems from the fact that this package makes extensive use of PostScript possibilities. Unfortunately, it has one big shortcoming: it doesn't work together with pdfLaTeX, as such. To generate a PDF document from TeX source, you have to go from TeX to DVI to PDF, losing hyperlinks, metadata, and microtypographic features of pdflatex in the process.

In addition, numerous packages have been written for specific purposes. One of them is XY-pic, described at the end of this chapter. A wide variety of these packages are described in detail in The LaTeX Graphics Companion (not to be confused with The LaTeX Companion).

Perhaps the most powerful graphical tool related with LaTeX is MetaPost, the twin of Donald E. Knuth’s METAFONT. MetaPost has the very powerful and mathematically sophisticated programming language of METAFONT. Contrary to METAFONT, which generates bitmaps, MetaPost generates encapsulated PostScript files, which can be imported in LaTeX. For an introduction, see A User’s Manual for MetaPost. A very thorough discussion of LaTeX and TEX strategies for graphics (and fonts) can be found in TEX Unbound

The last but certainly not least are the PGF/TikZ and Asymptote systems. While the previous systems (<tt>picture</tt>, <tt>epic</tt>, <tt>pstricks</tt> or <tt>metapost</tt>) focus on the how to draw, TikZ and Asymptote focus more on the what to draw. One could say that TikZ and Asymptote are to drawing in LaTeX as LaTeX is to digital typesetting. It's recommended to use one of these if your LaTeX distribution includes it. TikZ is a pure (La)TeX system, not reliant on external software, while Asymptote is an external system which integrates seamlessly with (La)TeX. If using Asymptote, it is very helpful to use latexmk to manage the compilation steps.

In many cases, especially for more advanced diagrams, it may be easier to draw the graphics using external vector graphics software, and then import the file into the document (see ../Importing Graphics). However, most software does not support LaTeX fonts or mathematical notation, which can result in not suitable and inconsistent graphics. There are several solutions to this problem.

LaTeX/Представљање процедуралних графика