TikZ 多行矩阵

TikZ 多行矩阵

我正在尝试适应提出的解决方案@JLDiaz在 TikZ 矩阵中跨多行跨越一个单元格为了在 TikZ 中拥有多个多行matrix

这是我目前拥有的:

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,calc,fit}

\begin{document}

\newlength{\csep}
\setlength{\csep}{1.5mm}

\newlength{\twidth}
\setlength{\twidth}{25mm}

\begin{tikzpicture}[auto, node distance=2cm,font=\small,
    every node/.style={inner sep=0pt,rectangle, minimum height=2.5em, text centered},
    comp/.style={draw,very thick,text width=\twidth,fill=blue!10}]
\matrix (m) [ampersand replacement=\&,column sep=\csep, row sep=3mm]
{
\node (l5) [comp] {5}; \& \\
\node (l4) [comp] {4}; \& \\
\node (l3) [comp] {3}; \& \node[comp]{3'};\\
\node (l2) [comp] {2};\\
\node (l1) [comp] {1};\\
};
\coordinate (aux5) at  ($(l5.north east) + (\csep,0)$);
\coordinate (aux4) at ($(l4.south east) + (\csep+\twidth,0)$);
\node[comp, fit=(aux4)(aux5), inner sep=-.6pt] (X45) {}; 
\node[text width=3cm, text centered, anchor=center] at (X45.center) {4' 5'};

\coordinate (aux2) at  ($(l2.north east) + (\csep,0)$);
\coordinate (aux1) at ($(l1.south east) + (\csep+\twidth,0)$);
\node[comp, fit=(aux1)(aux2),  inner sep=-.6pt]
(X12) {}; 
\node[text width=3cm, text centered, anchor=center] at (X12.center) {1' 2'};

\end{tikzpicture}

\end{document}

在此处输入图片描述

但是,方框“4' 5'”和“3'”的宽度并不相等:

在此处输入图片描述

如何使第二列的所有框具有相同的宽度?

并且:有没有比创建这些aux坐标更简单的解决方案来获取一些跨越多行的框?

答案1

由于负值,这些框的宽度不相等inner sep。将它们设为零。此外,在坐标定义中,aux您必须添加1.5\pgflinewidth如下内容:

\coordinate (aux5) at  ($(l5.north east) + (\csep+1.5\pgflinewidth,-1.5\pgflinewidth)$);
\coordinate (aux4) at ($(l4.south east) + (\csep+\twidth+1.5\pgflinewidth,1.5\pgflinewidth)$);
\node[comp, fit=(aux4)(aux5), inner sep=0pt,outer sep=0pt] (X45) {};
\node[text width=3cm, text centered, anchor=center] at (X45.center) {4' 5'};

完整代码:

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,calc,fit}

\begin{document}

\newlength{\csep}
\setlength{\csep}{1.5mm}

\newlength{\twidth}
\setlength{\twidth}{25mm}

\begin{tikzpicture}[auto, node distance=2cm,font=\small,
    every node/.style={inner sep=0pt,rectangle, minimum height=2.5em, text centered},
    comp/.style={draw,very thick,text width=\twidth,fill=blue!10}]
\matrix (m) [ampersand replacement=\&,column sep=\csep, row sep=3mm]
{
\node (l5) [comp] {5}; \& \\
\node (l4) [comp] {4}; \& \\
\node (l3) [comp] {3}; \& \node[comp]{3'};\\
\node (l2) [comp] {2};\\
\node (l1) [comp] {1};\\
};
\coordinate (aux5) at  ($(l5.north east) + (\csep+1.5\pgflinewidth,-1.5\pgflinewidth)$);
\coordinate (aux4) at ($(l4.south east) + (\csep+\twidth+1.5\pgflinewidth,1.5\pgflinewidth)$);
\node[comp, fit=(aux4)(aux5), inner sep=0pt,outer sep=0pt] (X45) {};
\node[text width=3cm, text centered, anchor=center] at (X45.center) {4' 5'};

\coordinate (aux2) at  ($(l2.north east) + (\csep+1.5\pgflinewidth,-1.5\pgflinewidth)$);
\coordinate (aux1) at ($(l1.south east) + (\csep+\twidth+1.5\pgflinewidth,1.5\pgflinewidth)$);
\node[comp, fit=(aux1)(aux2),  inner sep=0pt]
(X12) {};
\node[text width=3cm, text centered, anchor=center] at (X12.center) {1' 2'};

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

我不知道决定使用什么matrix作为所需图像的基础。不使用矩阵进行绘制可能相对简单(在我看来比使用矩阵更简单):

\documentclass[border=3mm,
               tikz,
               preview]{standalone}
\usetikzlibrary{calc,chains}

    \begin{document}
\newlength{\twidth}
\setlength{\twidth}{25mm}
    \begin{tikzpicture}[
    node distance = 5mm,
      start chain = going above,       
every node/.style = {rectangle, draw, ultra thick, fill=blue!10,
                     text width=\twidth, minimum height=2.5em, 
                     inner sep=1mm, outer sep=0mm,
                     text centered, font=\small,
                     on chain},
                        ]
% first column
\node (l1) {1};\\
\node (l2) {2};\\
\node (l3) {3};\\
\node (l4) {4};\\
\node (l5) {5};\\
% second column
\path   let \p1 = (l1.south),
            \p2 = (l2.north),
            \n1 = {veclen(\x2-\x1,\y2-\y1)} in
        node[above right =0mm and 3mm of l1.south east,
             minimum height=\n1] {1' 2'}
        node {3'}
        node[minimum height=\n1] {4' 5'};
    \end{tikzpicture}
\end{document}

可以看出,节点被放在链中。第二列中较高节点的高度是通过第一列中前两个相邻节点之间的距离来计算的。除非第一列中的节点高度相等,否则这种简化不会带来麻烦。

在此处输入图片描述

答案3

以下代码显示了 Harish Kumar 解决方案的替代方案。它也使用fit库并在之后绘制多行节点matrix,但显示了如何在没有calc库的情况下以及在节点文本中使用第二个节点的情况下执行此操作。

\node[comp, fit={(l2.north-|l32.west) (l1.south-|l32.east)},  
      inner sep=0pt, label=center:{1' 2'}] (X12) {};

定义一个具有两个角的拟合节点,用perpendicular坐标系声明。withLabel选项center用于放置节点的内容。

为了使fit节点与第二列节点大小相同,outer sep=0pt必须添加comp样式。

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,fit}

\begin{document}

\newlength{\csep}
\setlength{\csep}{1.5mm}

\newlength{\twidth}
\setlength{\twidth}{25mm}

\begin{tikzpicture}[auto, node distance=2cm,font=\small,
    every node/.style={inner sep=0pt,rectangle, minimum height=2.5em, text centered},
    comp/.style={draw,very thick,text width=\twidth,fill=blue!10,outer sep=0pt}]
\matrix (m) [ampersand replacement=\&,column sep=\csep, row sep=3mm]
{
\node (l5) [comp] {5}; \& \\
\node (l4) [comp] {4}; \& \\
\node (l3) [comp] {3}; \& \node (l32) [comp]{3'};\\
\node (l2) [comp] {2};\\
\node (l1) [comp] {1};\\
};
\node[comp, fit={(l5.north-|l32.west) (l4.south-|l32.east)}, 
       inner sep=0pt, outer sep=0pt, label=center:{4' 5'}] (X45) {};

\node[comp, fit={(l2.north-|l32.west) (l1.south-|l32.east)},  
       inner sep=0pt, label=center:{1' 2'}]
(X12) {};

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容