Write trees in LaTeX
Alan's answer works OK, but with the current version of Forest there is no need to define the edge path
from scratch. Instead, we can use the edges
library with the option forked edges
. Moreover, we can eliminate growth parent anchor=east
as it doesn't do anything (even in the old version of Forest), and we can use the parent
and children
anchors rather than east
and west
to make the code more flexible.
parent anchor=children,
child anchor=parent,
forked edges,
edge={->,>=latex},
In fact, grow=east
is enough on its own, so we can drop parent anchor
and child anchor
specifications altogether.
This gives us the following code for the same output as Alan showed in his answer.
\documentclass[tikz,multi,border=10pt]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={
grow=east,
math content,
edge={->,>=latex},
},
forked edges
[\tau>\tau^0
[A_x<\tau<A_l
]
[D
[E]
[F]
]
[\tau<A_x
[{\zeta^n=\zeta^n-1}
]
]
]
\end{forest}
\end{document}
On closer inspection, however, the lines could be better:
The default definition of forked edges
is
forked edges/.style={
for tree={parent anchor=children},
for descendants={child anchor=parent,forked edge}
},
So, let's try redefining it so that the forked edge
is only used if a node has more than one child by adding this redefinition:
\forestset{
forked edges/.style={
for tree={parent anchor=children},
for descendants={
child anchor=parent,
if={n_children("!u")==1}{}{
forked edge
},
}
},
}
This is better:
However, the arrow is still angled - it is not quite horizontal. What we need to do is define an alternative edge path
for the case where there is precisely one child.
The default edge is drawn from the parent node's parent anchor (!u.parent anchor)
to the child node's child anchor (.child anchor)
. We'd like the start of the arrow to be horizontally aligned with (.child anchor)
. (We could instead align the end point with the parent's parent anchor, of course.)
\forestset{
forked edges/.style={
for tree={parent anchor=children},
for descendants={
child anchor=parent,
if={n_children("!u")==1}{
edge path'={
(!u.parent anchor |- .child anchor) -- (.child anchor)
},
}{
forked edge,
},
}
},
}
This produces the horizontal arrow we're looking for:
However, this may not be the best solution. If the parent and child nodes are too different in size, we might get a strange alignment. So perhaps we should instead tell Forest to align the child with the parent so that the child's child anchor lines up with the parent's parent anchor.
\forestset{
forked edges/.style={
for tree={parent anchor=children},
for descendants={
child anchor=parent,
if={n_children("!u")==1}{
!u.calign=child edge,
}{
forked edge,
},
}
},
}
does the trick.
Right now, the result looks like this:
This is better, but it would be nice if we could align the middle child with the parent when the parent has an odd number of children. For example, if D
's edge was aligned with the line drawn from the root node.
This is a little trickier, but not much so. We can use the calign=child edge
trick again and set the middle child to be its parent's 'primary' child.
We can add this into the preamble at the beginning of our forest
environment:
\begin{forest}
for tree={
...
if={isodd(n_children())}{
calign primary child/.pgfmath={(n_children()+1)/2},
calign=child edge,
}{},
},
In fact, since any node with exactly 1 child has an odd number of children, we can also drop the redefinition of forked edges
since we'll get a straight arrow now anyway:
\documentclass[tikz,multi,border=10pt]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={
grow=east,
math content,
edge={->,>=latex},
if={isodd(n_children())}{
calign primary child/.pgfmath={(n_children()+1)/2},
calign=child edge,
}{}
},
forked edges
[\tau>\tau^0
[A_x<\tau<A_l
]
[D
[E]
[F]
]
[\tau<A_x
[{\zeta^n=\zeta^n-1}
]
]
]
\end{forest}
\end{document}
Stealing Greek code shamelessly from Marco:
\begin{forest}
for tree={
grow'=east,
math content,
edge={->,>=latex},
if={isodd(n_children())}{
calign primary child/.pgfmath={(n_children()+1)/2},
calign=child edge,
}{}
},
forked edges
[\tau>\tau^0
[\tau<A_x
[{\zeta^n=\zeta^{n-1}}]
[{\zeta^n=\zeta^{n-1}}]
[{\zeta^n=\zeta^{n-1}}]
]
[A_x<\tau<A_l
[\sigma<C_{a}(T-A_{x})
[{\zeta^n=\zeta^{n-1}}]
[{\zeta^n=\zeta^{n-1}}]
[{\zeta^n=\zeta^{n-1}}]
]
[\sigma>C_{a}(T-A_{x})
[{\xi^{n}=\frac{\xi^{0}}{2}\cos \left ( a_{A}\left (\Gamma-A_{x}-\frac{\sigma}{C_{a}} \right ) \right )}]
[{\xi_{s}^{n}=\xi_{s}^{n}-\frac{\xi_{s}^{n}}{\xi_{s}^{n}}-(\xi^{0}-\xi^{n})}]
[{\xi_{s}^{n}=\xi_{s}^{n}-\frac{\xi_{s}^{n}}{\xi_{s}^{n}}-(\xi^{0}-\xi^{n})}]
]
]
]
\end{forest}
EDIT
Your code produces 'gaps' because [[<something>]]
produces an empty node and then a node with [<something>]
. To eliminate the empty node, just say [<something>]
.
\documentclass{article}
\usepackage{forest-1}
\begin{document}
\begin{forest}
for tree={
grow=east,
parent anchor=east,
child anchor=west,
math content,
edge={->, >={latex}},
edge path={\noexpand\path[\forestoption{edge}] (!u.parent anchor) -- +(5pt,0pt) |- (.child anchor) \forestoption{edge label};}
}
[T>T^0
[T>A_f
[C_a(T-A_f) <\sigma <C_a (T-A_s)
[ {\zeta_s^n=\zeta_s^0-\frac{\zeta_s^0}{\zeta^0}(\zeta^0-\zeta^n)} ]
[ {\zeta_T^n=\zeta_T^0-\frac{\zeta_T^0}{\zeta^0}(\zeta^0-\zeta^n)} ]
[{\zeta^n=\frac{\zeta^0}{2}(cos \big (\alpha_A(T-A_s-\frac{\sigma}{C_a})\big )+1)} ]
]
[\sigma<C_a(T-A_s)
[ {\zeta^n=\zeta^{n-1}} ]
[ {\zeta_s^n=\zeta_s^{n-1}} ]
[ {\zeta_T^n=\zeta_T^{n-1}} ]
]
]
[A_s<T<A_f
[\sigma<C_a(T-A_s)
[ {\zeta_s^n=\zeta_s^0-\frac{\zeta_s^0}{\zeta^0}(\zeta^0-\zeta^n)} ]
[ {\zeta_T^n=\zeta_T^0-\frac{\zeta_T^0}{\zeta^0}(\zeta^0-\zeta^n)} ]
[{\zeta^n=\frac{\zeta^0}{2}(cos \big (\alpha_A(T-A_s-\frac{\sigma}{C_a})\big )+1)}]
[\sigma>C_a(T-A_s)
[ {\zeta^n=\zeta^{n-1}} ]
[ {\zeta_s^n=\zeta_s^{n-1}} ]
[ {\zeta_T^n=\zeta_T^{n-1}} ]
]
]
]
[T<A_s
[ {\zeta^n=\zeta^{n-1}} ]
[ {\zeta_s^n=\zeta_s^{n-1}} ]
[ {\zeta_T^n=\zeta_T^{n-1}} ]
]
]
\end{forest}
\end{document}
Note that with version 1, you do need to specify the parent anchor=east, child anchor=west
and to change the edge path
. There is no forked edges
. Also, it is not easy to eliminate the kink when there is only one child because calign=child edge
is buggy in versions prior to 2.01. You could still do it, but it would be much easier to update than to work around the bug. This isn't an issue if your tree has no only children, but if you have other trees, it might be. Nor is it easy to align the edges for similar reasons. You would need to do something with edge path
to get it working.
With the current package, on the other hand, your tree can easily be adjusted to look like this and the code is simpler, too:
\documentclass{article}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={
grow'=east,
math content,
edge={->,>=latex},
if={isodd(n_children())}{
calign primary child/.pgfmath={(n_children()+1)/2},
calign=child edge,
}{}
},
forked edges
[T>T^0
[T>A_f
[C_a(T-A_f) <\sigma <C_a (T-A_s)
[ {\zeta_s^n=\zeta_s^0-\frac{\zeta_s^0}{\zeta^0}(\zeta^0-\zeta^n)} ]
[ {\zeta_T^n=\zeta_T^0-\frac{\zeta_T^0}{\zeta^0}(\zeta^0-\zeta^n)} ]
[{\zeta^n=\frac{\zeta^0}{2}(cos \big (\alpha_A(T-A_s-\frac{\sigma}{C_a})\big )+1)} ]
]
[\sigma<C_a(T-A_s)
[ {\zeta^n=\zeta^{n-1}} ]
[ {\zeta_s^n=\zeta_s^{n-1}} ]
[ {\zeta_T^n=\zeta_T^{n-1}} ]
]
]
[A_s<T<A_f
[\sigma<C_a(T-A_s)
[ {\zeta_s^n=\zeta_s^0-\frac{\zeta_s^0}{\zeta^0}(\zeta^0-\zeta^n)} ]
[ {\zeta_T^n=\zeta_T^0-\frac{\zeta_T^0}{\zeta^0}(\zeta^0-\zeta^n)} ]
[{\zeta^n=\frac{\zeta^0}{2}(cos \big (\alpha_A(T-A_s-\frac{\sigma}{C_a})\big )+1)}, calign with current]
[\sigma>C_a(T-A_s)
[ {\zeta^n=\zeta^{n-1}} ]
[ {\zeta_s^n=\zeta_s^{n-1}} ]
[ {\zeta_T^n=\zeta_T^{n-1}} ]
]
]
]
[T<A_s
[ {\zeta^n=\zeta^{n-1}} ]
[ {\zeta_s^n=\zeta_s^{n-1}} ]
[ {\zeta_T^n=\zeta_T^{n-1}} ]
]
]
\end{forest}
\end{document}
This is not very difficult to do with forest
. The following example should get you started.
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}for tree={
grow=east
parent anchor=east,
child anchor=west,
math content,
edge path={\noexpand\path[\forestoption{edge},->, >={latex}]
(!u.parent anchor) -- +(5pt,0pt) |- (.child anchor)
\forestoption{edge label};}}
[\tau>\tau^0 [A_x<\tau<A_l ] [D [E ] [F ]][\tau<A_x [ {\zeta^n=\zeta^n-1} ]]]
\end{forest}
\end{document}
Consider that this is not the right approach at all, but I am not an expert and this is the only way I know.
\documentclass[border=0.5cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
% \draw [help lines] (0,0) grid (13,13);
\draw [thick] (0,0) -- (0,10);
\draw [thick,-latex] (0,5) -- + (0.5,0) node [right] {$A<T<A_{f}$};
\draw [thick,-latex] (2.8,5) -- + (0.5,0);
\draw [thick] (3.3,3.5) -- (3.3,6.5);
\draw [thick,-latex] (3.3,3.5) -- + (0.5,0) node [right] {$\sigma<C_{a}(T-A_{x})$};
\draw [thick,-latex] (3.3,6.5) -- + (0.5,0) node [right] {$\sigma>C_{a}(T-A_{x})$};
\draw [thick,-latex] (0,10) -- + (0.5,0) node [right] {$T<A$};
\draw [thick,-latex] (1.8,10) -- + + (0.5,0);
\draw [thick] (2.3,9) -- (2.3,11);
\draw [thick,-latex] (2.3,9) -- + (0.5,0) node [right] {$\xi^{n}=\xi^{n-1}$};
\draw [thick,-latex] (2.3,10) -- + (0.5,0) node [right] {$\xi^{n}=\xi^{n-1}$};
\draw [thick,-latex] (2.3,11) -- + (0.5,0) node [right] {$\xi_{T}^{n}=\xi_{T}^{n-1}$};
\draw [thick,-latex] (6.5,6.5) -- + (0.5,0);
\draw [thick] (7,5.8) -- + (0,1.4);
\draw [thick,-latex] (7,5.8) -- + (0.5,0) node [right] {$\xi^{n}=\xi^{n-1}$};
\draw [thick,-latex] (7,6.5) -- + (0.5,0) node [right] {$\xi^{n}=\xi^{n-1}$};
\draw [thick,-latex] (7,7.2) -- + (0.5,0) node [right] {$\xi_{T}^{n}=\xi_{T}^{n-1}$};
\draw [thick,-latex] (6.5,3.5) -- + (0.5,0);
\draw [thick] (7,2.5) -- + (0,2);
\draw [thick,-latex] (7,2.5) -- + (0.5,0) node [right] {$\xi^{n}=\frac{\xi^{0}}{2}\cos \left ( a_{A}\left (\Gamma-A_{x}-\frac{\sigma}{C_{a}} \right ) \right )$};
\draw [thick,-latex] (7,3.5) -- + (0.5,0) node [right] {$\xi_{s}^{n}=\xi_{s}^{n}-\frac{\xi_{s}^{n}}{\xi_{s}^{n}}-(\xi^{0}-\xi^{n})$};
\draw [thick,-latex] (7,4.5) -- + (0.5,0) node [right] {$\xi_{s}^{n}=\xi_{s}^{n}-\frac{\xi_{s}^{n}}{\xi_{s}^{n}}-(\xi^{0}-\xi^{n})$};
\end{tikzpicture}
\end{document}