TikZ Matrix - 如何水平对齐跨越多个单元格/行的内容?

TikZ Matrix - 如何水平对齐跨越多个单元格/行的内容?

我想微调最后一列的大矩形及其内容,即使其全部与第三列的中心对齐。它目前太靠右了:

在此处输入图片描述

到目前为止我还无法找到缺失的选项。

平均能量损失

\documentclass[
11pt
]{scrartcl}
\usepackage{
tikz,
relsize,
tgheros
}

\usetikzlibrary{
    calc,trees,shadows,positioning,arrows,chains,shapes.geometric,
    decorations.pathreplacing,decorations.pathmorphing,shapes,
    matrix,shapes.symbols,patterns,intersections%,fit
    }

\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}

\tikzset{
    rolle/.style=
    {
        rectangle,
        drop shadow={opacity=0.5},
        minimum width=4em,
        minimum height=4em,
        text width=4cm,
        draw=black!80,
        very thick,
        fill=black!40,
        font=\sffamily\bfseries,
        align=center
    },
    einheit/.style=
    {
        rectangle,
        rounded corners, 
        minimum height=2em,
        minimum width=4em,
    draw=black!80,
        fill=black!20,
    font=\sffamily
    },
}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small,node distance=0.5cm]
    \draw[thick] (-7.5,-4) rectangle (7.5,4);
    \draw[thick,fill=black!20] (-7.5,4) rectangle (7.5,5) node[midway] {something};
    \matrix (m) [matrix of nodes,nodes in empty cells,row sep=5mm,column sep=5mm] {
    %
    \node[rolle] {123}; & \node[rolle] {456}; & \node[rolle] {789}; \\
    %
    \node[einheit] {words}; & & \node[minimum width=5cm] (abc) {}; \\
    %
    & \node[einheit] {words}; & \\
    %
    & \node[einheit] {words}; & \\
    %
    & \node[einheit] {companies}; & \\
    %
    \node[einheit] {thing}; & & \\
    };
    \draw[einheit] (abc.north east) rectangle (m-6-3.south west) node[midway]{abc};
\end{tikzpicture}
\end{center}
\end{document}

答案1

我会改变你所做的几件事:

\documentclass[11pt]{scrartcl}
\usepackage{tikz,relsize,tgheros}

\usetikzlibrary{calc,trees,shadows,positioning,arrows,chains,shapes.geometric,
    decorations.pathreplacing,decorations.pathmorphing,shapes,
    matrix,shapes.symbols,patterns,intersections%,fit
    }

\pgfdeclarelayer{background layer}
\pgfdeclarelayer{foreground layer}
\pgfsetlayers{background layer,main,foreground layer}

\tikzset{
    rolle/.style=
    {
        rectangle,
        drop shadow={opacity=0.5},
        minimum width=4em,
        minimum height=4em,
        text width=4cm,
        draw=black!80,
        very thick,
        fill=black!40,
        font=\sffamily\bfseries,
        align=center
    },
    einheit/.style=
    {
        rectangle,
        rounded corners, 
        minimum height=2em,
        minimum width=4em,
        draw=black!80,
        fill=black!20,
        font=\sffamily
    },
}

\begin{document}
\begin{center}
  \begin{tikzpicture}
    [
      font=\sffamily\small,
      node distance=0.5cm
    ]
    \draw[thick] (-7.5,-4) rectangle (7.5,4);
    \draw[thick,fill=black!20] (-7.5,4) rectangle (7.5,5) node[midway] {something};
    \matrix (m) 
      [
        matrix of nodes,
        nodes in empty cells,
        row sep=5mm,
        column sep=5mm
      ] 
      {           
        % 
        \node[rolle] {123};     & \node[rolle] {456};         & \node[rolle] {789};                   \\
        \node[einheit] {words}; &                             & \node[minimum width=3cm] (abc) {abc}; \\
                                & \node[einheit] {words};     &                                       \\
                                & \node[einheit] {words};     &                                       \\
                                & \node[einheit] {companies}; &                                       \\
        \node[einheit] {thing}; &                             & \node[minimum width=3cm] (xyz) {xyz}; \\
      };
    \draw[einheit] (abc.north west) rectangle (xyz.south east) node[midway]{abc};
%    \draw[einheit] (abc.north east) rectangle (m-6-3.south west) node[midway]{abc};
  \end{tikzpicture}
\end{center}
\end{document}

我会改变你试图居中的框角的锚定方式。我又加了另一个假的框,与xyz相同。我还将改为。minimum widthabcminimum width3cm

在此处输入图片描述

答案2

我想展示几个我认为更适合自定义调整的选项,因为这里矩阵的使用似乎只是为了定位,而不是为了利用矩阵结构本身。

其中一个问题是,如果您使用matrix of nodes,则只需要指定内容。如果您需要传递额外的选项,您可以使用|...|我在第一列中所做的语法。

