Summary
This lesson teaches how to draw inside the document with TikZ. It explains what is gained by drawing in LaTeX (the typeface and the formulas of the text, scaling without loss, correction in the source and version control), what it costs, and when it is better to export a chart from R or Python; it shows that, in a document in a language whose babel module turns signs into shorthands, such as Spanish, TikZ needs its babel library, without which the first arrow produces a cascade of errors; it presents the tikzpicture environment and the \tikz command, Cartesian, polar and relative coordinates, and paths (segments, rectangles, circles, arcs and curves); it goes through the options of the stroke and the arrow tips of the arrows.meta library; it teaches nodes, named, with anchors and placed relative to one another with the positioning library; it fixes the notation with styles of one's own; it repeats with \foreach; it composes a flowchart, a force diagram, a concept tree, a syntax tree and the graph of a function, warning that plot measures angles in degrees; it presents pgfplots for data plots; it puts the figure in a float and compares the three ways of scaling it; and it ends with the typical errors, compilation time and what is best avoided.
Learning objectives
- Decide with reasons whether a figure should be drawn with TikZ, plotted with pgfplots or exported from a data analysis program.
- Prepare the preamble of a document for TikZ and explain when and why the babel library is needed.
- Draw with absolute, polar and relative coordinates, and predict the path traced by a sequence of coordinates with
+and++. - Compose diagrams with named nodes, placed with the positioning library, and with styles of one's own defined only once.
- Insert a drawing in a float with its caption and its label, scale it predicting what happens to the text, and diagnose the typical errors from the log.
Why draw in LaTeX
The previous lessons inserted images made outside the document. A drawing made with TikZ, by contrast, is written in the source with commands, and the engine typesets it as it typesets the text. Four advantages follow from this. The first is typographic unity: the labels of the drawing use the same typeface and the same size as the text, and a formula in a label is identical to the one in the paragraph that comments on it. The second is scaling without loss: the result is vector graphics, so it can be enlarged without pixels appearing. The third is that the drawing is corrected in the source: changing a label or moving a point means changing a line, without going back to a drawing program or looking for the original file. The fourth is that the drawing is versioned with the text, because it is text: it stays in the same file, in the same backup and in the same history.
These advantages, however, come at a price. TikZ is a language with its own syntax, whose manual runs to more than a thousand pages, and a drawing that takes a minute with the mouse in a graphics program may take half an hour here the first time. It is therefore worth distinguishing. Schemes, diagrams and geometric figures, made of a few shapes with labels, are the natural territory of TikZ. Data plots, on the other hand, are better made with pgfplots, built on TikZ and presented at the end of this lesson, or, if the data are analysed in R or in Python, with the same program that analyses them: the chart is exported as PDF and inserted as in the lesson on images, and it remains tied to the data and to the analysis that produce it. Both languages also offer outputs that write the chart as code for LaTeX (the tikzDevice package in R and the PGF backend of matplotlib in Python), so that the labels take on the typeface of the document.
Loading the content…
Completed
Sign in to save your progress