Problem
I am trying to draw a tree diagram with forest
package. I have multiline children including the \texttt
command and a one-line child with normal text. This results in a misalignment among the children, as you can see from the picture below.
I would like all children to be top-aligned. How can I achieve that?
MWE:
% Class
\documentclass[10pt]{article}
% Forest package
\usepackage[edges]{forest}
%Defining the styles used in trees
\tikzset{standard/.style={align=center,text width=3cm,rounded corners=2pt},
wide/.style={align=center,text width=4.2cm,rounded corners=2pt},
narrow/.style={align=center,text width=2cm,rounded corners=2pt}
}
% Document
\begin{document}
\begin{figure}[hbt!]
\centering
\begin{forest}
for tree={%
% grow'=0,
l sep=0.5cm,
s sep=0.1cm,
minimum height=0.8cm,
minimum width=2cm,
draw%
},
forked edges,
[\texttt{Parent} object, standard
[\texttt{AStandardChild} object, standard]
[\texttt{VeryVeryVeryLongChild} object, wide]
[Other, narrow]
]
\end{forest}
\end{figure}
\end{document}
Attempted solution
I tried to use some additional for tree
options inspired by this answer. In this attempt I need to force the line break (\\
) of the long tree elements because that would not happen automatically. The misalignment is reduced but it is still present.
MWE:
% Class
\documentclass[10pt]{article}
% Forest package
\usepackage[edges]{forest}
%Defining the styles used in trees
\tikzset{standard/.style={align=center,text width=3cm,rounded corners=2pt},
wide/.style={align=center,text width=4.2cm,rounded corners=2pt},
narrow/.style={align=center,text width=2cm,rounded corners=2pt}
}
% Document
\begin{document}
\begin{figure}[hbt!]
\centering
\begin{forest}
for tree={%
% grow'=0,
l sep=0.5cm,
s sep=0.1cm,
minimum height=0.8cm,
minimum width=2cm,
parent anchor=south, child anchor=north, align=center, base=top,
draw%
},
forked edges,
[\texttt{Parent} object, standard
[\texttt{AStandardChild}\\object, standard]
[\texttt{VeryVeryVeryLongChild}\\object, wide]
[Other, narrow]
]
\end{forest}
\end{figure}
\end{document}