此代码
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usepackage{forest}
\begin{document}
We are the knights who say "Ni!".
\begin{center}
\begin{tikzpicture}[framed]
\begin{forest}
[,for tree={grow'=east},shape=coordinate
[
[
[]
[]
]
[
[]
[]
]
]
[
[
[]
[]
]
[
[]
[]
]
]
]
\end{forest}
\end{tikzpicture}
\end{center}
\end{document}
给我
显然,树超出了图片的范围,导致它与上面的文本重叠。这是一个已知的错误还是我的失误?有解决办法吗?
答案1
forest
我认为将环境放在 a 中根本不是一个好主意tikzpicture
,我强烈怀疑坏事可能会出乎意料地发生。一般来说,嵌套tikzpicture
s 有时在简单情况下有效,但众所周知存在问题。(也就是说,如果它有效,它就有效。如果不行,你可以保留两个部分。)请记住 aforest
是 a tikzpicture
,所以你本质上是在说
\begin{tikzpicture}[framed]
...
\begin{tikzpicture}
...
baseline
对于您的示例,如果您适当调整图片,它确实有效。
\begin{tikzpicture}[framed,baseline=(current bounding box.center)]
不过,我认为将框架作为 的一部分来绘制会更安全。我forest
认为你最终想要一些比单纯的盒子更漂亮的东西。如果不是,还有更简单的方法。但复杂的背景可以很容易地作为规范本身的一部分添加forest
。有几种方法可以做到这一点。
\begin{forest}
[
..
]
\begin{scope}[on background layer]
<drawing commands>
\end{scope}
\end{forest}
或者,可以将绘制命令添加为树规范的一部分或树序言中。例如,
\begin{forest}
[,for tree={grow'=east},shape=coordinate, tikz+={%
\begin{scope}[on background layer]
\node [fit=(current bounding box.north east) (current bounding box.south west), draw=blue!50!cyan, outer color=blue!50!cyan!25, inner color=blue!50!cyan!10, rounded corners, line width=1mm] {};
\end{scope}
}
[
...
\end{forest}
完整示例代码:
\documentclass[a4paper]{article}
\usepackage{forest}
\usetikzlibrary{backgrounds,fit}
\begin{document}
We are the knights who say "Ni!".
\begin{center}
\begin{tikzpicture}[framed,baseline=(current bounding box.center)]
\begin{forest}
[,for tree={grow'=east},shape=coordinate
[
[
[]
[]
]
[
[]
[]
]
]
[
[
[]
[]
]
[
[]
[]
]
]
]
\end{forest}
\end{tikzpicture}
\end{center}
We are the knights who say "Ni!".
\begin{center}
\begin{forest}
[,for tree={grow'=east},shape=coordinate, tikz+={%
\begin{scope}[on background layer]
\node [fit=(current bounding box.north east) (current bounding box.south west), draw=blue!50!cyan, outer color=blue!50!cyan!25, inner color=blue!50!cyan!10, rounded corners, line width=1mm] {};
\end{scope}
}
[
[
[]
[]
]
[
[]
[]
]
]
[
[
[]
[]
]
[
[]
[]
]
]
]
\end{forest}
\end{center}
\end{document}