This defines a command that allows multiple lines within a forest node.

\documentclass{article}
\usepackage{forest}
\usepackage{varwidth}

\newcommand\nbox[1]{\begin{varwidth}{.5\textwidth}#1\end{varwidth}}

\begin{document}

\begin{figure}[h]
  \centering
  \begin{forest}
[parent
  [\nbox{first\\child}]
  [\nbox{second\\child}]
  ]
  \end{forest}
\end{figure}

\end{document}

Is it possible to have this command inserted automatically into all nodes, so it does not have to be repeated? This would be useful where all or almost all nodes have multiple lines.

  • Use ’\forestset{default preamble={...}}’. – Sašo Živanović Oct 12 at 6:13
  • Combine @SašoŽivanović's comment with marmot's answer if you want this for all trees in the document or use just marmot's answer to apply on a per-tree basis. – cfr Nov 11 at 0:47

You really do not need varwidth to have multiline nodes. for tree={align=left} does that already.

\documentclass{article}
\usepackage{forest}

\begin{document}

\begin{figure}[h]
  \centering
  \begin{forest}
  for tree={align=left}
[parent
  [{first\\child}]
  [{second\\child}]
  ]
  \end{forest}
\end{figure}

\end{document}

enter image description here

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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