我用 Forest 制作了一棵宽树,我想resizebox
将其缩小以匹配文本长度。我注意到树左侧的空间奇怪地更大,特别是与右侧的空间相比。我该如何修复它?
\documentclass{article}
\usepackage{graphicx}
\usepackage{forest}
\begin{document}
Some text
\begin{center}
\resizebox{\textwidth}{!}{
\fbox{
\begin{forest}
for tree = {l sep = 20 mm}
[Root
[A long label for a inner node
[A long label for a leaf]
[A long label for a leaf]
]
[A long label for a inner node
[A long label for a leaf]
[A long label for a leaf]
]
[A long label for a inner node
[A long label for a leaf]
[A long label for a leaf]
]
[A long label for a inner node
[A long label for a leaf]
[A long label for a leaf]
]
]
\end{forest}
}
}
\end{center}
other text.
\end{document}
答案1
一个窍门:加载后写入此内容forest
。
\makeatletter
\preto\forest@pack{\nullfont}
\makeatother
这使得forest
在开始打包之前切换到空字体。(打包是 的核心forest
,其中计算节点的位置。这里进行了大量计算。特别是,使用 PGF 机制计算了大量线交叉点,这是我们通常获得泄漏空间的地方。)
虽然 hack 会消除泄漏的空格,但它会在 的顶部引入一个空行(可能是段落分隔符?) 。我不知道为什么会发生这种情况(我希望有人可以解释)。 hack 中的 hack 是在和参数\fbox
的开头添加注释符号——这会删除空格标记。\resizebox
\fbox
\documentclass{article}
\usepackage{graphicx}
\usepackage{forest}
\makeatletter
\preto\forest@pack{\nullfont}
\makeatother
\begin{document}
Some text
\begin{center}
\resizebox{\textwidth}{!}{% without these comments
\fbox{% we get some extra vertical space
\begin{forest}
for tree = {l sep = 20 mm}
[Root
[A long label for a inner node
[A long label for a leaf]
[A long label for a leaf]
]
[A long label for a inner node
[A long label for a leaf]
[A long label for a leaf]
]
[A long label for a inner node
[A long label for a leaf]
[A long label for a leaf]
]
[A long label for a inner node
[A long label for a leaf]
[A long label for a leaf]
]
]
\end{forest}
}
}
\end{center}
other text.
\end{document}
请注意,虽然这个 hack 在这个特定示例中有效,但它通常不起作用。如果typeset node
在打包后使用,它肯定会失败,因为空字体仍然有效。
如果我设法解决这些问题,我甚至可能会考虑将它整合到forest
它自身中——毕竟,PGF 的未来版本不可避免地会引入一些新的泄漏空间……
答案2
比通过缩放forest
树形图使注释文本难以阅读更好的方法是考虑旋转它,grow=east
并且(如果节点文本真的很长)使节点的文本分成更多行。例如:
\documentclass{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{forest}
\begin{document}
Some text
\begin{center}
\fbox{
\begin{forest}
for tree = {
font=\small,
grow'=east,
child anchor=west,
l sep = 12 mm,
s sep =2mm,
where level = {0}{}{text width=width("A long label ")}
}
[Root
[A long label for a inner node
[A long label for a leaf]
[A long label for a leaf]
]
[A long label for a inner node
[A long label for a leaf]
[A long label for a leaf]
]
[A long label for a inner node
[A long label for a leaf]
[A long label for a leaf]
]
[A long label for a inner node
[A long label for a leaf]
[A long label for a leaf]
]
]
\end{forest}
}
\end{center}
other text.
\end{document}
(红线表示文本边框)