tikz 节点/容器的宽度对齐

tikz 节点/容器的宽度对齐

如何调整所有蓝色虚线容器的宽度相对于最大宽度的容器(附图中标有‘外箱 1’的容器)的宽度。

还希望将相同的操作应用于带有标签 1 的容器和带有标签 2 的容器。

\documentclass[tikz, margin=3mm]{standalone}    
\usetikzlibrary{arrows,
calc,
fit,
positioning,
shapes.geometric}

\tikzset{
csbox/.style={
    rectangle,
    draw, thick, 
    minimum height=8mm, minimum width=35mm,
    text width =\pgfkeysvalueof{/pgf/minimum width}-2*\pgfkeysvalueof{/pgf/inner xsep},
    align=center
},  
ocbox/.style={
    rectangle,       
    minimum height=8mm, minimum width=28mm,
    text width =\pgfkeysvalueof{/pgf/minimum width}-2*\pgfkeysvalueof{/pgf/inner xsep},
    align=center
},      
container/.style={
    rectangle,
    draw, blue, dashed, thick,
    inner sep=3mm, yshift=1mm,
    node contents={}
},      
container2/.style={
    rectangle,
    draw, thick,
    inner sep=3mm, yshift=1mm,
    node contents={}
},      
}

\begin{document}    
\begin{tikzpicture}[
node distance=8mm and 4mm,
]

\node (c1) [csbox] {Box 1};
\node (c2) [csbox, left=of c1] {Box 2};
\node (c3) [csbox, right=of c1, , fill=black!20] {Box 3};   
\node (c4) [csbox, right=of c3, , fill=black!20] {Box 4};
\node (container1) [container,yshift=-1mm, fit=(c1) (c2) (c3) (c4)];
\node (oc1) [ocbox, left=of container1] {Outer Box 1};

\node (d1) [csbox, above=of c1, xshift=2cm] {Box 5};
\node (d3) [csbox, left=of d1, xshift=-1.5cm] {Box 6};
\node (d6) [csbox, right=of d1, fill=black!20, xshift=1.5cm] {Box 7};
\node (container2) [container,yshift=-1mm, fit=(d1) (d3) (d6)];
\node (oc2) [ocbox, left=of container2] {Outer Box 2};

\node (v1) [csbox, above=of d1] {Box 8};
\node (v2) [csbox, above=of d3] {Box 9};
\node (v3) [csbox, above=of d6, fill=black!20] {Box 10};
\node (container3) [container,yshift=-1mm, fit=(v1) (v2) (v3)];
\node (oc3) [ocbox, left=of container3] {Outer Box 3};

\node (n1) [csbox, below left=of c4, xshift=1.5cm, minimum width=45mm] {Box 11};
\node (n2) [csbox, below right=of c2, xshift=-1.5cm] {Box 12};  
\node (container4) [container,yshift=-1mm, fit=(n1)(n2)];
\node (oc4) [ocbox, left=of container4] {Outer Box 4};

\node (u1) [csbox, above=of v1] {Box 13};
\node (u2) [csbox, above=of v2] {Box 14};
\node (u3) [csbox, above=of v3, fill=black!20] {Box 15};
\node (container5) [container,yshift=-1mm, fit=(u1) (u2) (u3)];
\node (oc5) [ocbox, left=of container5] {Outer Box 5};  

\node (ooc1)[container2,yshift=-1mm, fit=(container1) (container2) (container3) (container4) (container5) (oc1) (oc2) (oc3) (oc4) (oc5)];

\node (s1) [csbox, above=of ooc1, minimum width=60mm,xshift=1.5cm] {Box 16};
\node (container6) [container,yshift=-1mm, fit=(s1)];
\node (oc6) [ocbox, left=of container6] {Outer Box 6};

\node (s1) [csbox, above=of s1,xshift=-1.5cm] {Box 16};
\node (s2) [csbox, left=of s1] {Box 17};
\node (s3) [csbox, right=of s1,minimum width=15mm] {....};
\node (s4) [csbox, right=of s3, minimum width=45mm] {Box 18};
\node (container7) [container,yshift=-1mm, fit=(s1) (s2) (s3) (s4)];
\node (oc7) [ocbox, left=of container7] {Outer Box 18};

\node (ooc2)[container2,yshift=-1mm, fit=(container6) (container7) (oc6) (oc7)];

\node[below right, inner sep=5pt] at (ooc2.north west) {label1};
\node[below right, inner sep=5pt] at (ooc1.north west) {label2};

\end{tikzpicture}       
\end{document}

在此处输入图片描述

答案1

请注意,我只是做了“代码修复”。我只是引入了另一个容器,适合您的容器,我没有绘制它。然后我使用这个附加容器的左极值和右极值使容器节点具有相同的宽度。并且oc我只使用标签而不是某些节点。我使用列表,\foreach以便不一遍又一遍地写同样的东西。

\documentclass[tikz, margin=3.14mm]{standalone} % margin=3mm is wrong ;-)
\usetikzlibrary{arrows,
calc,
fit,
positioning,
shapes.geometric}

\tikzset{
csbox/.style={
    rectangle,
    draw, thick, 
    minimum height=8mm, minimum width=35mm,
    text width =\pgfkeysvalueof{/pgf/minimum width}-2*\pgfkeysvalueof{/pgf/inner xsep},
    align=center
},  
ocbox/.style={
    rectangle,       
    minimum height=8mm, minimum width=28mm,
    text width =\pgfkeysvalueof{/pgf/minimum width}-2*\pgfkeysvalueof{/pgf/inner xsep},
    align=center
},      
container/.style={% we don't draw them any more but use them for bookkeeping
    rectangle,opacity=0, 
    %draw, blue, dashed, thick,
    inner sep=3mm, yshift=1mm,outer sep=0pt,
    node contents={}
},      
container2/.style={
    rectangle,
    draw, thick,
    inner sep=3mm, yshift=1mm,
    node contents={}
},
fake box/.style={inner sep=0pt,node contents={}},
true container/.style={
    draw, blue, dashed, thick,
    inner sep=3mm, yshift=1mm,inner sep=0pt,
    node contents={},label={[align=right,name=oc\X]left:#1}
},     
}

\begin{document}    
\begin{tikzpicture}[
node distance=8mm and 4mm,
]

\node (c1) [csbox] {Box 1};
\node (c2) [csbox, left=of c1] {Box 2};
\node (c3) [csbox, right=of c1, , fill=black!20] {Box 3};   
\node (c4) [csbox, right=of c3, , fill=black!20] {Box 4};
\node (container1) [container,yshift=-1mm, fit=(c1) (c2) (c3) (c4)];
%\node (oc1) [ocbox, left=of container1] {Outer Box 1};

\node (d1) [csbox, above=of c1, xshift=2cm] {Box 5};
\node (d3) [csbox, left=of d1, xshift=-1.5cm] {Box 6};
\node (d6) [csbox, right=of d1, fill=black!20, xshift=1.5cm] {Box 7};
\node (container2) [container,yshift=-1mm, fit=(d1) (d3) (d6)];
%\node (oc2) [ocbox, left=of container2] {Outer Box 2};

\node (v1) [csbox, above=of d1] {Box 8};
\node (v2) [csbox, above=of d3] {Box 9};
\node (v3) [csbox, above=of d6, fill=black!20] {Box 10};
\node (container3) [container,yshift=-1mm, fit=(v1) (v2) (v3)];
%\node (oc3) [ocbox, left=of container3] {Outer Box 3};

\node (n1) [csbox, below left=of c4, xshift=1.5cm, minimum width=45mm] {Box 11};
\node (n2) [csbox, below right=of c2, xshift=-1.5cm] {Box 12};  
\node (container4) [container,yshift=-1mm, fit=(n1)(n2)];
%\node (oc4) [ocbox, left=of container4] {Outer Box 4};

\node (u1) [csbox, above=of v1] {Box 13};
\node (u2) [csbox, above=of v2] {Box 14};
\node (u3) [csbox, above=of v3, fill=black!20] {Box 15};
\node (container5) [container,yshift=-1mm, fit=(u1) (u2) (u3)];
%\node (oc5) [ocbox, left=of container5] {Outer Box 5};  


\xdef\Lst{}
\foreach \X in {1,...,5}
{\xdef\Lst{\Lst (container\X)}}
\node (phantom) [fake box,fit=\Lst];
\foreach \X in {1,...,5}
{\node [true container={Outer box \X\\ another line},fit=(container\X) (phantom.west |- container\X.center)
(phantom.east |- container\X.center)];}

\node (ooc1)[container2,yshift=-1mm, fit=(container1) (container2) (container3) (container4) (container5) (oc1) (oc2) (oc3) (oc4) (oc5)];

%upper part
\node (s1) [csbox, above=of ooc1, minimum width=60mm,xshift=1.5cm] {Box 16};
\node (container6) [container,yshift=-1mm, fit=(s1)];
%\node (oc6) [ocbox, left=of container6] {Outer Box 6};

\node (s1) [csbox, above=of s1,xshift=-1.5cm] {Box 16};
\node (s2) [csbox, left=of s1] {Box 17};
\node (s3) [csbox, right=of s1,minimum width=15mm] {....};
\node (s4) [csbox, right=of s3, minimum width=45mm] {Box 18};
\node (container7) [container,yshift=-1mm, fit=(s1) (s2) (s3) (s4)];
%\node (oc7) [ocbox, left=of container7] {Outer Box 18};

\xdef\Lst{}
\foreach \X in {6,7}
{\xdef\Lst{\Lst (container\X)}}
\node (phantom) [fake box,fit=\Lst];
\foreach \X in {6,7}
{\node [true container={Outer box \X},fit=(container\X) (phantom.west |- container\X.center)
(phantom.east |- container\X.center)];}

\node (ooc2)[container2,yshift=-1mm, fit=(container6) (container7) (oc6) (oc7)];

%OK
\node[below right, inner sep=5pt] at (ooc2.north west) {label1};
\node[below right, inner sep=5pt] at (ooc1.north west) {label2};

\end{tikzpicture}       
\end{document}

在此处输入图片描述

我其实想鼓励你制定一个策略开始编写大量重复的代码。这将使您的生活更轻松。

相关内容