我有以下代码
\documentclass[10pt, a4paper]{article}
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\ExplSyntaxOn
\NewDocumentCommand \WhenNotMeasuring { } { \legacy_if:nF {measuring@} }
\makeatother
\ExplSyntaxOff
\let\adding\relax
\pgfkeys{
/adding/.code=\ifx\adding\relax
\edef\adding{#1}%
\else
\edef\adding{\adding, #1}%
\fi
}
\begin{document}
\begin{align*}
\begin{bNiceMatrix}[name=mymatrix]
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
\end{bNiceMatrix}
\WhenNotMeasuring{
\begin{tikzpicture}[remember picture,overlay]
\pgfkeys{/adding={2-3/3-3/2}}
\foreach \x/\y/\z in \adding{
\draw[->] (mymatrix-\x) ++(.75em,0) -- ++(.75em,0) node [anchor=west]{\tiny x\z} |- ($ (mymatrix-\y) + (0.75em,0) $);
}
%\foreach \x/\y in {\pgfkeysvalueof{/mult}}
%\draw (\x) ++(.75em,0) node [anchor=west]{\tiny \y};
%\foreach \x/\y in {\pgfkeysvalueof{\swap}}
%\draw (\x) -- ++(3ex,0) |- (\y);
\end{tikzpicture}
}
\end{align*}
\end{document}
但是现在如果我将其移动\pgfkeys{/adding={2-3/3-3/2}}
到nicematrix
环境中例如
\begin{bNiceMatrix}[name=mymatrix]
1 & 2 & 3 \\
4 & 5 & 6 \\ \pgfkeys{/adding={1-3/2-3/2}}
7 & 8 & 9 \\
\end{bNiceMatrix}
箭头不再被绘制。
有什么想法知道为什么以及如何解决它?
编辑:
\documentclass[10pt, a4paper]{article}
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\ExplSyntaxOn
\NewDocumentCommand \WhenNotMeasuring { } { \legacy_if:nF {measuring@} }
\makeatother
\ExplSyntaxOff
\makeatletter
\pgfkeys{
/adding/.code=\ifx\tikz@atticus@adding\relax
\xdef\tikz@atticus@adding{#1}%
\else
\xdef\tikz@atticus@adding{\tikz@atticus@adding, #1}%
\fi,/tikz/.cd,
reset Gauss/.code=\global\let\tikz@atticus@adding\relax,
reset Gauss,
get list/.code=\edef#1{\tikz@atticus@adding},
get list/.default=\adding,
}
\makeatother
\newenvironment{gaussMatrix}%
{
\begin{pNiceMatrix}[name=mymatrix]
}%
{
\end{pNiceMatrix}
\WhenNotMeasuring{
\begin{tikzpicture}[remember picture,overlay,get list=\adding]
\foreach \x/\y/\z in \adding
\draw[->] (mymatrix-\x) ++(.75em,0) -- ++(.75em,0) node [anchor=west]{\tiny x\z} |- ($ (mymatrix-\y) + (0.75em,0) $);
\tikzset{reset Gauss}% <-important
\end{tikzpicture}
}
}
\begin{document}
\begin{align*}
\begin{gaussMatrix}
1 & 1 & 1 & 1 & 1 \\
2 & 2 & 2 & 2 & 2 \\
3 & 3 & 3 & 3 & 3 \\
\end{gaussMatrix}
\end{align*}
\end{document}
答案1
您可以通过将宏设为全局宏来解决问题。通常这是应该避免的事情。1如果真的想这样做,通常会引入包含某些特殊字符的宏,传统上是@
带有 的\makeatletter
宏。然后必须小心地重置列表,而这对于局部定义来说是不必要的。为此,我添加了一个reset Gauss
键。所以这是一个原型示例。
\documentclass[10pt, a4paper]{article}
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\ExplSyntaxOn
\NewDocumentCommand \WhenNotMeasuring { } { \legacy_if:nF {measuring@} }
\makeatother
\ExplSyntaxOff
\makeatletter
\pgfkeys{
/adding/.code=\ifx\tikz@atticus@adding\relax
\xdef\tikz@atticus@adding{#1}%
\else
\xdef\tikz@atticus@adding{\tikz@atticus@adding, #1}%
\fi,/tikz/.cd,
reset Gauss/.code=\global\let\tikz@atticus@adding\relax,
reset Gauss,
get list/.code=\edef#1{\tikz@atticus@adding},
get list/.default=\adding,
}
\makeatother
\begin{document}
\begin{align*}
\begin{bNiceMatrix}[name=mymatrix]
1 & 2 & 3 \\
4 & 5 & 6 \\ \pgfkeys{/adding={2-3/3-3/2}}
7 & 8 & 9 \\
\end{bNiceMatrix}
\WhenNotMeasuring{
\begin{tikzpicture}[remember picture,overlay,get list=\adding]
\foreach \x/\y/\z in \adding{
\draw[->] (mymatrix-\x) ++(.75em,0) -- ++(.75em,0) node [anchor=west]{\tiny x\z} |- ($ (mymatrix-\y) + (0.75em,0) $);
}
\tikzset{reset Gauss}% <-important
\end{tikzpicture}
}
\end{align*}
\end{document}
这确实有效。但是,正如作者 F. Patigny 给出的几个出色答案一样nicematrix
,我认为更简洁的方法可能是在包中内置一些官方钩子nicematrix
(如果它们尚不存在)以获得更简洁、更安全的代码。我写这篇文章只是因为你在评论中询问。
1 pgf的未来版本将能够一定程度上解决这个问题:https://tex.stackexchange.com/a/491246。