0

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.

enter image description here

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.

enter image description here

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

Like this?

enter image description here

You only need to add (in your simple example) anchor=north to styles of nodes:

\documentclass[10pt]{article}
\usepackage[edges]{forest}

% Document
\begin{document}
    \begin{figure}[hbt!]
        \centering
        \begin{forest}
    for tree={              % style of tree nodes
      draw, semithick, rounded corners,
      align = center,
      inner sep = 2mm,
      anchor=north,
                            % styles of tree
      forked edge,             
%      tier/.option=level,  % if you have more levels of children
        l sep = 6mm,    
     fork sep = 3mm,     
                }
[\texttt{Parent}\\   object
    [\texttt{AStandardChild}\\ object]
    [\texttt{VeryVeryVeryLongChild}\\ object]
    [\textrm{Other}]
]
        \end{forest}
    \end{figure}
\end{document}
| improve this answer | |

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.