# What should be included in “good practices” for Tikz 101

I have been making graphics (mostly flow graphs) for my presentations, lately, using Powerpoint and most recently LucidChart. I kind of avoided using Latex for this purpose as I had a feeling that its not going to be easy. Anyways, now I have decided to learn and master this powerful tool but going through the manual ,as I expected, is not a good idea for a jump start. So I was wondering if I can get some good advices that I should flow while making graphics in my beamer presentations. One thing that I have noticed that one needs to specify coordinates for everything. Is it a good idea to first draw a grid on slide and then proceed?

So please contribute practices that you have found to be useful for generating graphics using tikz package. I am looking forward to hear what experts have to say in this regard

• You can also use relative coordinates. For a path use ++(x,y). For coordinates (not part of a path) you can use the calc library ($(A)+(x,y)$). For nodes you can use [above right=1em] (for example) or [xshift=x,yshift=y]. – John Kormylo Mar 2 '16 at 23:56
• Make the tikz graphics as separate files and use only the PDFs in the main document. Advantages: faster compilation (significant for long presentations with many images), better focus on frame contents and document structure if the source is not bloated everywhere with distracting tikz code, less risk of break something accidentally and easier debugging in case of disaster. – Fran Mar 3 '16 at 5:55
• I don't think that this question should be tagged beamer. – Federico Poloni Mar 4 '16 at 13:09

• 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.

• So before I start exploring these pointers, I have one basic question. Consider a standard beamer slide with a tile n slide number. How should I fix rest of the available area for the graphic. The idea is that I first assign a fixed space to my graphic in every slide and then proceed from there. Right now I am trying to insert the right size of grid in my code so that from there I can know the coordinates (I don't know yet how can I make tikz compute the coordinates for me). Is this idea of fixing space sound right? – NAASI Mar 3 '16 at 0:44
• I want to replicate my graphics so that by changing colors of my component blocks, I can give an impression of animation. I normally do this by making multiple copies of every slide and then change colors of text to send items in background and foreground. Now I want to do the same with my block diagrams – NAASI Mar 3 '16 at 0:47
• @NAASI: Beamer has a very elegant way of sending elements to "the background and foreground". You can use \only<...>{...} or other variants. Why don't you create a new question with what you to do? – Aditya Mar 3 '16 at 1:08
• What is a "TikZ length command"? When I need a constant, I use \pgfmathsetmacro, but I think that won't let me define real lengths. – Chris Chudzicki Mar 3 '16 at 4:03
• @mrc It is \pgfmathsetlength look for it in the manual – percusse Mar 3 '16 at 10:46
• 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.


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.]

• @HoodChatham That is rather a major consequence, is it not? A bit like saying, do you mean anything more significant than the fact that it will destroy the house? I don't see what other problems striking a match when I smell gas has. – cfr Mar 3 '16 at 3:35
• I agree that it's extremely important, I just thought that since it only takes one simple sentence to explain, it would be better for completeness to include it. I see now that in my original comment I said "more significant than". I agree that that was a poor choice of words -- I was thinking of conceptual significance (difficulty to explain), not practical significance. – Hood Chatham Mar 3 '16 at 3:38
• @Fran ?? What does that do? – cfr Mar 3 '16 at 12:46
• @HoodChatham Very hard ;). It is far from obvious that \table is required by the table environment, for example. Or think about \def\list...! – cfr Mar 3 '16 at 16:16
• @cfr A nuclear war. It is just cryptic black humor: Some time ago (1983) the NATO played a war game (Able Archer) simulating a DEFCON1. The funny was that URSS thought that it was not a game, so were closer than ever to the nuclear war since Cuba missile crisis (1962). An example of a dangerous play with a def[ense condition]. (Do not try to see any relationship with LaTeX, at some point of the sleeplessness I gain this weird no-sense of no-humor). – Fran Mar 3 '16 at 22:14

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 scopes 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.

• For defining a position \coordinate is often better than \node. You can then use \node or node later to add the label. – Andrew Swann Mar 9 '16 at 9:39
• @AndrewSwann: As far as I understand, \coordinate can always be replaced by a node with empty label, but the other way round is less easy (e.g., when you want your label inside the node, and edges arrive precisely at the border). So for starting I think \node is enough, but of course in the long run it is useful to learn about \coordinate to improve the code. – tarulen Mar 9 '16 at 9:49
• Indeed, I only said "often", which of course depends on what you typical use case is. – Andrew Swann Mar 9 '16 at 10:03