在 TikZ 中使用花括号连接节点

在 TikZ 中使用花括号连接节点

我想知道如何在 TikZ 中用花括号连接节点。下图显示了我想要做的事情:

在此处输入图片描述

这是我已经得到的:

\documentclass[class=minimal,border=0pt]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows.meta}

\begin{document}
\begin{tikzpicture}

\tikzstyle{myrect} = [rectangle,draw,thin,minimum width=3cm,minimum height=1cm,align=center]

\node (MD) [myrect] {Texttexttext};
\node (Z1) [myrect,above of=MD,yshift=0.5cm,minimum width=1.2cm] {Z1};
\node (Z2) [myrect,left of=Z1,xshift=-1.5cm,minimum width=1.2cm,opacity=.2,text opacity=.2] {Z2};
\node (Z3) [rectangle,left of=Z1,xshift=-0.5cm,minimum width=1.2cm] {$\cdots$};
\node (X1) [myrect,above of=Z1,xshift=1.5cm,yshift=0.5cm,minimum width=1cm] {X1};
\node (X2) [myrect,left of=X1,minimum width=1cm] {X2};
\node (X3) [myrect,left of=X2,minimum width=1cm] {X3};
\node (X4) [myrect,left of=X3,minimum width=1cm] {X4};
\node (X5) [myrect,left of=X4,minimum width=1cm] {X5};
\draw [decorate,decoration={brace,amplitude=0.5cm,mirror}] (Z1.south west) -- (Z1.south east);
\draw [decorate,decoration={brace,amplitude=0.5cm,mirror}] (X5.south west) -- (X1.south east);

\end{tikzpicture}
\end{document}

括号看起来还是有点糟糕,那么有没有一种自动且好用的方法用括号连接节点?还是我必须通过坐标精确定位它们?

问候!

答案1

“通过括号连接节点”是不可能的,但您可以稍微调整一下raise选项amplitude以获得更好的结果。

下面的代码显示了您的一些更改:

完整的代码如下:

\documentclass[class=minimal,border=2mm]{standalone}
%\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows.meta, positioning, decorations.pathreplacing}

\begin{document}
\begin{tikzpicture}[myrect/.style={rectangle, draw, thin, minimum height=1cm, align=center}]

\node (MD) [myrect, minimum width=3cm] {Texttexttext};
\node (Z1) [myrect, above=4mm of MD, minimum width=1.2cm] {Z1};
\node (Z2) [myrect, left = of Z1, minimum width=1.2cm, opacity=.2, text opacity=.2] {Z2};

\path (Z2) -- (Z1) node[midway]{\dots};

\node (X3) [myrect,above= 4mm of Z1] {X3};
\node (X4) [myrect,left = -\pgflinewidth of X3] {X4};
\node (X5) [myrect,left = -\pgflinewidth of X4] {X5};
\node (X2) [myrect,right = -\pgflinewidth  of X3] {X2};
\node (X1) [myrect,right = -\pgflinewidth of X2] {X1};

\draw [decorate,decoration={brace,amplitude=3mm,mirror, raise=.5mm}] (Z1.south west) -- (Z1.south east);
\draw [decorate,decoration={brace,amplitude=3mm,mirror,raise=.5mm}] (X5.south west) -- (X1.south east);

\end{tikzpicture}
\end{document}

结果

在此处输入图片描述

相关内容