Write trees in LaTeX

I think this would be a good question how to bring a customized flowchart (your own) in MS Word to LaTeX. Well, I have attached a World file, and I am trying to convert that in LaTex. The Save as "type" LaTeX did not work, and generated a nonsense PDF. So, what would be the best way?

I do not know how to attach a word file here. so I have put a snapshot of that.

One way is to re-type this in LaTeX, but I do not feel confidence to do that (feel difficulty to have this arrows) Clearly, when I bring this as figure into LaTeX source file, the fonts are not match with other stuff in paper. Therefore, I want to convert that (as simplest way perhaps)

• You either import as an image (via \includegraphics) or you use a native 'drawing' program such as PSTricks or TikZ/PGF. The first way is easy; the second way will look much nicer. Or draw it in something like Inkscape and convert it to TikZ. I believe there are a few 'svg2tikz'-type programs out there.... – jon Feb 22 '16 at 19:48
• @ jon via \includegraphics) does not work. There is no match between fonts (in snapshot) and the one you typed in LaTex. Inkscape is good, but it is hard to set up these equations in greek letters. It also again is picture to be imported in LaTex. I have word file of the snapshot, wanted to convert that in LaTex. How? – Mohammad Feb 22 '16 at 19:52
• I realize the shortcomings. I was giving you the options. I also realize it is too late for you now, but this is a clear example why you should never do drawings in (binary and proprietary, no less!) software if you want to use the drawings somewhere else. In my opinion, you are best served redrawing them. – jon Feb 22 '16 at 19:55
• @jon, how should I control the flow of arrows in LaTex? I am not familiar with this. Typing greek is easy but bring them in the form of snapshot is not easy, I think. – Mohammad Feb 22 '16 at 19:59
• It is easy to create this picture in tikz. Arrows are created by \draw[->] (0,0)--(2,0); and you're Greek letters by \node at (2,0) {\alpha}; – Carina Feb 22 '16 at 20:40

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}

• @ cfr please look at snapshot in my original question. – Mohammad Feb 22 '16 at 22:56
• +10 (if I could) Why do I even bother answering tree questions any more... :) – Alan Munn Feb 22 '16 at 23:18
• @Mohammad Although Marco's answer solves your direct problem, if you have more than one of these trees to draw the forest method that I and @cfr have shown you will be much more practical in the long run. And remember that the answers are designed to show you what you can do, they can't do everything for you. You will need to spend a little time yourself with the TikZ and forest documentation to become more acquainted with the code we've all given you. – Alan Munn Feb 22 '16 at 23:22
• @AlanMunn So I can take a very scenic route to a very minor improvement? I don't understand the problem with the GGGGs, though. – cfr Feb 22 '16 at 23:29
• @cfr the code above does not compile. – Mohammad Feb 22 '16 at 23:31

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}


• @ Alan Munn, please vote question if you found that interesting. Thanks for super beautiful answer. I accepted that. – Mohammad Feb 22 '16 at 21:32
• There's a funny kink in one of your arrows. Also, what does growth parent anchor do? I can find it in none of the current documentation, the current source or the .sty for the final version 1 of the package. (Not currently sure how to eliminate the kink....) – cfr Feb 22 '16 at 22:02
• @ Alan Munn , how should I have another child in front of F and E? can you modify the code? – Mohammad Feb 22 '16 at 22:08
• @cfr look at snapshot. this code represents that one. – Mohammad Feb 22 '16 at 22:09
• @Mohammad Sorry. I don't understand what you mean. In the second part of this answer I explain a bit about Forest and, in particular, how to convert a tree to Forest's bracket syntax. Does that help? – cfr Feb 22 '16 at 22:49

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}


• @ Macro, how did you work with these number e.g. \draw [thick,-latex] (2.3,9) -- + (0.5,0) node [right]? did you type them in LaTeX? – Mohammad Feb 22 '16 at 21:24
• I used a grid (in the code it is commented out). As you can see I used TikZ. – Marco Feb 22 '16 at 21:31
• You should already have it. Have you tried compiling the code I posted? – Marco Feb 22 '16 at 21:36
• @ yes, it has been compiled. I perhaps did not ask my question clearly, what "node" do for us here? and other parameter in this syntax. – Mohammad Feb 22 '16 at 21:41
• See the paragraph 5 cremeronline.com/LaTeX/minimaltikz.pdf – Marco Feb 22 '16 at 21:48