我确实写过
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[edges]{forest}
\begin{document}
\section{trees}
\begin{figure}[!h]
\begin{minipage}[t]{0.5\linewidth}
\begin{forest} for tree={draw, semithick, rounded corners, font = \sffamily, top color = white, bottom color = white, grow = south, s sep = 4mm, l sep = 8mm,} [{S}[{C}[{A}[{B}[{H}[{I}]][{I}][{H}[{I}]]][{I}][{H}[{I}]]][{A}[{H} [{I}]][{I}][{H}[{I}]]]]]; \end{forest}
\end{minipage}
\hspace{0.1cm}
\begin{minipage}[t]{0.5\linewidth}
\begin{forest} for tree={draw, semithick, rounded corners, font = \sffamily, top color = white, bottom color = white, grow = south, s sep = 4mm, l sep = 8mm,} [{S}[{B}[{B}[{H}[{I}]][{I}][{H}[{I}]]][{I}][{H}[{I}]]]]; \end{forest}
\end{minipage}
\end{figure}
\end{document}
我如何才能制作类似的表格,但更重要的是,如何在确定的单元格中容纳任意维度的树?
答案1
你想要这样的东西吗?
基本思想是,如果您想要一个表格,您不妨使用tabular
。tabularx
用于填充当前行的宽度,使用\multicolumn
s 表示更大的树(这里只是现有树之一的重复,因此不是更大)和下面的文本。但请注意,在表格中使用垂直规则并不是好习惯。也就是说,这可能不是真正的表格,所以这可能无关紧要。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{forest}
\usepackage{tabularx,array}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\forestset{%
my tree/.style={
baseline,
tikz+={\node [fit to=tree, inner sep=5pt] {};},
for tree={
draw,
semithick,
rounded corners,
font = \sffamily,
% fill=white, % if presenting on a coloured background
% top color = white, % these two lines are pointless: shading from white to white is just a white fill; on a white page, this makes no difference
% bottom color = white,
% grow = south, % default
% s sep' = 4mm, % if you are trying to save space, the last thing you want is to increase the spaciness of the tree ...
% l sep' = 8mm, & ... in either direction ...
},
},
}
\begin{document}
\section{trees}
% don't use figure unless you want it to float away; never use h as the sole location option - it means 'here, if there's space', but what if there isn't space?!'
\begin{tabularx}\linewidth{|C|C|}
\hline
\begin{forest} my tree [S[C[A[B[H[I]][I][H[I]]][I][H[I]]][A[H [I]][I][H[I]]]]] \end{forest}
&
\begin{forest}
my tree
[S[B[B[H[I]][I][H[I]]][I][H[I]]]]
\end{forest} \\\hline
\multicolumn{2}{|c|}{ \begin{forest} my tree [S[C[A[B[H[I]][I][H[I]]][I][H[I]]][A[H [I]][I][H[I]]]]] \end{forest}} \\\hline
\multicolumn{2}{|l|}{\textsf{Something here}} \\\hline
\end{tabularx}
\end{document}
我不确定你说的“任意维度”的树是什么意思。显然,如果树太大,它就放不下。你可以缩放它,但缩放图表以适应它并不是一个好主意,因为你最终会得到各种字体大小的奇怪大杂烩。如果无法避免这种情况,你可以使用软件包\resizebox
中的选项graphicx
,但tikzscale
提供更多可口的选项。(但是,我不记得它们是否适用于 Forest。)
在执行此操作之前,让 Forest 尽其所能生成一棵紧凑的树,即不是增加l sep
或s sep
。其次,尝试类似font=\sffamily\footnotesize
以一致的方式减小字体大小的方法。如果您使用具有光学尺寸的字体(例如 Computer Modern 或 Latin Modern,尽管如果我没记错的话,无衬线字体的限制更多一些),这也会产生更好的效果。这将有助于避免使用标准尺寸的大杂烩问题,并保持图表之间的线宽不变(无论如何,它们指定相同的线宽)。请注意,正如所指定的,您的树使用混合的线宽 - 边是标准的,节点边框是semithick
。我不确定这是不是故意的。如果您想在整个过程中使用,请在定义中edge+={semithick}
添加或删除`semithick(如果您想在任何地方都使用默认厚度)。for tree
my tree
semithick