此外,还有一些有用的键可用于批量将样式应用到节点。我放了一些行和列样式。有时需要解决这些冲突样式的交集,您需要在列中指定(因为它们稍后发布,所以它们会胜出)。这些不是最佳应用程序,但只需进行简单的手动调整即可节省大量自动化时间。

我还绘制了一个大矩形作为附加路径,并将文本放在上面。

\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{matrix,shadows}
\tikzset{
    rolle/.style={rectangle,drop shadow={opacity=0.5},
        minimum size=4em,text width=4cm,
        draw=black!80,very thick,
        fill=black!40,font=\sffamily\bfseries,
        align=center,anchor=center
    },
    einheit/.style={rectangle,rounded corners, 
        minimum height=2em,minimum width=4em,
        draw=black!80,fill=black!20,
        font=\sffamily,anchor=center
    },
}
\begin{document}
\begin{center}
  \begin{tikzpicture}[font=\sffamily\small]
    \matrix (m) 
      [
        matrix of nodes,nodes in empty cells,
        row sep=5mm,
        column sep=5mm,
        row 1/.style={nodes={rolle}},% declare the default style for row 1 nodes
        column 2/.style={nodes={einheit}, % declare the default style for col 2 nodes
                         row 1/.append style={nodes={rounded corners=false}},% Handle the ambiguities
                         row 2/.style={nodes={fill=none,draw=none}},% Don't append but overwrite!
                         row 6/.style={nodes={fill=none,draw=none}},
                        },
      ]
      {
        123               & 456       & 789 \\
        |[einheit]| words &           &     \\
                          & words     &     \\
                          & words     &     \\
                          & companies &     \\
        |[einheit]| thing &           &     \\
      };

    \draw[einheit] (m-1-3.south west|-m-2-1.north) rectangle (m-1-3.south east|-m-6-1.south) 
                    node at (m-4-3) {ABC};
  \end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

无耻的插件:这是另一个有趣的练习,用于比较行/列样式如何在 LaTeX 中创建比较表?

答案3

代码

\documentclass[11pt,tikz,convert=false]{standalone}
\usepackage{tgheros}
% https://github.com/Qrrbrbirlbel/pgf/blob/master/tikzlibrarypositioning-plus.code.tex
\usetikzlibrary{shadows,positioning-plus,matrix}
\tikzset{
  add minimum width/.style={minimum width/.expanded={\pgfkeysvalueof{/pgf/minimum width}+#1}},
  rows/.style 2 args={
    @/.style={row ##1/.style={#2}},
    @/.list={#1}
  }
}
\tikzset{
  rolle/.style={
    rectangle,
    drop shadow={opacity=0.5},
    minimum height=4em,
    minimum width=4cm,
    draw=black!80,
    very thick,
    fill=black!40,
    font=\sffamily\bfseries,
    align=center
  },
  einheit/.style={
    rectangle,
    rounded corners, 
    minimum height=2em,
    minimum width=4em,
    draw=black!80,
    fill=black!20,
    font=\sffamily
  }
}
\begin{document}
\begin{tikzpicture}[font=\sffamily\small]
    \matrix[inner sep=5mm] (m) [
      matrix of nodes,
      nodes={inner sep=+.3333em},
      rows={2,...,6}{nodes=einheit},
      row 1/.style={nodes=rolle},% or rows={1}{nodes=rolle}
      row sep=5mm,
      column sep=5mm
    ] {
     123  &    456    & 789 \\
    words &           & \\
          &   words   & \\
          &   words   & \\
          & companies & \\
    thing &           & \\
   };

   \node[einheit,span vertical=(m-2-1)(m-6-1),minimum width=3cm] at (fit bounding box -| m-1-3) {abc};
   \node[draw,thick,fill=black!20,above=+0pt of -m, minimum height=1cm] (s) {something};
   \node[draw,thick,span=(m)(s)] at (fit bounding box) {};
\end{tikzpicture}
\begin{tikzpicture}[font=\sffamily\small]
    \matrix[inner sep=0mm] (m) [
      matrix of nodes,
      nodes={inner sep=+.3333em},
      rows={2,...,6}{nodes=einheit},
      row 1/.style={nodes=rolle},% or rows={1}{nodes=rolle}
      row sep=5mm,
      column sep=5mm
    ] {
     123  &    456    & 789 \\
    words &           & \\
          &   words   & \\
          &   words   & \\
          & companies & \\
    thing &           & \\
   };

   \node[einheit,span vertical=(m-2-1)(m-6-1),minimum width=3cm] at (fit bounding box -| m-1-3) {abc};
   \node[draw,thick,fill=black!20,above=+0pt of -(m-1-1)(m-1-3),add minimum width=5mm,minimum height=1cm] (s) {something};
   \node[draw,thick,span=(m-6-1)(s)] at (fit bounding box) {};
\end{tikzpicture}
\end{document}

输出

inner sep=5mm

在此处输入图片描述

inner sep=0mm

在此处输入图片描述

相关内容