链接两个 tikz 矩阵的列宽

链接两个 tikz 矩阵的列宽

我有两个图形,一个是 tikzfigure,另一个是矩阵。这两个矩阵是相关的,即具有相同的列,我希望 tikz 为它们分配相同的列宽,就像我在一个矩阵中拥有所有行一样。自动解决方案是理想的,我无需提供手动间距或将一个矩阵标记为模板,即 tikz 应该从所有矩阵中收集间距,然后应用它们(可能需要重新编译)。

那可能吗?

以下代码演示了这个问题

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
Currently I have two matrices, this:

\begin{tikzpicture}
\matrix{
\node{first matrix with long and}; & \node{short column}; \\
\node{second}; & \node{row}; \\
};
\end{tikzpicture}

and this:

\begin{tikzpicture}
\matrix{
\node{second matrix}; & \node{with another column long}; \\
};
\end{tikzpicture}


I want that is that both matrices look as if they were cut out of this combined matrix:

\begin{tikzpicture}
\matrix{
\node{first matrix with long and}; & \node{short column}; \\
\node{second}; & \node{row}; \\
\node{second matrix}; & \node{with another column long}; \\
};
\end{tikzpicture}
\end{document}

渲染示例

在例子中,中间的文本很简单,但在实际例子中,两个矩阵可能位于完全不同的位置,例如两个不同的浮动图形。

答案1

思考我有一个解决方案。它的工作原理是测量 pgf 矩阵中列的宽度并将其保存到文件中.aux。然后在后续运行中(或后续矩阵的后续运行中),它​​会在开头插入一个额外的行,该行由正确宽度的路径组成。这里有一些技巧可以确保这个额外的行实际上不会任何方法都可以,但我不能保证这种方法一定有效 - 我通过设置行样式和设置行间距进行了测试。但我的测试并不广泛。

代码如下:

\documentclass{article}
%\url{http://tex.stackexchange.com/q/57871/86}
\thispagestyle{empty}
\usepackage{tikz}

\makeatletter
\pgfkeys{/tikz/save cell widths code/.code={
    \edef\lc@widths{\noexpand\noexpand\noexpand\expandafter\gdef\noexpand\noexpand\noexpand\csname lc@widths@#1@cols\noexpand\noexpand\noexpand\endcsname{\the\pgf@matrix@numberofcolumns}}
    \bgroup
    \foreach \lc@k in {1,...,\pgf@matrix@numberofcolumns} {
      \edef\lc@temp{\noexpand\noexpand\noexpand\expandafter\gdef\noexpand\noexpand\noexpand\csname lc@widths@#1@maxx\lc@k\noexpand\noexpand\noexpand\endcsname{\csname pgf@matrix@maxx\lc@k\endcsname}\noexpand\noexpand\noexpand\expandafter\gdef\noexpand\noexpand\noexpand\csname lc@widths@#1@minx\lc@k\noexpand\noexpand\noexpand\endcsname{\csname pgf@matrix@minx\lc@k\endcsname}}
      \expandafter\expandafter\expandafter\gdef\expandafter\expandafter\expandafter\lc@widths\expandafter\expandafter\expandafter{\expandafter\lc@widths\lc@temp}
    }
    \immediate\write\pgfutil@auxout{\lc@widths}
    \edef\lc@temp{\lc@widths}
    \lc@temp
    \egroup
  }
}

\tikzset{save cell widths/.style={%
    hack end matrix=#1,
    append after command={[save cell widths code=#1]},%
  },
  hack end matrix/.code={%
    \let\lc@matrix=\pgf@matrix@cont
    \def\pgf@matrix@cont{\lc@matrix\lc@use{#1}}%
  }
}

\def\lc@use#1{%
  \@ifundefined{lc@widths@#1@cols}{}{%
    \def\lc@line{}%
    \expandafter\let\expandafter\lc@cols\csname lc@widths@#1@cols\endcsname
    \xdef\lc@line{\noexpand\path (\csname lc@widths@#1@minx1\endcsname,0pt) (\csname lc@widths@#1@maxx1\endcsname,0pt);}
    \foreach \lc@k in {2,...,\lc@cols} {
      \expandafter\gdef\expandafter\lc@line\expandafter{\lc@line\pgfmatrixnextcell}
      \edef\lc@temp{\noexpand\path (\csname lc@widths@#1@minx\lc@k\endcsname,0pt) (\csname lc@widths@#1@maxx\lc@k\endcsname,0pt);}
      \expandafter\expandafter\expandafter\gdef\expandafter\expandafter\expandafter\lc@line\expandafter\expandafter\expandafter{\expandafter\lc@line\lc@temp}

    }
\pgfmathparse{-\pgfmatrixrowsep}
\edef\lc@temp{\noexpand\pgfmatrixendrow[\pgfmathresult pt]}
      \expandafter\expandafter\expandafter\gdef\expandafter\expandafter\expandafter\lc@line\expandafter\expandafter\expandafter{\expandafter\lc@line\lc@temp}
\lc@line
\advance\pgfmatrixcurrentrow by -1\relax
  }
}

\makeatother

\begin{document}
Currently I have two matrices, this:

\begin{tikzpicture}
\matrix[save cell widths=m]{
\node{first matrix with long and}; & \node{short column}; \\
\node{second}; & \node{row}; \\
};
\end{tikzpicture}

and this:

\begin{tikzpicture}
\matrix[save cell widths=m]{
\node{second matrix}; & \node{with another column long}; \\
};
\end{tikzpicture}


I want that is that both matrices look as if they were cut out of this combined matrix:

\begin{tikzpicture}
\matrix[save cell widths=m]{
\node{first matrix with long and}; & \node{short column}; \\
\node{second}; & \node{row}; \\
\node{second matrix}; & \node{with another column long}; \\
};
\end{tikzpicture}

\begin{tikzpicture}
\matrix[nodes={anchor=base},draw,save cell widths=mm,row 2/.style={every node/.style={anchor=base,green}},row sep=1cm] (m) at (0,0) {
\node {a}; & \node {b}; & \node {cde}; \\
\node {a}; & \node {b}; & \node{c}; \\
};
\matrix[nodes={anchor=base},draw,save cell widths=mm] (ma) at (0,-3) {
\node {abc}; & \node {b}; & \node{c}; \\
\node {a}; & \node {b}; & \node{c}; \\
};
\end{tikzpicture}

\begin{tikzpicture}
\matrix[draw,save cell widths=mmm,row sep=1cm] (m) at (0,0) { \node[right] {abcdefgh}; & \node{x}; \\ };
\matrix[draw,save cell widths=mmm] (ma) at (0,-3) { \node[left] {abc}; & \node{x}; \\ };
\end{tikzpicture}
\end{document}

结果如下:

矩阵分裂比对

编辑 2012-06-01根据评论,它现在不再将添加的内容居中,而是直接使用和,maxx从而minx允许单元格内容相对于原点移动(参见代码中的最后一个例子)。

相关内容