[Tex/LaTex] Better way to calculate coordinates in Tikz


I am great fan of pgf and tikz in general to produce great looking figures. But I don't like the amount of time I have to put in to draw a complicated picture, because I have to provide all the coordinates of the points lines etc. Is there any wrapper which makes this simpler? Like in some cases, eg when you have to put text over or around arrows, there are ways to give relative position instead of exact coordinates. Are there any similar ways to draw other shapes with relative coordinates? Is there any external program that can convert figures with lines and shapes into coordinates?

Best Answer

Complete LaTeX documents used to generate the figures in this answer are available as a Gist on GitHub.

One way to solve the "putting text over arrows" question is to use inline nodes and relative positioning:

  \draw[->] (0,0) -- (5,0) node[above]{Hello, world!};

Example of using relative positioning in TikZ

You can also use named nodes and coordinates combined with relative positioning. A \coordinate is just like a \node without a text area (they may still have shapes). Naming your nodes and coordinates allows you to re-use information about their position without re-typing the locations. Using relative positioning constructs (provided by the TikZ positioning library), like left = 1ex of A, allows you to position nodes/coordinates relative to other nodes/coordinates without working out the mathematics and geometry in your head.

  Name/.style = {font={\bfseries}}

  \coordinate (A) at (0,0);
  %            ^---- A name for the node/coordinate
  \coordinate (B) at (5,1);
  \coordinate (C) at (3,4);

  \draw (A) -- (B) -- (C)  -- cycle;

  \node (A-label) [left=1ex of A, Name] {A};
  \node (B-label) [right=1ex of B, Name] {B};
  \node (C-label) [above=1ex of C, Name] {C};

  \node [above left = 2 em of C] {This is a relatively positioned node!}
    edge[out=270,in=180,thick,->] (C-label);


Demonstration of named coordinates and relative positioning.

See Tutorial 2 "A Petri-Net for Hagen" in the TikZ manual for an excellent step-by-step walkthrough of named nodes and relative positioning.

I prefer constructing figures using relative positioning as opposed to using a GUI program because I can change the look of the figure by adjusting the locations of a few "base coordinates". In this case, I could draw a completely different triangle by moving the locations of coordinates A, B and C and for most adjustments the rest of the figure would just fall into place. Learning to set up figures like this takes practice but once you get the it down it can save a bunch of time when it comes to fine-tuning the image when compared to a GUI program.

Finally, as to your question about GUI programs, I would recommend Inkscape- it is a very robust, open source editor for vector graphics that is similar to Adobe Illustrator. There is even a plugin available that generates TikZ code from Inkscape graphics.

Related Question