Tikz 数学节点矩阵中 \\ 的可选参数在最后一行后保留额外空间

Tikz 数学节点矩阵中 \\ 的可选参数在最后一行后保留额外空间

我正在使用解决方案形式使用 TiKZ 数学节点矩阵模拟 amsmath 矩阵的障碍tikz matrix以 的风格创造一种bmatrix

我想在最后一行之后留出一些额外的空间,并认为使用\\[1.5ex]应该可以完成这项工作:

\matrix (m) {
   1 &  4 \\ 
   7 & 10 \\ 
  13 & 16 \\[1.5ex]% <--- This seems to have no effect.
};

但这似乎没有任何效果。将此可选参数用于除最后一行之外的任何行似乎都可以。

在此处输入图片描述

bmatrix版本的行为符合预期。

相关问题

代码:

\documentclass{article}
\usepackage{mathtools}% for amsmath {bmatrix}
\usepackage{tikz}
\usetikzlibrary{matrix}

%% https://tex.stackexchange.com/questions/26866/obstacles-to-simulating-an-amsmath-matrix-by-a-tikz-matrix-of-math-nodes
\newlength\mtxrowsep   \setlength\mtxrowsep{1.5ex}
\newlength\mtxcolsep   \setlength\mtxcolsep{2\arraycolsep}

\tikzset{
  ams/.style={
    baseline=-.7ex,
    every delimiter/.style={yshift=-1pt},
    every left delimiter/.style={xshift=2pt},
    every right delimiter/.style={xshift=-2pt},
    every node/.style={inner sep=0pt},
  },
  ams matrix/.style={
    inner sep=1pt,
    column sep=\mtxcolsep,
    row sep=\mtxrowsep,
    % Following is also commented at https://tex.stackexchange.com/questions/26866
    %ampersand replacement=\&,
    matrix of math nodes,
  },
  bmatrix/.style={
    ams,
    every matrix/.style={
      ams matrix,
      left delimiter={[},
      right delimiter={]},
    }
  },
  Bmatrix/.style={
    ams,
    every matrix/.style={
      ams matrix,
      left delimiter={\lbrace},
      right delimiter={\rbrace},
    }
  },
  pmatrix/.style={
    ams,
    every matrix/.style={
      ams matrix,
      left delimiter={(},
      right delimiter={)},
    }
  },
  vmatrix/.style={
    ams,
    every matrix/.style={
      ams matrix,
      left delimiter={|},
      right delimiter={|},
    }
  },
  Vmatrix/.style={
    ams,
    every matrix/.style={
      ams matrix,
      left delimiter={\|},
      right delimiter={\|},
    }
  },
}

