问题
我正在尝试使用forest
包绘制树形图。我有多行子元素(包括命令\texttt
)和一行子元素(包含普通文本)。这导致子元素之间错位,如下图所示。
我希望所有子元素都与顶部对齐。我该如何实现?
梅威瑟:
% 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}
尝试的解决方案
我尝试使用一些for tree
受启发的附加选项这个答案。在此尝试中,我需要强制长树元素的换行符 ( \\
),因为这不会自动发生。错位减少了,但仍然存在。
梅威瑟:
% 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}
答案1
像这样?
您只需要添加(在您的简单示例中) anchor=north
节点样式:
\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}