📜 ⬆️ ⬇️

Charts and graphs in LaTeX using PGF / TikZ 3.0



A few months ago, a graphic package for LaTeX PGF / TikZ 3.0 was released, and many interesting pieces appeared in it. In this article, we will try to apply them to draw a simple flowchart. Let's draw, for example, a piece of the well - known scheme for determining the language by writing . We will not touch the means already discussed in a previously published article , but let's talk about simplified notation for writing graphs and controlling node positioning and graph branching.


')
Simplified Graph Notation


The standard commands for drawing nodes and lines in TikZ are \node and \path , but the code with them is quite verbose and you can lose the diagram itself behind the fence from the \node commands. In TikZ 3.0, a simplified notation for graphs appeared, borrowed from the famous Graphviz package and its DOT language . In the DOT notation, the simplest graph can be written as a sequence of text labels and pseudo arrows, like a -> b -> c .



Let's start with the preamble:
 \usepackage{tikz} \usetikzlibrary{graphs} 


And make a simple graph:
  \begin{tikzpicture} \graph { ? -> ! ->  }; \end{tikzpicture} 


The command \graph in its argument takes the description of the graph in the DOT notation, and we assume that we will get a chain of three vertices. In reality, not everything is so simple: our tags got lost in the Kuchamalu (point 1 in the picture “The Chain of Summits”)

You can position the nodes of the graph manually, and we will deal with this in the next part, but for now let's try automatic positioning. The simplest thing you can do is tell TikZ in the options of the \graph command \graph should grow and where to branch. Let's grow the graph to the right, so that the centers of the nodes are located on the grid in increments of three centimeters (point 2):

  \begin{tikzpicture} \graph[grow right=3cm] { ? -> ! ->  }; \end{tikzpicture} 


You can specify the distance not between the centers, but between the adjacent edges of the nodes (point 3):

  \begin{tikzpicture} \graph[grow right sep=2em] { ? -> ! ->  }; \end{tikzpicture} 


Count can grow in any direction. The standard directions are right, left, up, down, orthogonal to the axes of coordinates, but you can also grow at an angle (point 4):

  \begin{tikzpicture} \graph[chain shift=(-45:1)] { ? -> ! ->  }; \end{tikzpicture} 


Free text in tags



If the label has, for example, a hyphen, or something else more or less complex (mathematical formula?), Then they will not understand us without additional hints, but if you quote it, then everything will be ok:
  \begin{tikzpicture} \graph[grow right sep=1em] { ? ->   E:\\ \`{E}, \'{E}, \^{E}, \"{E} ->  }; \end{tikzpicture} \begin{tikzpicture} \graph[grow right sep=1em] { ? -> "  E:\\ \`{E}, \'{E}, \^{E}, \"{E}" ->  }; \end{tikzpicture} 


Graph branching


Let's make our scheme more complicated and add branching. In the DOT-notation, nodes can be grouped into groups using curly brackets and an arc from the ancestor node can be drawn to each of the group nodes:

  \begin{tikzpicture} %%  nodes     . align=center     \graph[nodes={align=center}, grow down sep, branch right sep] { ? -> { "  E:\\ \`{E}, \'{E}, \^{E}, \"{E}" -> ,  -> " - ?" -> { "\c{C}  \"{E}" ->   ? -> { "$\dots$" -> ,   ->  }, " \"{A}  \"{O} \\   \\ " ->  } } }; \end{tikzpicture} 




As you can see, from the question nodes, the arcs were drawn to the corresponding answer nodes. For the location of nodes during branching, the branch parameter is responsible, in our case, right sep says that the branching should go to the right, with the same distance between the layers. It can take other values, similar to the grow parameter. By the way, we needed to specify the alignment of the text in the nodes, without which line breaks in the labels would not work

But we seem to have a problem. The arc from the node "Oh ... he" to the conclusion "French" was spent, but went somewhere upwards. Is it possible to make the conclusion that “French” was lower than all the questions and answers that preceded it? If it is naive to place the conclusion “French” after the whole group after the question “Diacritic?” Then arcs will be drawn from all the leaves of the group:


  \begin{tikzpicture} %%      \graph[nodes={align=center,rectangle,draw=black}, grow down sep, branch right sep] { ? -> { "  E:\\ \`{E}, \'{E}, \^{E}, \"{E}",  -> " - ?" -> { "\c{C}  \"{E}" ->   ? -> { "$\dots$",   ->  }, " \"{A}  \"{O} \\   \\ " ->  } } ->  }; \end{tikzpicture} 



But you can explicitly exclude leaves from the list of ends of arcs by adding the options not source and not target . Their names are somewhat contradictory: in order to indicate that an arc should not go from the “Albanian” node to the “French” node, the [not target] option should be assigned to the “Albanian” node

  \begin{tikzpicture} \graph[nodes={align=center,rectangle,draw=black}, grow down sep, branch right sep] { ? -> { "  E:\\ \`{E}, \'{E}, \^{E}, \"{E}",  -> " - ?" -> { "\c{C}  \"{E}" ->   ? -> { "$\dots$",   -> [not target] }, " \"{A}  \"{O} \\   \\ " -> [not target] } } ->  }; \end{tikzpicture} 


Perhaps, for the first part is enough, and in the following parts you can consider other node positioning strategies and options for using DOT notation.

References:
[1] The source text of the diagrams from the article and the compiled result
[2] PGF / TikZ package

Source: https://habr.com/ru/post/224501/


All Articles