我正在尝试重叠多个节点以可视化三个级别的子集,并为每个子集包含一些文本。有没有办法强制 fit 命令增加填充?或者手动调整每个框的大小是否更有效?感谢您的支持。
\documentclass[a4paper, 12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning, fit, calc}
\begin{document}
\begin{tikzpicture}
[every node/.style={align=center, inner xsep=6mm, inner ysep=3mm, rounded corners}, very thick]
\node (inner1) [draw] {text text text text text text text text};
\node (outer) [fit=(inner1)] {};
\coordinate (o) at ($(inner1.north)!1!(inner1.south)$);
\draw [blue, rounded corners] (outer.north west) rectangle (outer.east |- o) node [above=5mm of inner1, anchor=center] {text text text text text text text text};
\node (doubleouter) [fit=(outer)(inner1)] {};
\draw [red, rounded corners] (doubleouter.north west) rectangle (doubleouter.north east |- o) node [above=15mm of inner1, anchor=center] {text text text text text text text text};
\end{tikzpicture}
\end{document}
答案1
由于您想要实现不对称的节点间分离(下部为 0,上部为 xmm),因此无法直接实现。您需要计算节点坐标或使用多个节点。
这是一个具有多个节点的解决方案。
框节点与框相对应。它们具有 0pt 内部 ysep,因此可以简单地包含它们而无需添加额外的垂直间距。它们具有内部 xsep,以便具有不断增长的尺寸。
文本节点具有内部 y 分隔,以便正确显示文本。
您必须首先绘制文本,然后绘制框,将该文本和前一个框括起来,这要归功于合适的库。
\documentclass[a4paper, 12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning, fit, calc}
\begin{document}
\begin{tikzpicture}
[
box/.style={inner xsep=6mm, inner ysep=0, draw=#1, rounded corners, very thick},
txt/.style={align=center, inner xsep=0mm, inner ysep=3mm},
]
\node[txt] (inner1 txt) {text text text text text text text text};
\node[box=black,fit=(inner1 txt)] (inner1 box) {} ;
\node[txt, above=0 of inner1 box] (inner2 txt) {\ldots} ;
\node[box=blue,fit={(inner1 box) (inner2 txt)}] (inner2 box) {} ;
\node[txt, above=0 of inner2 box] (inner3 txt) {text text text text text text text text} ;
\node[box=red,fit={(inner2 box) (inner3 txt)}] (inner3 box) {} ;
\end{tikzpicture}
\end{document}
答案2
一个替代方案(@Alain Merigot 不错的解决方案的变体:+1)使用chains
和库:fit
positioning
\documentclass[a4paper, 12pt]{article}
\usepackage{tikz}
\usetikzlibrary{chains,
fit,
positioning}
\begin{document}
\begin{tikzpicture}[
node distance = 1mm and 0mm,
start chain = A going above,
FIT/.style args = {#1/#2}{draw=#1, rounded corners, ultra thick,
fit=#2,
inner xsep=6mm, inner ysep=0pt, outer sep=0pt,
node contents={}},
txt/.style = {minimum height=3ex, align=center, on chain=A},
]
\node[txt] {text text text text text text text text}; % A-1
\node[txt] {\dots}; %
\node[txt] {text text text text text text text text};
%
\node (f1) [FIT=black/(A-1)];
\node (f2) [FIT=blue/(f1) (A-2)];
\node (f3) [FIT=red/(f2) (A-3)];
\end{tikzpicture}
\end{document}
答案3
可能的 :
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \i/\L/\c in {1/A/black,2/B/red,3/C/blue}
{\def\mw{4+2*\i}
\node (inner\i at (2,0) [draw,color=\c,thick,minimum width= \mw cm,minimum height=\i cm, align=center, text depth=\i cm, rounded corners,anchor=south] {Set $\L$};}
\end{tikzpicture}
\end{document}