171

First, I must say I have very limited knowledge about PGF/TikZ. Most things I do are blind copy-pastes of examples I find on the Internet with lots of trial-and-error and googling around.

After spending 4 hours on drawing the simple diagram below (and many other hours on previous diagrams), I could not help to wonder how much time an experienced TikZ user would need to draw it. Had I used some click-and-drag tool, it would definitely take less than 30 minutes.

I understand that TikZ has quite a learning curve, much like LaTeX itself and other tools like the vi editor, shell scripting, sed, awk, etc. You have to study a lot to actually figure out how things work and how to express what you want (just like learning a programming language), but the sacrifice pays off later, because you can accomplish tasks much faster than with regular "friendly" tools. However, I am not so sure this holds for TikZ, at least for simple stuff like Computer Science diagrams.

Hence, my question is: Would it pay off for me to learn and understand all principles and techniques of PGF/TikZ? Would you, as an expert TikZ user, draw a simple diagram like the one below faster than a regular click-and-drag user?

enter image description here

Link to diagram: https://docs.google.com/open?id=0ByXEz0ubVg5rTGh3bFo3SW93c00

8
  • 12
    TikZedt seems to be something that you might enjoy for not-so-complicated drawings.
    – percusse
    Commented Apr 19, 2012 at 11:56
  • 6
    Instantly seeing the drawing you are typing is indeed very nice and productive, even for professional TikZers. Too bas it's windows-only. Commented Apr 19, 2012 at 15:51
  • 8
    Actually, that's another reason why I like to program my diagrams - I don't get distracted by what it looks like at a halfway stage. Commented Apr 19, 2012 at 17:38
  • 7
    For Linux you also have KTikZ
    – percusse
    Commented Apr 19, 2012 at 22:18
  • 3
    For diagrams that are a little simpler than this, you can easily get away with using the simpler GraphViz. In the simplest cases its syntax is dead simple: digraph G { "a" -> "c" -> "d"; "b" -> "c" }. Save in test.dot and then run dot test.dot -Tsvg -o test.svg.
    – badp
    Commented Apr 20, 2012 at 6:25

6 Answers 6

138

