Tikz 數學節點矩陣中 \\ 的可選參數,以在最後一行後留下額外的空間

Tikz 數學節點矩陣中 \\ 的可選參數,以在最後一行後留下額外的空間

我正在使用解決方案形式透過數學節點的 TiKZ 矩陣模擬 amsmath 矩陣的障礙tikz matrix以 a 的風格創建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),這表示矩陣內容的結尾。

如果沒有,在 a 之後,\cr它將\vskip根據行的大小以及是否使用了between originsor來執行一些 s between 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}

輸出

在此輸入影像描述

相關內容