%% This does not seem to be necessary here
%\let\matamp=&
%
%\catcode`\&=13
%\makeatletter
%\def&{\iftikz@is@matrix
%  \pgfmatrixnextcell
%  \else
%  \matamp
%  \fi}
%\makeatother


\begin{document}
\hspace*{0.2em}
\begin{tikzpicture}[bmatrix]
\matrix (m) {
   1 &  4 \\ 
   7 & 10 \\ 
  13 & 16 \\[1.5ex]% <--- This seems to have no effect.
};
\end{tikzpicture}
%% ----------------
\hspace*{0.3em}
$\begin{bmatrix}
   \smash{1} &  \smash{4} \\ 
   7 & 10 \\ 
  13 & 16 \\[1.5ex]
\end{bmatrix}$

tikz matrix \hspace*{0.6em} bmatrix

\end{document}

临时附录:

我尝试使用 Qrrbrbirlbel 的解决方案,但遇到了一个问题:节点的坐标不再位于tikz它们认为的位置。使用答案中提供的前言和以下代码,似乎节点(例如m-1-1.north west)发生了偏移:

在此处输入图片描述

\newcommand{\FillMatrix}{%
\fill [rounded corners=3pt, fill opacity=.35, fill=green!80] 
           ([shift={(0pt, 2pt)}]m-1-1.north west) 
        -- ([shift={(0pt,-2pt)}]m-2-1.south west)
        -- ([shift={(2pt,-2pt)}]m-2-2.south east)
        -- ([shift={(2pt, 0pt)}]m-2-2.north east)
        -- cycle;
}%

\begin{document}
\hspace*{0.2em}
\begin{tikzpicture}[bmatrix]
    \matrix (m) {
       1 &  4 \\
       7 & 10 \\
      13 & 16 \\[-2pt]
    };
    \FillMatrix
\end{tikzpicture}
%% ----------------
\hspace*{0.3em}
\begin{tikzpicture}[bmatrix, matrix bottom crcr]
    \matrix (m) {
       1 &  4 \\
       7 & 10 \\
      13 & 16 \\[-2pt]
    };
    \FillMatrix
\end{tikzpicture}

\hspace*{0.6em} default \hspace*{0.6em} matrix bottom crcr

\end{document}

答案1

最后一条[<vspace>]没有效果,因为 PGF 根本不会执行与普通 相同的代码\\。它会对其进行评估并保存,以便我们可以在下面的补丁中使用它。完成后,它会检查后面是否有}(= \egroup),这表示矩阵内容的结束。

如果没有,它将根据行的大小\cr执行一些操作,以及是否使用了或,然后继续下一行。\vskipbetween originsbetween borders

如果是,则它只会结束行 ( \cr) 并结束矩阵 ( \pgf@end@matrix)。不过,我们可以修补相同的\vskip过程。

(PGF 矩阵是通过这个实现的,\halign这就是您在代码中看到\crs 和s 的原因。)\noalign


为了安全起见,我将为此添加一个切换开关matrix bottom crcr,以便您可以在其他矩阵无法正常工作时禁用它。

代码

\documentclass{article}
\usepackage{mathtools}% for amsmath {bmatrix}
\usepackage{tikz}
\usetikzlibrary{matrix}

%% https://tex.stackexchange.com/questions/26866/obstacles-to-simulating-an-amsmath-matrix-by-a-tikz-matrix-of-math-nodes
\newlength\mtxrowsep   \setlength\mtxrowsep{1.5ex}
\newlength\mtxcolsep   \setlength\mtxcolsep{2\arraycolsep}

\tikzset{
  ams/.style={
    baseline=-.7ex,
    every delimiter/.style={yshift=-1pt},
    every left delimiter/.style={xshift=2pt},
    every right delimiter/.style={xshift=-2pt},
    every node/.style={inner sep=0pt},
  },
  ams matrix/.style={
    inner sep=1pt,
    column sep=\mtxcolsep,
    row sep=\mtxrowsep,
    matrix of math nodes,
  },
  create ams matrix/.style n args={3}{#1matrix/.style={
    ams, every matrix/.style={
      ams matrix, left delimiter={#2}, right delimiter={#3}}}},
  create ams matrix/.list={b[], B\lbrace\rbrace, p(), v||, V\|\|},
}

\makeatletter
\let\pgf@matrix@finish@line@orig\pgf@matrix@finish@line
\tikzset{
  matrix bottom crcr/.is choice, matrix bottom crcr/.default=enabled,
  matrix bottom crcr/enabled/.code=
    \let\pgf@matrix@finish@line\qrr@pgf@matrix@finish@line
    \def\pgf@matrix@last@line@y{\ifpgf@matrix@noendrow@skip0pt\else\the\pgf@y\fi},
  matrix bottom crcr/disabled/.code=
    \let\pgf@matrix@finish@line\pgf@matrix@finish@line@orig
    \def\pgf@matrix@last@line@y{0pt}}
\def\qrr@pgf@matrix@finish@line{%
  \global\pgf@y=\pgf@y%
  \pgf@ya=-\pgf@y%
  \global\advance\pgf@ya by\csname pgf@matrix@miny\the\pgfmatrixcurrentrow\endcsname%
  \expandafter\xdef\csname pgf@matrix@miny\the\pgfmatrixcurrentrow\endcsname{\the\pgf@ya}%
  \pgfutil@ifnextchar\egroup{\pgf@matrix@eom@found}{\pgf@matrix@no@eom@found}% Qrr: change
}%
\def\pgf@matrix@eom@found{% Qrr: overwrite (unused before)
  \cr
  \unless\ifpgf@matrix@noendrow@skip
  \noalign{%
    \vskip\pgf@y
    \ifpgf@matrix@fixed % Qrr: “between borders”
      \vskip\csname pgf@matrix@miny\the\pgfmatrixcurrentrow\endcsname% unskip
      \vskip\pgf@y
      \pgf@y=-\pgf@y
      \expandafter\xdef\csname pgf@matrix@miny\the\pgfmatrixcurrentrow\endcsname{\the\pgf@y}%
    \fi
  }%
  \fi
  \pgf@end@matrix
}%

\usepackage{etoolbox}
\patchcmd\pgf@matrix@compute@origin{0pt}{\pgf@matrix@last@line@y}{}{\PatchFailed}
\def\pgf@matrix@last@line@y{0pt}
\newif\ifpgf@matrix@noendrow@skip
\patchcmd\pgfmatrixendrow
  {\pgfutil@ifnextchar[{\pgfmatrixendrow@skip}{\pgf@matrix@finish@line}}
  {\pgfutil@ifnextchar[{\global\pgf@matrix@noendrow@skipfalse\pgfmatrixendrow@skip}
                       {\global\pgf@matrix@noendrow@skiptrue\pgf@matrix@finish@line}}
  {}{\PatchFailed}
\makeatother

\newcommand{\FillMatrix}{%
\fill [rounded corners=3pt, fill opacity=.35, fill=green!80] 
           ([shift={(0pt, 2pt)}]m-1-1.north west) 
        -- ([shift={(0pt,-2pt)}]m-2-1.south west)
        -- ([shift={(2pt,-2pt)}]m-2-2.south east)
        -- ([shift={(2pt, 0pt)}]m-2-2.north east)
        -- cycle;
}%
\newcommand*\AMSMATH[1]{$\begin{bmatrix}\smash{1} & \smash{4} \\ 7 & 10 \\ 13 & 16 \\#1\end{bmatrix}$}
\begin{document}
no patch, \verb|\\|:\par
\begin{tikzpicture}[bmatrix]
\matrix (m) {
   1 &  4 \\
   7 & 10 \\
  13 & 16 \\
};
\FillMatrix
\end{tikzpicture}\AMSMATH{}\medskip

no patch, \verb|\\[1.5ex]|:\par
\begin{tikzpicture}[bmatrix]
\matrix (m) [row sep=0pt]{
   1 &  4 \\
   7 & 10 \\
  13 & 16 \\[1.5ex]
};
\FillMatrix
\end{tikzpicture}\AMSMATH{[1.5ex]}\bigskip

patch, \verb|\\|:\par
\begin{tikzpicture}[bmatrix, matrix bottom crcr]
\matrix (m) {
   1 &  4 \\
   7 & 10 \\
  13 & 16 \\
};
\FillMatrix
\end{tikzpicture}\AMSMATH{}\medskip

patch, \verb|\\[1.5ex]| (row sep + arg inserted) (\AmS: \verb|[3.0ex]|):\par
\begin{tikzpicture}[bmatrix, matrix bottom crcr]
\matrix (m) [row sep=0pt]{
   1 &  4 \\
   7 & 10 \\
  13 & 16 \\[1.5ex]
};
\FillMatrix
\end{tikzpicture}\AMSMATH{[3ex]}\medskip

patch, \verb|\\[-\pgfmatrixrowsep]| (row sep compensated):\par
\begin{tikzpicture}[bmatrix, matrix bottom crcr]
\matrix (m) [row sep=0pt]{
   1 &  4 \\
   7 & 10 \\
  13 & 16 \\[-\pgfmatrixrowsep]
};
\FillMatrix
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容