What should be included in "good practices" for Tikz 101
Use styles. Whenever you have color, shape, fonts, alignment, define a TikZ style for it and use it. Don't apply such formatting details to nodes or edges, apply the style. A single point for consistent customizing.
Inherit styles. Start with a base node style (font family, base color), define styles which use base styles and add size or color or alignment - no repetitions, single points for global changes.
Use macros. Have consistent TikZ commands or command sequences, which can be reused and changed.
Use constants. For every value needed, such as distances, declare a constant via
\def
or a TikZ length command, so you can use it repeatedly and adjust it at a single source code position to customize a whole drawing or a lot of drawings.Use relative positions. So you can change a reference coordinate, and all other positions will be automatically adjusted.
Let TikZ calculate for you. Once certain points such as corners are defined, use TikZ syntax to define a relative positions such as middle points and intersection points. Let TikZ do the geometry for you. If you change the reference points or image size, all will automatically adjust.
Name everything. Especially in non-trivial drawings, edges between named coordinates are much clearer to read than using coordinate numbers everywhere.
Use scopes. Don't repeat things - if you cannot apply a bunch of properties via styles, use a scope to apply settings to a whole area of a drawing. Also here, it's easy to change that part at a single position.
Use loops. If you need to repeat things, benefit from the power of TikZ
\foreach
loops to reduce the amount of repeated code.Don't nest TikZ pictures. There is always another way to do it.
Don't try to use everything from Stefan Kottwitz's otherwise mostly excellent answer in your first TikZ graphic.
More generally, don't try to learn everything at once. Even if your initial code is longer, less flexible and harder to maintain.
When you have sufficient knowledge to produce a picture, add one complication as appropriate e.g. a loop or a style.
When you can do basic loops or styles without too much trouble, add one more thing.
Don't use
\def
unless (1) you cannot use a LaTeX command creation macro (\newcommand*
,\newcommand
,\newlength
,\newcounter
etc.) and (2) you understand why you need\def
and (3) you understand the consequences of using\def
.
Not specific to TikZ:
annotate your code so that you can tell what it is for when you inevitably forget later.
[At least, I inevitably forget.]
1. Start with basics
Place named nodes and draw lines between them:
\node (a) at (1,5) {$A$};
\node (b) at (2,3) {$B$};
\draw (a) -- (b);
This way you can make any simple graph, you just need to get your coordinates right (at this point, it is better to make a quick drawing on paper first, and get the coordinates this way)
2. Improve style as you need
Tikz has many styling functions, for line width, colors, etc. Some more useful than others. Look for anything you think would improve the drawing, and you'll find it. You'll remember the most useful ones quickly.
3. Improve your code to make it more efficient
Don't want to give exact numbers for coordinates? There are many ways to achieve relative placement! Learn about .north
, .south
placement, or use calculations in coordinates.
You use copy-paste too much for your own taste? Learn about styles, macros, use scope
s or the most-powerfull \foreach
loops.
You use beamer? Learn how to make overlays painlessly using e.g. \only
or \alt
You'll end up having a much shorter code, much faster to write, so you can quickly make, say, a nice animation of an algorithm running on a graph.