# Getting multiline nodes automatically with forest

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 '18 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 '18 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}