![如何避免全局重新定义 & 符号?](https://linux22.com/image/445575/%E5%A6%82%E4%BD%95%E9%81%BF%E5%85%8D%E5%85%A8%E5%B1%80%E9%87%8D%E6%96%B0%E5%AE%9A%E4%B9%89%20%26%20%E7%AC%A6%E5%8F%B7%EF%BC%9F.png)
在我尝试建立我的图书馆时zx-微积分基于tikz
并tikzcd
兼容 tikz externalize(以节省编译时间),我基本上一直在使用这个解决方案https://tex.stackexchange.com/a/362104/116348除了我将自己的库包装在里面而不是 tikzcd 之外。
到目前为止它运行良好,但是这个解决方案全局定义:
\def\temp{&} \catcode`&=\active \let&=\temp
否则编译器就不能&
正确处理该符号。
我必须承认,我并不完全理解这些行是如何工作的(如果您能告诉我,我会很好奇),但我担心这些更改对整个文件是全局的。我记得使用 quantikz 时遇到过问题,它会全局重新定义这个相同的符号(可能是因为这个原因),这在使用其他包(如 tabularray)时会产生错误。
有没有办法在本地重新定义它?我尝试将其移到\mytikzcdcontext
函数内部,但没有成功。
平均能量损失:
\documentclass{article}
\usepackage{tikz, environ, etoolbox}
\usetikzlibrary{cd, external}
\tikzexternalize
% activate the following such that you can check the macro expansion in
% *-figure0.md5 manually
%\tikzset{external/up to date check=diff}
%%% How to avoid redefining this globally?
\def\temp{&} \catcode`&=\active \let&=\temp
\newcommand{\mytikzcdcontext}[2]{
\begin{tikzpicture}[baseline=(maintikzcdnode.base)]
\node (maintikzcdnode) [inner sep=0, outer sep=0] {\begin{tikzcd}[#2]
#1
\end{tikzcd}};
\end{tikzpicture}}
\NewEnviron{mytikzcd}[1][]{%
% In the following, we need \BODY to expanded before \mytikzcdcontext
% such that the md5 function gets the tikzcd content with \BODY expanded.
% Howerver, expand it only once, because the \tikz-macros aren't
% defined at this point yet. The same thing holds for the arguments to
% the tikzcd-environment.
\def\myargs{#1}%
\edef\mydiagram{\noexpand\mytikzcdcontext{\expandonce\BODY}{\expandonce\myargs}}%
\mydiagram%
}
\begin{document}
\begin{equation}
\begin{mytikzcd}[row sep=huge,baseline = (B.base)]
A \arrow[rd] \arrow[r, "\varphi"] & |[alias=B]| B \\ & C
\end{mytikzcd}
\hbox{sharing a baseline with } B
\end{equation}
\end{document}
作为参考,以下是我一直尝试用我的图书馆做的事情:
\documentclass[options]{article}
\usepackage{tikz}
\makeatletter
%%% Download this at https://github.com/leo-colisson/zx-calculus
\input{tikzlibraryzx-calculus.code.tex}
\makeatother
\usetikzlibrary{external}
\tikzexternalize % activate!
\NewExpandableDocumentCommand\zxE{O{}m}{
\begin{tikzpicture}[baseline=(tmpdirty.base)]
\node[inner sep=0pt,outer sep=0pt](tmpdirty){\begin{ZX}[#1]
#2
\end{ZX}};
\end{tikzpicture}
}
%% Use myZXE instead, this fails in math environment, no idea why.
\NewDocumentEnvironment{ZXE}{O{}+b}{%
\def\myargs{#1}%
\def\mybody{#2}%
\edef\mydiagram{\noexpand\zxE[\expandonce\myargs]{\expandonce\mybody}}%
\mydiagram%
}
\usepackage{environ}
\NewEnviron{myZXE}[1][]{%
% In the following, we need \BODY to expanded before \mytikzcdcontext
% such that the md5 function gets the tikzcd content with \BODY expanded.
% Howerver, expand it only once, because the \tikz-macros aren't
% defined at this point yet. The same thing holds for the arguments to
% the tikzcd-environment.
\def\myargs{#1}%
\edef\mydiagram{\noexpand\zxE[\expandonce\myargs]{\expandonce\BODY}}%
\mydiagram%
}
\NewDocumentEnvironment{myZXEw}{}{%
\bgroup%
\def\temp{&} \catcode`&=\active \let&=\temp%
\begin{myZXE}%
}{%
\end{myZXE}%
\egroup
}
\begin{document}
%% Redefine locally?
\def\temp{&} \catcode`&=\active \let&=\temp%
Works:
$x = \zxE[math baseline=kk]{
\zxX{\alpha} & \zxZ{\beta}\\
\zxZ[a=kk]{}
}$.
Works:
$x = $\begin{ZXE}[math baseline=kk]
\zxX[a=kk]{\epsilon} & \zxX{\epsilon}\\
\zxZ{abcd}
\end{ZXE}
% % Fails:
% $x = \begin{ZXE}[math baseline=kk]
% \zxX[a=kk]{\epsilon}\\
% \zxZ{}
% \end{ZXE}$
Works:
$x = \begin{myZXEw}[math baseline=kk]
\zxX[a=kk]{\delta} & \zxZ{\alpha}\\
\zxZ{}
\end{myZXE}$
\end{document}
答案1
你是指这样的吗?
&
体内的 首先被活性的 所取代&
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{cd, external}
\tikzexternalize
% activate the following such that you can check the macro expansion in
% *-figure0.md5 manually
%\tikzset{external/up to date check=diff}
% simpler to define a helper macro outside \ExplSyntaxOff
% because of spaces; we'll define an internal `expl3` version later
\newcommand{\mytikzcdcontext}[2]{
\begin{tikzpicture}[baseline=(maintikzcdnode.base)]
\node (maintikzcdnode) [inner sep=0, outer sep=0] {%
\begin{tikzcd}[#1]
#2
\end{tikzcd}%
};
\end{tikzpicture}%
}
\ExplSyntaxOn
\NewDocumentEnvironment{mytikzcd}{O{}+b}
{
\tl_set:Nn \l_tmpa_tl { #2 }
\regex_replace_all:nnN { \& } { \cA\& } \l_tmpa_tl
\tobias_bora_mytikzcd:nV { #1 } \l_tmpa_tl
}
{}
\cs_set_eq:NN \tobias_bora_mytikzcd:nn \mytikzcdcontext
\cs_generate_variant:Nn \tobias_bora_mytikzcd:nn { nV }
\ExplSyntaxOff
\begin{document}
\begin{equation}
\begin{mytikzcd}[row sep=huge,baseline = (B.base)]
A \arrow[rd] \arrow[r, "\varphi"] & |[alias=B]| B \\ & C
\end{mytikzcd}
\mbox{sharing a baseline with } B
\end{equation}
\end{document}
您确定要嵌套吗tikzpicture
?