diff options
Diffstat (limited to 'Master/texmf-dist/doc/generic/pgf/text-en/pgfmanual-en-tutorial-chains.tex')
-rw-r--r-- | Master/texmf-dist/doc/generic/pgf/text-en/pgfmanual-en-tutorial-chains.tex | 525 |
1 files changed, 267 insertions, 258 deletions
diff --git a/Master/texmf-dist/doc/generic/pgf/text-en/pgfmanual-en-tutorial-chains.tex b/Master/texmf-dist/doc/generic/pgf/text-en/pgfmanual-en-tutorial-chains.tex index e6b05fd6bd7..c6ba16ff87f 100644 --- a/Master/texmf-dist/doc/generic/pgf/text-en/pgfmanual-en-tutorial-chains.tex +++ b/Master/texmf-dist/doc/generic/pgf/text-en/pgfmanual-en-tutorial-chains.tex @@ -10,17 +10,18 @@ \section{Tutorial: Diagrams as Simple Graphs} -In this tutorial we have a look at how graphs and matrices can be used -to typeset a diagram. - -Ilka, who just got tenure for her professorship on Old and -Lovable Programming Languages, has recently dug up a technical report entitled -\emph{The Programming Language Pascal} in the dusty cellar of the -library of her university. Having been created in the good old times -using pens and rules, it looks like this\footnote{The shown diagram was not scanned, but - rather typeset using \tikzname. The jittering lines were created - using the |random steps| decoration.}: - +In this tutorial we have a look at how graphs and matrices can be used to +typeset a diagram. + +Ilka, who just got tenure for her professorship on Old and Lovable Programming +Languages, has recently dug up a technical report entitled \emph{The +Programming Language Pascal} in the dusty cellar of the library of her +university. Having been created in the good old times using pens and rules, it +looks like this% +\footnote{The shown diagram was not scanned, but rather typeset using +\tikzname. The jittering lines were created using the |random steps| +decoration.}: +% \tikzset{ nonterminal/.style={ % The shape: @@ -99,9 +100,8 @@ using pens and rules, it looks like this\footnote{The shown diagram was not scan \end{tikzpicture} \medskip -For her next lecture, Ilka decides to redo this diagram, but this time -perhaps a bit cleaner and perhaps also bit ``cooler.'' - +For her next lecture, Ilka decides to redo this diagram, but this time perhaps +a bit cleaner and perhaps also bit ``cooler''. \medskip \noindent\begin{tikzpicture}[point/.style={coordinate},>=stealth',thick,draw=black!50, @@ -155,29 +155,27 @@ perhaps a bit cleaner and perhaps also bit ``cooler.'' \end{tikzpicture} }\medskip -Having read the previous tutorials, Ilka knows already how to set up -the environment for her diagram, namely using a |tikzpicture| -environment. She wonders which libraries she will need. She decides -that she will postpone the decision and add the necessary libraries as -needed as she constructs the picture. +Having read the previous tutorials, Ilka knows already how to set up the +environment for her diagram, namely using a |tikzpicture| environment. She +wonders which libraries she will need. She decides that she will postpone the +decision and add the necessary libraries as needed as she constructs the +picture. \subsection{Styling the Nodes} -The bulk of this tutorial will be about arranging the nodes and -connecting them using chains, but let us start with setting up styles -for the nodes. - -There are two kinds of nodes in the diagram, namely what theoreticians -like to call \emph{terminals} and \emph{nonterminals}. For the -terminals, Ilka decides to use a black color, which visually shows -that ``nothing needs to be done about them.'' The nonterminals, which -still need to be ``processed'' further, get a bit of red mixed in. - -Ilka starts with the simpler nonterminals, as there are no rounded -corners involved. Naturally, she sets up a style: +The bulk of this tutorial will be about arranging the nodes and connecting them +using chains, but let us start with setting up styles for the nodes. +There are two kinds of nodes in the diagram, namely what theoreticians like to +call \emph{terminals} and \emph{nonterminals}. For the terminals, Ilka decides +to use a black color, which visually shows that ``nothing needs to be done +about them''. The nonterminals, which still need to be ``processed'' further, +get a bit of red mixed in. +Ilka starts with the simpler nonterminals, as there are no rounded corners +involved. Naturally, she sets up a style: +% \begin{codeexample}[] \begin{tikzpicture}[ nonterminal/.style={ @@ -198,20 +196,20 @@ corners involved. Naturally, she sets up a style: \node [nonterminal] {unsigned integer}; \end{tikzpicture} \end{codeexample} -Ilka is pretty proud of the use of the |minimum size| option: As the -name suggests, this option ensures that the node is at least 6mm by -6mm, but it will expand in size as necessary to accommodate longer -text. By giving this option to all nodes, they will all have the same -height of 6mm. - -Styling the terminals is a bit more difficult because of the round -corners. Ilka has several options how she can achieve them. One way -is to use the |rounded corners| option. It gets a dimension as -parameter and causes all corners to be replaced by little arcs with -the given dimension as radius. By setting the radius to 3mm, she will -get exactly what she needs: circles, when the shapes are, indeed, -exactly 6mm by 6mm and otherwise half circles on the sides: - +% +Ilka is pretty proud of the use of the |minimum size| option: As the name +suggests, this option ensures that the node is at least 6mm by 6mm, but it will +expand in size as necessary to accommodate longer text. By giving this option +to all nodes, they will all have the same height of 6mm. + +Styling the terminals is a bit more difficult because of the round corners. +Ilka has several options how she can achieve them. One way is to use the +|rounded corners| option. It gets a dimension as parameter and causes all +corners to be replaced by little arcs with the given dimension as radius. By +setting the radius to 3mm, she will get exactly what she needs: circles, when +the shapes are, indeed, exactly 6mm by 6mm and otherwise half circles on the +sides: +% \begin{codeexample}[] \begin{tikzpicture}[node distance=5mm, terminal/.style={ @@ -227,11 +225,12 @@ exactly 6mm by 6mm and otherwise half circles on the sides: \end{tikzpicture} \end{codeexample} -Another possibility is to use a shape that is specially made for -typesetting rectangles with arcs on the sides (she has to use the -|shapes.misc| library to use it). This shape gives Ilka -much more control over the appearance. For instance, she could have an -arc only on the left side, but she will not need this. +Another possibility is to use a shape that is specially made for typesetting +rectangles with arcs on the sides (she has to use the |shapes.misc| library to +use it). This shape gives Ilka much more control over the appearance. For +instance, she could have an arc only on the left side, but she will not need +this. +% \begin{codeexample}[] \begin{tikzpicture}[node distance=5mm, terminal/.style={ @@ -247,8 +246,10 @@ arc only on the left side, but she will not need this. \node (E) [terminal,right=of digit] {E}; \end{tikzpicture} \end{codeexample} -At this point, she notices a problem. The baseline of the text in the -nodes is not aligned: +% +At this point, she notices a problem. The baseline of the text in the nodes is +not aligned: +% \begin{codeexample}[] \begin{tikzpicture}[node distance=5mm] \node (dot) [terminal] {.}; @@ -263,19 +264,20 @@ nodes is not aligned: (-.5,\y3) -- (3.5,\y3); \end{tikzpicture} \end{codeexample} -\noindent (Ilka has moved the style definition to the preamble by -saying |\tikzset{terminal/.style=...}|, so that she can use it in all -pictures.) +% +\noindent (Ilka has moved the style definition to the preamble by saying +|\tikzset{terminal/.style=...}|, so that she can use it in all pictures.) For the |digit| and the |E| the difference in the baselines is almost -imperceptible, but for the dot the problem is quite severe: It looks -more like a multiplication dot than a period. +imperceptible, but for the dot the problem is quite severe: It looks more like +a multiplication dot than a period. Ilka toys with the idea of using the |base right=of...| option rather than -|right=of...| to align the nodes in such a way that the baselines -are all on the same line (the |base right| option places a node -right of something so that the baseline is right of the baseline of -the other object). However, this does not have the desired effect: +|right=of...| to align the nodes in such a way that the baselines are all on +the same line (the |base right| option places a node right of something so that +the baseline is right of the baseline of the other object). However, this does +not have the desired effect: +% \begin{codeexample}[] \begin{tikzpicture}[node distance=5mm] \node (dot) [terminal] {.}; @@ -283,14 +285,15 @@ the other object). However, this does not have the desired effect: \node (E) [terminal,base right=of digit] {E}; \end{tikzpicture} \end{codeexample} -The nodes suddenly ``dance around''! There is no hope of changing the -position of text inside a node using anchors. Instead, Ilka must use a -trick: The problem of mismatching baselines is caused by the fact that -|.| and |digit| and |E| all have different heights and depth. If they -all had the same, they would all be positioned vertically in the same -manner. So, all Ilka needs to do is to use the |text height| and -|text depth| options to explicitly specify a height and depth for the -nodes. +% +The nodes suddenly ``dance around''! There is no hope of changing the position +of text inside a node using anchors. Instead, Ilka must use a trick: The +problem of mismatching baselines is caused by the fact that |.| and |digit| and +|E| all have different heights and depth. If they all had the same, they would +all be positioned vertically in the same manner. So, all Ilka needs to do is to +use the |text height| and |text depth| options to explicitly specify a height +and depth for the nodes. +% \begin{codeexample}[] \begin{tikzpicture}[node distance=5mm, text height=1.5ex,text depth=.25ex] @@ -301,34 +304,35 @@ nodes. \end{codeexample} - \subsection{Aligning the Nodes Using Positioning Options} -Ilka now has the ``styling'' of the nodes ready. The next problem is -to place them in the right places. There are several ways to do -this. The most straightforward is to simply explicitly place the nodes -at certain coordinates ``calculated by hand.'' For very simple -graphics this is perfectly alright, but it has several disadvantages: +Ilka now has the ``styling'' of the nodes ready. The next problem is to place +them in the right places. There are several ways to do this. The most +straightforward is to simply explicitly place the nodes at certain coordinates +``calculated by hand''. For very simple graphics this is perfectly alright, but +it has several disadvantages: +% \begin{enumerate} -\item For more difficult graphics, the calculation may become - complicated. -\item Changing the text of the nodes may make it necessary to - recalculate the coordinates. -\item The source code of the graphic is not very clear since the - relationships between the positions of the nodes are not made - explicit. + \item For more difficult graphics, the calculation may become + complicated. + \item Changing the text of the nodes may make it necessary to recalculate + the coordinates. + \item The source code of the graphic is not very clear since the + relationships between the positions of the nodes are not made + explicit. \end{enumerate} -For these reasons, Ilka decides to try out different ways of arranging -the nodes on the page. +For these reasons, Ilka decides to try out different ways of arranging the +nodes on the page. -The first method is the use of \emph{positioning options}. To use -them, you need to load the |positioning| library. This gives you -access to advanced implementations of options like |above| or |left|, -since you can now say |above=of some node| in order to place a node -above of |some node|, with the borders separated by |node distance|. +The first method is the use of \emph{positioning options}. To use them, you +need to load the |positioning| library. This gives you access to advanced +implementations of options like |above| or |left|, since you can now say +|above=of some node| in order to place a node above of |some node|, with the +borders separated by |node distance|. Ilka can use this to draw the place the nodes in a long row: +% \tikzset{terminal/.append style={text height=1.5ex,text depth=.25ex}} \tikzset{nonterminal/.append style={text height=1.5ex,text depth=.25ex}} @@ -344,17 +348,17 @@ Ilka can use this to draw the place the nodes in a long row: \end{tikzpicture} \end{codeexample} -For the plus and minus nodes, Ilka is a bit startled by their -placements. Shouldn't they be more to the right? The reason they are -placed in that manner is the following: The |north east| anchor of the -|E| node lies at the ``upper start of the right arc,'' which, a bit -unfortunately in this case, happens to be the top of the -node. Likewise, the |south west| anchor of the |+| node is actually at -its bottom and, indeed, the horizontal and vertical distances between -the top of the |E| node and the bottom of the |+| node are both 5mm. - -There are several ways of fixing this problem. The -easiest way is to simply add a little bit of horizontal shift by hand: +For the plus and minus nodes, Ilka is a bit startled by their placements. +Shouldn't they be more to the right? The reason they are placed in that manner +is the following: The |north east| anchor of the |E| node lies at the ``upper +start of the right arc'', which, a bit unfortunately in this case, happens to +be the top of the node. Likewise, the |south west| anchor of the |+| node is +actually at its bottom and, indeed, the horizontal and vertical distances +between the top of the |E| node and the bottom of the |+| node are both 5mm. + +There are several ways of fixing this problem. The easiest way is to simply add +a little bit of horizontal shift by hand: +% \begin{codeexample}[] \begin{tikzpicture}[node distance=5mm and 5mm] \node (E) [terminal] {E}; @@ -364,9 +368,10 @@ easiest way is to simply add a little bit of horizontal shift by hand: \end{tikzpicture} \end{codeexample} -A second way is to revert back to the idea of using a normal rectangle -for the terminals, but with rounded corners. Since corner rounding -does not affect anchors, she gets the following result: +A second way is to revert back to the idea of using a normal rectangle for the +terminals, but with rounded corners. Since corner rounding does not affect +anchors, she gets the following result: +% \begin{codeexample}[] \begin{tikzpicture}[node distance=5mm and 5mm,terminal/.append style={rectangle,rounded corners=3mm}] \node (E) [terminal] {E}; @@ -375,15 +380,16 @@ does not affect anchors, she gets the following result: \node (ui2) [nonterminal,below right=of plus] {unsigned integer}; \end{tikzpicture} \end{codeexample} +% A third way is to use matrices, which we will do later. -Now that the nodes have been placed, Ilka needs to add -connections. Here, some connections are more difficult than -others. Consider for instance the ``repeat'' line around the -|digit|. One way of describing this line is to say ``it starts a -little to the right of |digit| than goes down and then goes to the -left and finally ends at a point a little to the left of |digit|.'' -Ilka can put this into code as follows: +Now that the nodes have been placed, Ilka needs to add connections. Here, some +connections are more difficult than others. Consider for instance the +``repeat'' line around the |digit|. One way of describing this line is to say +``it starts a little to the right of |digit| than goes down and then goes to +the left and finally ends at a point a little to the left of |digit|''. Ilka +can put this into code as follows: +% \begin{codeexample}[] \begin{tikzpicture}[node distance=5mm and 5mm] \node (dot) [terminal] {.}; @@ -405,11 +411,12 @@ Ilka can put this into code as follows: \end{tikzpicture} \end{codeexample} -Since Ilka needs this ``go up/down then horizontally and then up/down -to a target'' several times, it seems sensible to define a special -\emph{to-path} for this. Whenever the |edge| command is used, it -simply adds the current value of |to path| to the path. So, Ilka can -set up a style that contains the correct path: +Since Ilka needs this ``go up/down then horizontally and then up/down to a +target'' several times, it seems sensible to define a special \emph{to-path} +for this. Whenever the |edge| command is used, it simply adds the current value +of |to path| to the path. So, Ilka can set up a style that contains the correct +path: +% \begin{codeexample}[] \begin{tikzpicture}[node distance=5mm and 5mm, skip loop/.style={to path={-- ++(0,-.5) -| (\tikztotarget)}}] @@ -424,11 +431,11 @@ set up a style that contains the correct path: \end{tikzpicture} \end{codeexample} -Ilka can even go a step further and make her |skip look| style -parameterized. For this, the skip loop's vertical offset is passed as -parameter |#1|. Also, in the following code Ilka specifies the start -and targets differently, namely as the positions that are ``in the -middle between the nodes.'' +Ilka can even go a step further and make her |skip look| style parameterized. +For this, the skip loop's vertical offset is passed as parameter |#1|. Also, in +the following code Ilka specifies the start and targets differently, namely as +the positions that are ``in the middle between the nodes''. +% \begin{codeexample}[] \begin{tikzpicture}[node distance=5mm and 5mm, skip loop/.style={to path={-- ++(0,#1) -| (\tikztotarget)}}] @@ -446,19 +453,19 @@ middle between the nodes.'' \subsection{Aligning the Nodes Using Matrices} -Ilka is still bothered a bit by the placement of the plus and minus -nodes. Somehow, having to add an explicit |xshift| seems too much like -cheating. +Ilka is still bothered a bit by the placement of the plus and minus nodes. +Somehow, having to add an explicit |xshift| seems too much like cheating. -A perhaps better way of positioning the nodes is to use a -\emph{matrix}. In \tikzname\ matrices can be used to align quite -arbitrary graphical objects in rows and columns. The syntax is very -similar to the use of arrays and tables in \TeX\ (indeed, internally -\TeX\ tables are used, but a lot of stuff is going on additionally). +A perhaps better way of positioning the nodes is to use a \emph{matrix}. In +\tikzname\ matrices can be used to align quite arbitrary graphical objects in +rows and columns. The syntax is very similar to the use of arrays and tables in +\TeX\ (indeed, internally \TeX\ tables are used, but a lot of stuff is going on +additionally). -In Ilka's graphic, there will be three rows: One row containing only -the plus node, one row containing the main nodes and one row -containing only the minus node. +In Ilka's graphic, there will be three rows: One row containing only the plus +node, one row containing the main nodes and one row containing only the minus +node. +% \begin{codeexample}[] \begin{tikzpicture} \matrix[row sep=1mm,column sep=5mm] { @@ -476,13 +483,15 @@ containing only the minus node. }; \end{tikzpicture} \end{codeexample} -That was easy! By toying around with the row and columns separations, -Ilka can achieve all sorts of pleasing arrangements of the nodes. +% +That was easy! By toying around with the row and columns separations, Ilka can +achieve all sorts of pleasing arrangements of the nodes. -Ilka now faces the same connecting problem as before. This time, she -has an idea: She adds small nodes (they will be turned into -coordinates later on and be invisible) at all the places -where she would like connections to start and end. +Ilka now faces the same connecting problem as before. This time, she has an +idea: She adds small nodes (they will be turned into coordinates later on and +be invisible) at all the places where she would like connections to start and +end. +% \begin{codeexample}[] \begin{tikzpicture}[point/.style={circle,inner sep=0pt,minimum size=2pt,fill=red}, skip loop/.style={to path={-- ++(0,#1) -| (\tikztotarget)}}] @@ -506,31 +515,30 @@ where she would like connections to start and end. (p2) edge [->,skip loop=5mm] (p6); \end{tikzpicture} \end{codeexample} +% Now, it's only a small step to add all the missing edges. - \subsection{The Diagram as a Graph} -Matrices allow Ilka to align the nodes nicely, but the connections are -not quite perfect. The problem is that the code does not really -reflect the paths that underlie the diagram. For this, it seems -natural enough to Ilka to use the |graph| library since, after all, -connecting nodes by edges is exactly what happens in a graph. -The |graph| library can both be used to connect nodes that have -already been created, but it can also be used to create nodes ``on the +Matrices allow Ilka to align the nodes nicely, but the connections are not +quite perfect. The problem is that the code does not really reflect the paths +that underlie the diagram. For this, it seems natural enough to Ilka to use the +|graph| library since, after all, connecting nodes by edges is exactly what +happens in a graph. The |graph| library can both be used to connect nodes that +have already been created, but it can also be used to create nodes ``on the fly'' and these processes can also be mixed. \subsubsection{Connecting Already Positioned Nodes} -Ilka has already a fine method for positioning her nodes (using a -|matrix|), so all that she needs is an easy way of specifying the -edges. For this, she uses the |\graph| command (which is actually just -a shorthand for |\path graph|). It allows her to write down edges -between them in a simple way (the macro |\matrixcontent| contains -exactly the matrix content from the previous example; no need to -repeat it here): +Ilka has already a fine method for positioning her nodes (using a |matrix|), so +all that she needs is an easy way of specifying the edges. For this, she uses +the |\graph| command (which is actually just a shorthand for |\path graph|). It +allows her to write down edges between them in a simple way (the macro +|\matrixcontent| contains exactly the matrix content from the previous example; +no need to repeat it here): +% \def\matrixcontent{ % First row: \& \& \& \& \& \& \& \& \& \& \& \node (plus) [terminal] {+};\\ @@ -551,7 +559,7 @@ repeat it here): hv path/.style={to path={-| (\tikztotarget)}}, vh path/.style={to path={|- (\tikztotarget)}}] \matrix[row sep=1mm,column sep=2mm] { \matrixcontent }; - + \graph { (p1) -> (ui1) -- (p2) -> (dot) -- (p3) -> (digit) -- (p4) -- (p5) -- (p6) -> (e) -- (p7) -- (p8) -> (ui2) -- (p9) -> (p10); @@ -564,23 +572,22 @@ repeat it here): \end{tikzpicture} \end{codeexample} -This is already pretty near to the desired result, just a few -``finishing touches'' are needed to style the edges more nicely. - -However, Ilka does not have the feeling that the |graph| command is -all that hot in the example. It certainly does cut down on the number -of characters she has to write, but the overall graph structure is not -that much clear -- it is still mainly a list of paths through the -graph. It would be nice to specify that, say, there the path from -|(p7)| sort of splits to |(plus)| and |(minus)| and then merges once -more at |(p8)|. Also, all these parentheses are bit hard to type. - -It turns out that edges from a node to a whole group of nodes are -quite easy to specify, as shown in the next example. Additionally, by -using the |use existing nodes| option, Ilka can also leave out all the -parentheses (again, some options have been moved outside to keep the -examples shorter): - +This is already pretty near to the desired result, just a few ``finishing +touches'' are needed to style the edges more nicely. + +However, Ilka does not have the feeling that the |graph| command is all that +hot in the example. It certainly does cut down on the number of characters she +has to write, but the overall graph structure is not that much clear -- it is +still mainly a list of paths through the graph. It would be nice to specify +that, say, there the path from |(p7)| sort of splits to |(plus)| and |(minus)| +and then merges once more at |(p8)|. Also, all these parentheses are bit hard +to type. + +It turns out that edges from a node to a whole group of nodes are quite easy to +specify, as shown in the next example. Additionally, by using the +|use existing nodes| option, Ilka can also leave out all the parentheses +(again, some options have been moved outside to keep the examples shorter): +% \begin{codeexample}[pre={\tikzset{ampersand replacement=\&,point/.style={coordinate}, skip loop/.style={to path={-- ++(0,##1) -| (\tikztotarget)}}, hv path/.style={to path={-| (\tikztotarget)}}, @@ -589,7 +596,7 @@ examples shorter): every new ->/.style={shorten >=1pt}, graphs/every graph/.style={edges=rounded corners}] \matrix[column sep=4mm] { \matrixcontent }; - + \graph [use existing nodes] { p1 -> ui1 -- p2 -> dot -- p3 -> digit -- p4 -- p5 -- p6 -> e -- p7 -- p8 -> ui2 -- p9 -> p10; p4 ->[skip loop=-5mm] p3; @@ -603,31 +610,35 @@ examples shorter): \subsubsection{Creating Nodes Using the Graph Command} -Ilka has heard that the |graph| command is also supposed to make it -easy to create nodes, not only to connect them. This is, indeed, -correct: When the |use existing node| option is not used and when a -node name is not surrounded by parentheses, then \tikzname\ will -actually create a node whose name and text is the node name: +Ilka has heard that the |graph| command is also supposed to make it easy to +create nodes, not only to connect them. This is, indeed, correct: When the +|use existing nodes| option is not used and when a node name is not surrounded +by parentheses, then \tikzname\ will actually create a node whose name and text +is the node name: +% \begin{codeexample}[] -\tikz \graph [grow right=2cm] { unsigned integer -> d -> digit -> E }; +\tikz \graph [grow right=2cm] { unsigned integer -> d -> digit -> E }; \end{codeexample} -Not quite perfect, but we are getting somewhere. First, let us change -the positioning algorithm by saying |grow right sep|, which causes new -nodes to be placed to the right of the previous nodes with a certain -fixed separation (|1em| by default). Second, we add some options to -make the node ``look nice''. Third, note the funny |d| node above: Ilka -tried writing just |.| there first, but got some error messages. The -reason is that a node cannot be called |.| in \tikzname, so she had to -choose a different name -- which is not good, since she wants a dot to -be shown! The trick is to put the dot in quotation marks, this allows -you to use ``quite arbitrary text'' as a node name: +% +Not quite perfect, but we are getting somewhere. First, let us change the +positioning algorithm by saying |grow right sep|, which causes new nodes to be +placed to the right of the previous nodes with a certain fixed separation +(|1em| by default). Second, we add some options to make the node ``look nice''. +Third, note the funny |d| node above: Ilka tried writing just |.| there first, +but got some error messages. The reason is that a node cannot be called |.| in +\tikzname, so she had to choose a different name -- which is not good, since +she wants a dot to be shown! The trick is to put the dot in quotation marks, +this allows you to use ``quite arbitrary text'' as a node name: + \begin{codeexample}[] \tikz \graph [grow right sep] { unsigned integer[nonterminal] -> "."[terminal] -> digit[terminal] -> E[terminal] -}; +}; \end{codeexample} -Now comes the fork to the plus and minus signs. Here, Ilka can use the -grouping mechanism of the |graph| command to create a split: +% +Now comes the fork to the plus and minus signs. Here, Ilka can use the grouping +mechanism of the |graph| command to create a split: +% \begin{codeexample}[] \tikz \graph [grow right sep] { unsigned integer [nonterminal] -> @@ -640,29 +651,28 @@ grouping mechanism of the |graph| command to create a split: "-" [terminal] } -> ui2/unsigned integer [nonterminal] -}; +}; \end{codeexample} -Let us see, what is happening here. We want two |unsigned integer| -nodes, but if we just were to use this text twice, then \tikzname\ -would have noticed that the same name was used already in the current -graph and, being smart (actually too smart in this case), would have -created an edge back to the already-created node. Thus, a fresh name -is needed here. However, Ilka also cannot just write -|unsigned integer2|, because she wants the original text to be shown, -after all! The trick is to use a slash inside the node name: In -order to ``render'' the node, the text following the slash -is used instead of the node name, which is the text before the -slash. Alternatively, the |as| option can be used, which also -allows you to specify how a node should be rendered. - -It turns out that Ilka does not need to invent a name like -|ui2| for a node that she will not reference again -anyway. In this case, she can just leave out the name (write nothing -before |/|), which always stands for a ``fresh, anonymous'' node name. - -Next, Ilka needs to add some coordinates in between of some nodes -where the back-loops should got and she needs to shift the nodes a -bit: +% +Let us see, what is happening here. We want two |unsigned integer| nodes, but +if we just were to use this text twice, then \tikzname\ would have noticed that +the same name was used already in the current graph and, being smart (actually +too smart in this case), would have created an edge back to the already-created +node. Thus, a fresh name is needed here. However, Ilka also cannot just write +|unsigned integer2|, because she wants the original text to be shown, after +all! The trick is to use a slash inside the node name: In order to ``render'' +the node, the text following the slash is used instead of the node name, which +is the text before the slash. Alternatively, the |as| option can be used, which +also allows you to specify how a node should be rendered. + +It turns out that Ilka does not need to invent a name like |ui2| for a node +that she will not reference again anyway. In this case, she can just leave out +the name (write nothing before |/|), which always stands for a ``fresh, +anonymous'' node name. + +Next, Ilka needs to add some coordinates in between of some nodes where the +back-loops should got and she needs to shift the nodes a bit: +% \begin{codeexample}[pre={\tikzset{ skip loop/.style={to path={-- ++(0,##1) -| (\tikztotarget)}}, hv path/.style={to path={-| (\tikztotarget)}}, @@ -699,20 +709,19 @@ bit: \end{tikzpicture} \end{codeexample} -All that remains to be done is to somehow get rid of the strange -curves between the |E| and the unsigned integer. They are caused by -\tikzname's attempt at creating an edge that first goes vertical and -then horizontal but is actually just horizontal. Additionally, the -edge should not really be pointed; but it seems difficult to get rid -of this since the \emph{other} edges from |q1|, namely to |plus| and -|minus| should be pointed. - -It turns out that there is a nice way of solving this problem: You can -specify that a graph is |simple|. This means that there can be at most -one edge between any two nodes. Now, if you specify an edge twice, the -options of the second specification ``win.'' Thus, by adding two more -lines that ``correct'' these edges, we get the final diagram with its -complete code: +All that remains to be done is to somehow get rid of the strange curves between +the |E| and the unsigned integer. They are caused by \tikzname's attempt at +creating an edge that first goes vertical and then horizontal but is actually +just horizontal. Additionally, the edge should not really be pointed; but it +seems difficult to get rid of this since the \emph{other} edges from |q1|, +namely to |plus| and |minus| should be pointed. + +It turns out that there is a nice way of solving this problem: You can specify +that a graph is |simple|. This means that there can be at most one edge between +any two nodes. Now, if you specify an edge twice, the options of the second +specification ``win''. Thus, by adding two more lines that ``correct'' these +edges, we get the final diagram with its complete code: +% \begin{codeexample}[] \tikz [>=stealth', black!50, text=black, thick, every new ->/.style = {shorten >=1pt}, @@ -725,7 +734,7 @@ complete code: bottom color=red!50!black!20, font=\itshape, text height=1.5ex,text depth=.25ex}, terminal/.style = { rounded rectangle, minimum size=6mm, very thick, draw=black!50, top color=white, - bottom color=black!20, font=\ttfamily, text height=1.5ex, text depth=.25ex}, + bottom color=black!20, font=\ttfamily, text height=1.5ex, text depth=.25ex}, shape = coordinate ] \graph [grow right sep, branch down=7mm, simple] { @@ -745,23 +754,23 @@ complete code: \end{codeexample} - +%% TODOsp: a commented subsection % \subsection{Using Chains} - +% % Matrices allow Ilka to align the nodes nicely, but the connections are % not quite perfect. The problem is that the code does not really % reflect the paths that underlie the diagram. - - +% +% % For this reason, Ilka decides to try out \emph{chains} by including % the |chain| library. Basically, a chain is just a sequence of % (usually) connected nodes. The nodes can already have been constructed % or they can be constructed as the chain is constructed (or these % processes can be mixed). - +% % \subsubsection{Creating a Simple Chain} - - +% +% % Ilka starts with creating a chain from scratch. For this, she starts a % chain using the |start chain| option in a scope. Then, inside the % scope, she uses the |on chain| option on nodes to add them to the @@ -776,11 +785,11 @@ complete code: % \end{tikzpicture} % \end{codeexample} % (Ilka will add the plus and minus nodes later.) - +% % As can be seen, the nodes of a chain are placed in a row. This can be % changed, for instance by saying |start chain=going below| we get a % chain where each node is below the previous one. - +% % The next step is to \emph{join} the nodes of the chain. For this, we % add the |join| option to each node. This joins the node with the % previous node (for the first node nothing happens). @@ -805,10 +814,10 @@ complete code: % \node [nonterminal] {unsigned integer}; % \end{tikzpicture} % \end{codeexample} - - +% +% % \subsubsection{Branching and Joining a Chain} - +% % It is now time to add the plus and minus signs. They obviously % \emph{branch off} the main chain. For this reason, we start a branch % for them using the |start branch| option. @@ -827,7 +836,7 @@ complete code: % \node [nonterminal,join=with plus,join=with minus] {unsigned integer}; % \end{tikzpicture} % \end{codeexample} - +% % Let us see, what is going on here. First, the |start branch| begins a % branch, starting with the node last created on the current chain, % which is the |E| node in our case. This is implicitly also the first @@ -838,18 +847,18 @@ complete code: % to be placed above and right of the |E| node. It is automatically % joined to its predecessor on the branch by the implicit |join| % option. - +% % When the first branch ends, only the plus node has been added and the % current chain is the original chain once more and we are back to the % |E| node. Now we start a new branch for the minus node. After this % branch, the current chain ends at |E| node once more. - +% % Finally, the rightmost unsigned integer is added to the (main) chain, % which is why it is joined correctly with the |E| node. The two % additional |join| options get a special |with| parameter. This allows % you to join a node with a node other than the predecessor on the % chain. The |with| should be followed by the name of a node. - +% % Since Ilka will need scopes more often in the following, she includes % the |scopes| library. This allows her to replace |\begin{scope}| % simply by an opening brace and |\end{scope}| by the corresponding @@ -860,7 +869,7 @@ complete code: % |chain/|\meta{branch}|-|\meta{i}. The \meta{i} can be replaced by % |begin| and |end| to reference the first and (currently) last node on % the chain. - +% % \begin{codeexample}[] % \begin{tikzpicture}[start chain,node distance=5mm, every on chain/.style={join}, every join/.style={->}] % \node [on chain,nonterminal] {unsigned integer}; @@ -876,8 +885,8 @@ complete code: % \node [nonterminal,on chain,join=with chain/plus-end,join=with chain/minus-end] {unsigned integer}; % \end{tikzpicture} % \end{codeexample} - - +% +% % The next step is to add intermediate coordinate nodes in the same % manner as Ilka did for the matrix. For them, we change the |join| % style slightly, namely for these nodes we do not want an arrow @@ -885,7 +894,7 @@ complete code: % |every join| style or, which is what is done in the below example, by % giving the desired style using |join=by ...|, where |...| is the style % to be used for the join. - +% % \begin{codeexample}[] % \begin{tikzpicture}[start chain,node distance=5mm and 2mm, % every node/.style={on chain}, @@ -911,14 +920,14 @@ complete code: % \node [point] {}; % \end{tikzpicture} % \end{codeexample} - - +% +% % \subsubsection{Chaining Together Already Positioned Nodes} - +% % The final step is to add the missing arrows. We can also use branches % for them (even though we do not have to, but it is good practice and % they exhibit the structure of the diagram in the code). - +% % Let us start with the repeat loop around the |digit|. This can be % thought of as a branch that starts at the point after the digit and % that ends at the point before the digit. However, we have already @@ -927,7 +936,7 @@ complete code: % command. This command must be followed by a coordinate that contains a % node name and optionally some options. The effect is that the named % node is made part of the current chain. - +% % \begin{codeexample}[pre={\tikzset{node distance=5mm and 2mm, % every node/.style={on chain}, % terminal/.append style={join=by ->}, @@ -943,18 +952,18 @@ complete code: % \node [point] {}; % \end{tikzpicture} % \end{codeexample} - - +% +% % \subsubsection{Combined Use of Matrices and Chains} - +% % Ilka's final idea is to combine matrices and chains in the following % manner: She will use a matrix to position the nodes. However, to show % the logical ``flow structure'' inside the diagram, she will create % chains and branches that show what is going on. - +% % Ilka starts with the matrix we had earlier, only with slightly adapted % styles. Then she writes down the main chain and its branches: - +% % \begin{codeexample}[] % \begin{tikzpicture}[point/.style={coordinate},>=stealth',thick,draw=black!50, % tip/.style={->,shorten >=1pt},every join/.style={rounded corners}, @@ -975,7 +984,7 @@ complete code: % % Third row: % & & & & & & & & & & & \node (minus)[terminal] {-};\\ % }; - +% % { [start chain] % \chainin (p1); % \chainin (ui1) [join=by tip]; |