I took one look at that diagram and thought, "Absolutely! Use TikZ.". I then thought of trying to typeset that diagram and seeing how long it took me - I would estimate about a 1/4hr to get something fairly close and then probably another 10 minutes to tweak things to how I wanted them (assuming that I didn't encounter either any hidden difficulties or any interesting behaviour - which would send me off on a tangent while I found out about it).

But then I decided that that wouldn't be helpful. In my "scratch" directory where I try out answers for this site, I have 502 tex files of which 306 match usepackage{tikz}. So I'm a little TikZ-obsessed! And so a straightforward diagram like that really shouldn't take me much time, otherwise I've been wasting a heck of a lot of time learning TikZ.

Rather, I'm going to explain why I use TikZ instead of a graphical package such as Inkscape. I think my reasoning is actually why I use a programming system rather than a graphical system, so would equally apply to PSTricks or other such.

  1. Precision I'm a bit fanatical about getting stuff in the actual right place, not just what looks okay. Your diagram would look not so polished if the Maps didn't line up exactly - it might not be obvious why, but looking at it one would have a feeling of it not being quite right. It's easier to program precision than drag-and-drop it.

  2. Repetition There's a lot that's the same in the diagram. Lots of pieces have the same style, the same layout. Again, it's easier to program these than drag-and-drop them. I expect that programs like Inkscape have lots of shortcuts for "apply the same style to all of these", but I'd also imagine that it's easy to miss out one aspect, or include one aspect too many.

  3. Variation I'm never happy with how things turn out the first time. I frequently want to change little things until I'm happy with them. Being able to change just one thing and have a whole lot depend on that one change makes this much easier. So defining a style and changing that, or basing a sub-diagram at a particular coordinate, makes it easier to vary things until I'm happy.

  4. Repetition I do a lot of diagrams, but not so many that I'm a graphical designer. Every time that I do a new diagram there's a certain amount of "I've done something like this before, how did I do it?" with subsequent cut-and-pasting. When I use a graphical system then all of the "How did I do it?" information is lost. I can cut-and-paste actual objects, but it's rarely the objects that I want to copy - it's the "how did I get that particular effect" that I want to copy. When programming a diagram, all of this is laid out in an easily copy-able form.

For me, this last one is the reason TikZ fits so well into my workflow. It's the same with TeX itself. Every so often I find myself sitting in front of OpenOffice (or worse) and thinking, "How did I get the footer to stay on the bottom last time? Yet again it's floating in the middle of the page!". Then I have to remember the exact sequence of menus and options to do it. With TeX then I look back at the old document where this happened, find the \def\footerlocation{bottom}, and copy it over. That is the real time-saver for me.

In conclusion, if you've only one or a small number of diagrams to do then the time saved/time spent lines may not cross for you. But if it's something that you'll do a lot, consider time learning to program your diagrams time well spent.


As this question is proving so popular, I thought I'd add a little more. Specifically, examples. A little while ago, I uploaded all the pictures I'd generated using TikZ for this site to Picassa (at least, I think they all use TikZ. Certainly most of them do.). You can see them at this link: https://plus.google.com/photos/110765980098077923527/albums/5659314316046158689?banner=pwa. A particular picture, which isn't there, which I think demonstrates where something like TikZ is essential is the following:

Rings of rings

Other examples abound in The Enchanted Forest.

15
  • 4
    Excellent explanation! I'd replace "4. Repetition" by "4. Reuse". Commented Apr 19, 2012 at 10:47
  • 28
    @StephanLehmke But that would spoil the (appalling) pun ... Commented Apr 19, 2012 at 10:50
  • 1
    @CaetanoSauer (I will confess to merely repeating a pun I learnt elsewhere). To your follow-ups: 1. Because it's what I know and I'm a one-trick-pony. 2. No idea. I have no intention of finding out! Seriously. I'm the sort of person who would rather figure out how to get TeX to output Markdown than write in Markdown. So I will use TikZ - because that's what I know - rather than use any other system if at all possible. Fortunately, I've yet to find a circumstance in which I couldn't use TikZ to achieve what I wanted. Commented Apr 19, 2012 at 11:41
  • 7
    Another argument for TikZ is that it provides a human-readable, self-descriptive textual representation of your diagram, making it very well-suited to version control. Commented Apr 19, 2012 at 14:18
  • 11
    Link to the Enchanted Forest no longer works.
    – anderstood
    Commented Oct 6, 2016 at 16:27
66

Andrew stated some very good reasons to use TikZ for such a diagram and similar material. I want to make a counterargument that there are a number of reasons to use other tools, such as Inkscape, Adobe Illustrator, online collaboration tools (Lucidchart and others) or any of the large number of illustration/drawing software packages.

The main reasons to avoid TikZ or similar 'graphics programming' languages are

  1. Precision/productivity tradeoff

    While alignment of elements, the exact same dimensions and font sizes are really what every designer should aim for it often does not matter so much in the end. I did a fair amount of CAD so I love when everything is aligned absolutely perfectly my colleagues often can't distinguish between some 'quick hack' and a perfectly aligned figure. The quick version, as you stated, takes 30 minutes, a precise version can take several hours.

  2. Interoperability

    Nobody outside of the small TeX-community has ever heard of TiKZ. So if you want to give your diagram to somebody and they want to modify anything you have to do it yourself. This is also true for the other direction: if I find some nice design I can copy&paste it easily into a graphics software and in the case of a vector format take it apart, modify and play with it. Even the well written examples from Andrew here take me several minutes to read and understand how he achieved certain tricks, let alone use them myself.

  3. Interactive design

    Often with only a vague idea of how it might look 'right' I push and pull things around in a diagram, modify the linewidth, angle of curves or try some fancy effect and the most often used key combination is CTRL+Z to undo the last operation. To go through a compile cycle every time really slows you down a lot if you do not know beforehand how it should look like.

  4. Learning curve

    This is something that is hard to admit. I do not like to read manuals, almost all of my colleagues never consult any manual. Now TikZ comes with a well written and beautiful manual. Still, without it you can do absolutely nothing in TikZ. I recently started doing some small stuff in Adobe Illustrator and it also took me a little while to figure out the basics but I feel reasonably productive with it, and I only had to consult the manual for more exotic things or using the builtin Javascript language to automate certain tasks.

One advantage of TikZ that can be invaluable: try with any other software to create a diagram like yours with changing datasets, i.e. a flexible number of elements, labels and numbers. It is painful and can hardly be automated.

5
  • 7
    Really nice answer! However, regarding the interoperability: You can just give the PDF of your TikZ image to someone else to modify. Inkscape or Adobe Illustrator (or, I guess, most vector drawing programs?) can easily open and edit PDF drawings.
    – Jake
    Commented Apr 19, 2012 at 15:51
  • 4
    Let me add one more expansion of (2) that applies even if you are in a community of tex users. None of my collaborators have working tikz installations as part of their latex distributions, and are unwilling to fix this. Most of them are happy using inkscape, but are afraid of my tikz diagrams. This means that if I start making diagrams in tikz, I become the designated diagram person on the project.
    – Sam Lisi
    Commented Apr 19, 2012 at 22:53
  • In order to create this kind of diagrams wouldn't it be easier to use (and learn) the package smartdiagram?
    – skan
    Commented Dec 2, 2016 at 10:08
  • +1 I'm very jealous of people working in fields where Tikz diagram is easy to create. In my field it is extremely complicated, even the simplest diagram requires dramatic solution. See: tex.stackexchange.com/questions/318762/… 5 minute in Power Point = years worth of experience using Tikz. But does Inkscape come with Latex font?
    – Fraïssé
    Commented Jan 19, 2017 at 23:19
  • @Fraïssé I don't know if Inkscape comes with LaTeX fonts, but Adobe Illustrator doesn't. However, you can simply download the LaTex fonts on their ctan over here (or you might even have installed them already with your LaTex installation). This way, I can create my diagrams in Illustrator with the well-known and beautiful LaTeX fonts. (Btw: I use "lmroman10-regular.otf" most of the time out of the fonts)
    – Splines
    Commented Aug 26, 2021 at 13:34
47

Now it's time to practice !

What I want here it's to get something flexible because it's a big graph and I think it would be very useful to scale the result. I want to adapt the dimensions and to modify the style easily.

First we need to analyse your example. We can see seven groups of nodes. The main node is Group. The nodes are linked from the left side to the right side and we can see three wrappers in gray.

The first node is easy to draw

\node[main] (shuffle) {Group}; 

I named this node (shuffle) and I attributed a style named main main/.style={draw,fill=yellow,inner sep=.5em}. I give the value .5em to inner sep because it's more aesthetics when we change the size of the font.

The most important groups of nodes now are Reducer and Mapper. The method is the same for these groups. First we need to decide the distance between (shuffle) and Reducer. I took 48 ex near of 8cm, the distance depends of the size of the font.

update I would like to scale so I made some modifications. It's possible to use (fit) if we use x=...and y=... to scale the picture but in this case I need to use values without units. I write 8 instead of 48 ex etc.

To create the node I used relative coordinate ($(shuffle)+(8,0)$) and I named the node R0,R1+ and R1-. It's easy to understand above + below -.

\node[R] at ($(shuffle)+(8,1)$)    (R1+) {Reduce};
\node[R] at ($(shuffle)+(8, 0)$)   (R0)  {Reduce};
\node[R] at ($(shuffle)+(8,-1)$)   (R1-) {Reduce};   

etc. for the Mapper group.

More complex is the first group S, because we need to use a table inside the nodes, we need to place correctly this table. I used also the relative coordinates and I decide to place the group at 9 ex before the group M. As you can see, the nodes are aligned are have the same width. I decided to use a multipart node. The code is repetitive so I created a macro \tabins

Some remarks : I try to get the same graph, so I needed to make some good choices. To place correctly the table I used [baseline=(Tab.base)it's always the same name for each table but the objects inside this table are on the same baseline that $k_i$. It's possible to use an array here so I don't give other explanations about this macro because inside the code is relative to TeX and not to TikZ. Important is the style S/.style={anchor=east} why this ? It's to align all the node to the right !

For the last nodes, problem are very similar and it's more easy.

Now, I need to draw the wrappers, there are a lot of possibilities to draw the wrappers and I don't know exactly what is the best method (It's a future question). I chose a simple method :

\begin{scope}[on background layer]
    \node[fill=lightgray!50,inner sep = 4mm,fit=(shuffle),label=above:Shuffle] {}; 
\end{scope} 

The problem with `fit', it's for scaling the picture. It was possible to create a macro because the wrapper is used three times. And a macro would be useful in another picture.

How to draw the edges. It's very easy with a \foreach because it's always the same code. I's important to see the difference between -- andto`

The complete code :

% (S) -> (M) -> (V) -> (shuffle) -> (P) -> (R) - (F)
% S for Start F for Final
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{%
  calc,
  fit,
  shapes,
  backgrounds
}
% the next macro is useful to create a table
\newcommand\tabins[3]{%
 \tikz[baseline=(Tab.base)] 
           \node  [rectangle split, 
                   rectangle split parts=3, 
                   draw, 
                   align=right,
                   inner sep=.5em,
                   rectangle split horizontal] (Tab)
                           {\hbox to 4ex{#1}
           \nodepart{two}  {\hbox to 8ex{\hfill #2\$}}  
           \nodepart{three}{\hbox to 3ex{#3}}}; 
}

\begin{document}
\parindent=0pt
\begin{tikzpicture}[%
   %every node/.style={transform shape},% now it's not necessary but good for a poster
  x=1.25cm,y=2cm,  
  font=\footnotesize,
  % every group of nodes have a style except for main, the style is named by a letter
  main/.style={draw,fill=yellow,inner sep=.5em},
  R/.style={draw,fill=purple!40!blue!30,inner sep=.5em},
  M/.style={draw,fill=green!80!yellow,inner sep=.5em},
  S/.style={anchor=east},
  V/.style={anchor=west},
  P/.style={anchor=center},
  F/.style={anchor=west}
  ]

% main node the reference Shuffle 
\node[main] (shuffle) {Group};
%group R reducer
\node[R] at ($(shuffle)+(8,1)$)    (R1+) {Reduce};
\node[R] at ($(shuffle)+(8, 0)$)   (R0)  {Reduce};
\node[R] at ($(shuffle)+(8,-1)$)   (R1-) {Reduce};
% group M Mapper
\node[M] at ($(shuffle)+(-6,+2.5)$)   (M3+)  {Map};
\node[M] at ($(shuffle)+(-6,+ 1.5)$)  (M2+)  {Map};
\node[M] at ($(shuffle)+(-6,+ .5)$)   (M1+)  {Map};
\node[M] at ($(shuffle)+(-6,- .5)$)   (M1-)  {Map};
\node[M] at ($(shuffle)+(-6,- 1.5)$)  (M2-)  {Map};
\node[M] at ($(shuffle)+(-6,-2.5)$)   (M3-)  {Map};
% group S Start the first nodes
\node[S] at ($(M3+)+(-1.5,0)$)  (S3+) {\Big($k_1$,\tabins{4711}{59.90}{NY}\Big)};
\node[S] at ($(M2+)+(-1.5,0)$)  (S2+) {\Big($k_2$,\tabins{4713}{142.99}{CA}\Big)};
\node[S] at ($(M1+)+(-1.5,0)$)  (S1+) {\Big($k_3$,\tabins{4714}{72.00}{NY}\Big)}; 
\node[S] at ($(M1-)+(-1.5,0)$)  (S1-) {\Big($k_4$,\tabins{4715}{108.75}{NY}\Big)}; 
\node[S] at ($(M2-)+(-1.5,0)$)  (S2-) {\Big($k_5$,\tabins{4718}{19.89}{WA}\Big)};  
\node[S] at ($(M3-)+(-1.5,0)$)  (S3-) {\Big($k_6$,\tabins{4719}{36.60}{CA}\Big)};  
% group V  why not
\node[V] at ($(M3+)+(1.5,0)$)  (V3+) {\Big(NY,59.90\$\Big)};
\node[V] at ($(M2+)+(1.5,0)$)  (V2+) {\Big(CA,142.99\$\Big)};
\node[V] at ($(M1+)+(1.5,0)$)  (V1+) {\Big(NY,72.00\$\Big)}; 
\node[V] at ($(M1-)+(1.5,0)$)  (V1-) {\Big(NY,108.75\$\Big)}; 
\node[V] at ($(M2-)+(1.5,0)$)  (V2-) {\Big(WA,19.89\$\Big)};  
\node[V] at ($(M3-)+(1.5,0)$)  (V3-) {\Big(CA,36.60\$\Big)};   

\node[P] at ($(R1+)+(-4,0)$) (P1+) {\Big(CA,\big[142.99\$,36.60\$\big]\Big)};
\node[P] at ($(R0) +(-4,0)$) (P0)  {\Big(NY,\big[59.90\$,72.00\$,108.75\big]\Big)};
\node[P] at ($(R1-)+(-4,0)$) (P1-) {\Big(WA,\big[19.89\$\big]\Big)}; 

\node[F] (F1+) at ($(R1+)+(1.5,0)$) {(CA,89.80\$)};
\node[F] (F0)  at ($(R0) +(1.5,0)$) {(NY,80.22\$)}; 
\node[F] (F1-) at ($(R1-)+(1.5,0)$) {(WA,72.00\$)}; 

% wrappers
\begin{scope}[on background layer]
    \node[fill=lightgray!50,inner sep = 4mm,fit=(shuffle),label=above:Shuffle] {}; 
\end{scope} 
\begin{scope}[on background layer]
    \node[fill=lightgray!50,inner sep = 4mm,fit=(R1+)(R1-),label=above:Reducer] {}; 
\end{scope}  
\begin{scope}[on background layer]
    \node[fill=lightgray!50,inner sep = 4mm,fit=(M3+)(M3-),label=above:Mapper] {}; 
\end{scope}

%edges

\foreach \indice in {3+,2+,1+,1-,2-,3-} \draw[->] (S\indice.east) -- (M\indice.west); 
\foreach \indice in {3+,2+,1+,1-,2-,3-} \draw[->] (M\indice.east) -- (V\indice.west);
\foreach \indice in {3+,2+,1+,1-,2-,3-} \draw[->] (V\indice.east) to [out=0,in=180] (shuffle.west); 
\foreach \indice in {1+,0,1-} \draw[->] (shuffle.east) to [out=0,in=180] (P\indice.west);  
\foreach \indice in {1+,0,1-} \draw[->] (P\indice.east) -- (R\indice.west);
\foreach \indice in {1+,0,1-} \draw[->] (R\indice.east) -- (F\indice.west);   
\end{tikzpicture} 

\end{document}      

Conclusion : 10 minutes to write the code and to get something correct, then some difficulties : the table inside the first column problem with the size and the position but it's a tex's problem not really tikz's problem. The other problem is about the wrapper because, I want to get something useful for my next pictures. Another problem not for me but for a beginner is to understand why (fit) doesn't work with scale ( some answers on this site explains the reason ).

enter image description here

3
  • 1
    Very nice. I will be having my LaTeX students come to this answer to see the analysis process to get a diagram. Commented Apr 20, 2012 at 16:25
  • Great example! I would like to add it to the TeXample TikZ gallery, I just thought of possible improvements: a Parentheses/.style={execute at begin node=\Big(,execute at end node=\Big)} for inheriting by S, V, P, there's just danger for the \tabins node(parts) which would inherit, but could be a scope instead of a tikzpicture (?) or their style resets the specific outer execution style... using such an effective style saving typing and giving consistency could might be even more to learn.
    – Stefan Kottwitz
    Commented Feb 27, 2014 at 9:31
  • In order to create this kind of diagrams wouldn't it be easier to use (and learn) the package smartdiagram?
    – skan
    Commented Dec 2, 2016 at 10:08
24

I just wanted to add another novice voice to the answers. I'm not an expert!

Separation of code and data

One big advantage is the separation between code and data. Suppose that you wanted to add another "mapper" or "reducer" to your drawing. Then, with Tikz, you have it so that the drawing is generated by something akin to a function call whose parameters are the values you've written out. This can let you try a lot of variations quickly and effortlessly. (This is a very convincing demo of Tikz for others, in my experience.)

Exact representation

Suppose that you're making a diagram in which some elements have to be exact at positions. I don't know what your click-and-drag software is capable of, but Tikz definitely does that.

Version control

If you're already using version control (backup that lets you see differences with old versions) then because Tikz is text-based, changes that you make to your diagrams are available in version control.

Comments

I don't know how you add explanatory comments to a diagram, but you can embed them right in the tikz code.

Community

Tikz is not easy to learn and there's a lot to it, but we are so lucky to have the tex.stackexchange community who answer our questions. After you've given your problem a really good effort, then I've found this community to be really solid at answering questions.

Diagram generation

Tikz is really powerful. It has mathematical evaluation, loops, and some recursion to help you generate complicated diagrams from relatively little code. (If you need real function calls and lambdas and so on, then you probably have to go to something like Python + matplotlib. One day we will hopefully have a Python module that supplants Tikz.)

Anyway, like I said, I'm no expert, but my experience has shown Tikz to be really useful in illustrating my work.

4
  • As for the last point of yours, PiScript is a nice tool when it comes to complicated programing which is (at least for me) hard to implement in TikZ.
    – Dror
    Commented Apr 20, 2012 at 10:26
  • @Dror: Wow!! Thanks for sharing that. That's exactly the kind of thing I was looking for. Now, I'm hoping that piscript becomes as powerful as tikz.
    – Neil G
    Commented Apr 20, 2012 at 10:41
  • 1
    Another potential benefit is that TikZ, as with other software discussed on this site, has lower demands on computing hardware when compared with graphical tools.
    – mas
    Commented Apr 25, 2012 at 8:38
  • The separation of code and data is an important point. I often set up my axes etc. as I like and then keep my data in a separate file written straight out from Mathematica. When I want to plot a different data set the only part of the code that needs to change is the \input{} that points to the file containing the data.
    – Ubiquitous
    Commented Aug 13, 2013 at 12:55
14

I'm going to answer as a longtime user of PStricks. Everything I have to say applies to TikZ as well, but the learning curve for PStricks is longer and steeper than the learning curve for TikZ. (I have done a few TikZ things, but I already know PStricks, and while TikZ is better, it is not enough better to get me to switch.)

Would it pay off for me to learn and understand all principles and techniques of PGF/TikZ?

Probably not. Focus on coordinates, nodes, and paths and you will be able to make a lot of useful drawings.

Would you, as an expert TikZ user, draw a simple diagram like the one below faster than a regular click-and-drag user?

I don't know how fast regular users are. But, I hate "What you see is all you get". I might get an OK diagram quickly, but when I have to get the diagram right, I need to be able to program it. And when I have a group of diagrams that go through multiple revisions, I really benefit from the programmability of TikZ and PStricks.

There's a larger question here, which is what to do about diagrams. Here's what I do:

  • If I need an adequate diagram quickly, I try to get dot to draw it for me.

  • If that doesn't work, usually the quickest solution is to scribble something by hand and use a scan or photograph.

  • If the drawing has to be right, I will almost always use a program to create it. Usually PStricks, sometimes TikZ, sometimes something more specialized.

You may find it useful to know about ktikz and qtikz, which are respectively KDE and QT editors for TikZ diagrams. Perhaps they offer the best of both worlds?

13

I'm not an expert. I haven't learned all of TikZ. However, when I do produce documents, I do the graphics in TikZ. (For other situations I pen it in hand or use computer drawing software - mostly hand however)

Aspects such as precision, reusability and configurability has already been mentioned, but what I like to benefit from is automation. For example, in one project I several data sets that needed to be plotted. Since TikZ code is just text I was able to automate the generation of these graphics.

1
  • 1
    Isn't tikz to programming languages what latex is also ? No type system etc... if you do need a programming language, it's hard to believe there aren't higher level ones with nice semantic to compile to tikz. Just thinking out loud here
    – nicolas
    Commented Feb 1, 2017 at 7:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .