[Tex/LaTex] Write trees in LaTeX

treesword-to-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?

enter image description here

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)

Best Answer

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:

kinky arrow

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:

less kinky

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:

unkinked arrow

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:

interim tree

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:

final tree

\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}

more tree

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}

fewer gaps

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:

aligned tree

\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}