![如何使嵌套矩形正确水平对齐?](https://linux22.com/image/267384/%E5%A6%82%E4%BD%95%E4%BD%BF%E5%B5%8C%E5%A5%97%E7%9F%A9%E5%BD%A2%E6%AD%A3%E7%A1%AE%E6%B0%B4%E5%B9%B3%E5%AF%B9%E9%BD%90%EF%BC%9F.png)
我知道嵌套 tikz 图片是 经过考虑的 坏的,但我还是这么做了,因为我不想手动定位。我嵌套了几个类似于另一个关于圈子的问题。问题在于矩形未正确居中,这可以在左右堆叠的垂直线上看到。
这是嵌套引起的问题还是我做错了其他事情?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
class/.style={
draw,
align=center,
rectangle,
rounded corners=20pt,
minimum size=0pt,
inner sep=5pt,
outer sep=0pt,
},
label/.style={
rectangle,
align=center,
inner sep=6pt,
outer sep=4pt, % shifts labels a bit down
node distance=0pt,
},
]
\node [class] (csl) {
\begin{tikzpicture}
\node [class] (ptime) {
\begin{tikzpicture}
\node [class] (mcfl) {
\begin{tikzpicture}
\node [class] (rpl) {
\begin{tikzpicture}
\node [class] (tal) {
\begin{tikzpicture}
\node [class,rounded corners=5pt] (cfl) {CFG};
\node [label, below=of cfl] {TAG, LIG, CCG, HG };
\end{tikzpicture}
};
\node [label, below=of tal] {RPG, (2,2)-MCFG};
\end{tikzpicture}
};
\node [label, below=of rpl] {MCFG, LCFRS};
\end{tikzpicture}
};
\node [label, below=of mcfl] {RCG, simple LMG \\ (= PTIME)};
\end{tikzpicture}
};
\node [label, below=of ptime] {CSG};
\end{tikzpicture}
};
\end{tikzpicture}
\end{document}
答案1
将下面的代码与您的代码进行比较。仍有改进的空间。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{backgrounds,fit}
\tikzset{every fit/.append style=text badly centered}
\tikzset{class/.style={
draw,
rectangle,
rounded corners=20pt,
inner ysep=5pt,
},
label/.style={
align=center,
inner sep=6pt,
outer ysep=4pt,
node distance=0pt,
}
}
\begin{document}
\begin{tikzpicture}
\node [class,rounded corners=5pt] (cfl) {CFG};
\node [label, below=of cfl] (tal) {TAG, LIG, CCG, HG };
\node [label, below=of tal] (rpl) {RPG, (2,2)-MCFG};
\node [label, below=of rpl] (mcfl) {MCFG, LCFRS};
\node [label, below=of mcfl] (ptime) {RCG, simple LMG \\ (= PTIME)};
\node [label, below=of ptime] (csl) {CSG};
\begin{pgfonlayer}{background}
\node [class, inner xsep=5em, fit=(cfl) (tal) (rpl) (mcfl) (ptime) (csl)] {};
\node [class, inner xsep=4em, fit=(cfl) (tal) (rpl) (mcfl) (ptime)] {};
\node [class, inner xsep=3em, fit=(cfl) (tal) (rpl) (mcfl)] {};
\node [class, inner xsep=2em, fit=(cfl) (tal) (rpl)] {};
\node [class, inner xsep=1em, fit=(cfl) (tal)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}