Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
8  
TikZedt seems to be something that you might enjoy for not-so-complicated drawings. –  percusse Apr 19 '12 at 11:56
4  
Instantly seeing the drawing you are typing is indeed very nice and productive, even for professional TikZers. Too bas it's windows-only. –  Caetano Sauer Apr 19 '12 at 15:51
3  
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. –  Loop Space Apr 19 '12 at 17:38
4  
For Linux you also have KTikZ –  percusse Apr 19 '12 at 22:18
1  
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 Apr 20 '12 at 6:25

6 Answers 6

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.

share|improve this answer
2  
Excellent explanation! I'd replace "4. Repetition" by "4. Reuse". –  Stephan Lehmke Apr 19 '12 at 10:47
9  
@StephanLehmke But that would spoil the (appalling) pun ... –  Loop Space Apr 19 '12 at 10:50
    
Thank you for the excellent answer, Andrew! (nice pun btw) Now let me ask you a small follow-up (ok maybe two). The advantages you described apply to "diagram programming" in general. Besides the TeX integration, is there any reason why TikZ is most productive in particular? Also, are the concepts of diagram programming common to all such tools, to the point where a TikZ user could rapidly switch to another one if need be? –  Caetano Sauer Apr 19 '12 at 11:15
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. –  Loop Space Apr 19 '12 at 11:41
4  
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. –  Will Vousden Apr 19 '12 at 14:18

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.

share|improve this answer
2  
+1. I especially love the backhanded compliment in (2). –  Loop Space Apr 19 '12 at 12:54
4  
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 Apr 19 '12 at 15:51
2  
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 Apr 19 '12 at 22:53

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

share|improve this answer
    
Very nice. I will be having my LaTeX students come to this answer to see the analysis process to get a diagram. –  R. Schumacher Apr 20 '12 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 Feb 27 at 9:31

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 Turing-complete Tikz…)

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

share|improve this answer
    
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 Apr 20 '12 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 Apr 20 '12 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 Apr 25 '12 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 Aug 13 '13 at 12:55

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?

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.