2

I am drawing a tree with tikz, namely, using tikz-qtree-compat for simpler syntax. On the tree there can be nodes of different height but then branches become messy. I want to add an instruction where distance between the south point of a mother nodes and the north point of a child node is constant.

MWE: https://www.overleaf.com/2453721thvhsh#/6394537/

\documentclass[10pt]{report}

\usepackage{tikz-qtree-compat}  % draw trees with simple syntax

\begin{document}

wanted but obtained non-general way:
\begin{tikzpicture}[baseline=-20pt, grow=down]
\tikzset{level distance = 25pt, sibling distance = 30pt}
\tikzset{every tree node/.style={align=center,anchor=north}}
\tikzset{level 1/.style={level distance=38pt}}  
\tikzset{level 2/.style={level distance=60pt}}  
\tikzset{level 4/.style={level distance=60pt}}  
\Tree
[.\node{A\\A};
 [.\node{B\\B\\B\\B};
  [.\node{C};
   [.\node{E\\E\\E};]
  ]
  [.\node{D\\D\\D};
   [.\node at(0,-25pt){F\\F};
    [.\node{G};]
   ]
  ] 
 ]
]
\end{tikzpicture}

without any modification looks like:
\begin{tikzpicture}[baseline=-20pt, grow=down]
\tikzset{level distance = 25pt, sibling distance = 30pt}
\tikzset{every tree node/.style={align=center,anchor=north}}
%\tikzet{some single instruction that fix all trees in this way}
\Tree
[.\node{A\\A};
 [.\node{B\\B\\B\\B};
  [.\node{C};
   [.\node{E\\E\\E};]
  ]
  [.\node{D\\D\\D};
   [.\node{F\\F};
    [.\node{G};]
   ]
  ] 
 ]
]
\end{tikzpicture}


\end{document} 
  • How about growth parent anchor=north? See "Custom Growth Functions" in the PGF manual. – Mark Wibrow Mar 26 '15 at 19:18
4

With forest you can get what you want with a not too complex syntax:

\documentclass[10pt]{report}

\usepackage{forest}

\begin{document}

\begin{forest}  
[A\\A, for tree={parent anchor=south, child anchor=north, align=center, s sep=5mm}
 [B\\B\\B\\B
  [C [E\\E\\E]]
  [D\\D\\D [F\\F [G]]]
 ]
]
\end{forest}

\end{document} 

enter image description here

2

This is a variation on Ignasi's solution which tries to illustrate the process of converting the tree to forest and how to align the tree with the surrounding text in an equivalent way.

\documentclass{article}

\usepackage{forest}

\begin{document}

  a forest solution:
  \begin{forest}
    for tree={
      align=center,% will apply to every node in the tree, enables multiline nodes with centred alignment of text
      parent anchor=south,% required to emulate the behaviour of the target tree
      child anchor=north,% required to emulate the behaviour of the target tree
      s sep=30pt,% equivalent of setting sibling distance, I think
      l sep=10pt,% similar to setting level distance, I think
    }
    [A\\A, anchor=center, baseline% you need to specify the baseline differently to align the tree in a particular way with the surrounding text
     [B\\B\\B\\B
      [C
       [E\\E\\E]
      ]
      [D\\D\\D
       [F\\F
        [G]
       ]
      ]
     ]
    ]
  \end{forest}

\end{document}

another forest tree

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.