重新定义环境内

重新定义环境内

我一直在关注解决方案这里让 tikzcd 和外部化库彼此之间能够很好地协作。

其中一个步骤是重新定义&。但是,目前,它对整个文档执行此操作,而我希望它只对环境执行此操作mytikzcd。我尝试将代码移到mytikzcd定义中,但根据我将其放置的具体位置,我似乎会收到各种不同的错误。您有什么建议吗?

MWE 几乎直接摘自上一个答案:

\documentclass{article}
%\url{https://tex.stackexchange.com/q/171931/86}
\usepackage{tikz}
\usepackage{environ}
%we don't actually need externalisation to demonstrate the problem, so turn it off for now
\usetikzlibrary{cd}%,external}
%\tikzexternalize
\usepackage{tabularray}


\makeatletter


\def\tikzcd@[#1]{%
  \begin{tikzpicture}[/tikz/commutative diagrams/.cd,every diagram,#1]%
  \ifx\arrow\tikzcd@arrow%
    \pgfutil@packageerror{tikz-cd}{Diagrams cannot be nested}{}
  \fi%
  \let\arrow\tikzcd@arrow%
  \let\ar\tikzcd@arrow%
  \def\rar{\tikzcd@xar{r}}%
  \def\lar{\tikzcd@xar{l}}%
  \def\dar{\tikzcd@xar{d}}%
  \def\uar{\tikzcd@xar{u}}%
  \def\urar{\tikzcd@xar{ur}}%
  \def\ular{\tikzcd@xar{ul}}%
  \def\drar{\tikzcd@xar{dr}}%
  \def\dlar{\tikzcd@xar{dl}}%
  \global\let\tikzcd@savedpaths\pgfutil@empty%
  \matrix[/tikz/matrix of \iftikzcd@mathmode math \fi nodes,
          /tikz/every cell/.append code={\tikzcdset{every cell}},
          /tikz/commutative diagrams/.cd,every matrix]%
  \bgroup}


\def\endtikzcd{%
  \pgfmatrixendrow\egroup%
  \pgfextra{\global\let\tikzcdmatrixname\tikzlastnode};%
  \tikzcdset{\the\pgfmatrixcurrentrow-row diagram/.try}%
  \begingroup%
\pgfkeys{% `quotes' library support
      /handlers/first char syntax/the character "/.initial=\tikzcd@forward@quotes,%
      /tikz/edge quotes mean={%
        edge node={node [execute at begin node=\iftikzcd@mathmode$\fi,%$
                         execute at end node=\iftikzcd@mathmode$\fi,%$
                         /tikz/commutative diagrams/.cd,every label,##2]{##1}}}}%
    \let\tikzcd@errmessage\errmessage% improve error messages
    \def\errmessage##1{\tikzcd@errmessage{##1^^J...^^Jl.\tikzcd@lineno\space%
        I think the culprit is a tikzcd arrow in cell \tikzcd@currentrow-\tikzcd@currentcolumn}}%
    \tikzcd@before@paths@hook%
    \tikzcd@savedpaths%
  \endgroup%
  \end{tikzpicture}%
  \ifnum0=`{}\fi}


\NewEnviron{mytikzcd}[1][]{%
  \def\@temp{\tikzcd@[#1]\BODY}%
  \expandafter\@temp\endtikzcd
}


\makeatother

\def\temp{&} \catcode`&=\active \let&=\temp
\begin{document}

\begin{mytikzcd}
  A \arrow{rd} \arrow{r}{\phi} & B \\ & C
\end{mytikzcd}

\begin{mytikzcd}
  A \arrow{rd} \arrow{r}{\phi} & B \\ & C
\end{mytikzcd}

%re-add the material below to see the problem that results from global redefinition of the &

% \begin{center}
% \begin{tblr}{
%   rows = {35pt}, columns = {35pt},
%   rowsep = 1pt, colsep = 1pt, stretch = 0,
%   cells = {c,font=\Huge},
%   cell{odd}{even} = {azure7},
%   cell{even}{odd} = {azure7},
%   hlines, vlines,
% }
% a & b & c & d & e & f & g & h \\
% a & b & c & d & e & f & g & h \\
%    &   &   &   &   &   &   &   \\
%    &   &   &   &   &   &   &   \\
%    &   &   &   &   &   &   &   \\
%    &   &   &   &   &   &   &   \\
% a & b & c & d & e & f & g & h \\
% \end{tblr}
% \end{center}

\end{document}

上下文:这需要在包内操作;我不想告诉用户他们必须在每次使用 mytikzcd 环境之前和之后编写额外的材料。它需要对他们不可见。

答案1

进入前&将catcode 改为,离开后再改回。\activemytikzcd4mytikzcd

\documentclass{article}
%\url{https://tex.stackexchange.com/q/171931/86}
\usepackage{tikz}
\usepackage{environ}
%we don't actually need externalisation to demonstrate the problem, so turn it off for now
\usetikzlibrary{cd}%,external}
%\tikzexternalize
\usepackage{tabularray}


\makeatletter


\def\tikzcd@[#1]{%
  \begin{tikzpicture}[/tikz/commutative diagrams/.cd,every diagram,#1]%
  \ifx\arrow\tikzcd@arrow%
    \pgfutil@packageerror{tikz-cd}{Diagrams cannot be nested}{}
  \fi%
  \let\arrow\tikzcd@arrow%
  \let\ar\tikzcd@arrow%
  \def\rar{\tikzcd@xar{r}}%
  \def\lar{\tikzcd@xar{l}}%
  \def\dar{\tikzcd@xar{d}}%
  \def\uar{\tikzcd@xar{u}}%
  \def\urar{\tikzcd@xar{ur}}%
  \def\ular{\tikzcd@xar{ul}}%
  \def\drar{\tikzcd@xar{dr}}%
  \def\dlar{\tikzcd@xar{dl}}%
  \global\let\tikzcd@savedpaths\pgfutil@empty%
  \matrix[/tikz/matrix of \iftikzcd@mathmode math \fi nodes,
          /tikz/every cell/.append code={\tikzcdset{every cell}},
          /tikz/commutative diagrams/.cd,every matrix]%
  \bgroup}


\def\endtikzcd{%
  \pgfmatrixendrow\egroup%
  \pgfextra{\global\let\tikzcdmatrixname\tikzlastnode};%
  \tikzcdset{\the\pgfmatrixcurrentrow-row diagram/.try}%
  \begingroup%
\pgfkeys{% `quotes' library support
      /handlers/first char syntax/the character "/.initial=\tikzcd@forward@quotes,%
      /tikz/edge quotes mean={%
        edge node={node [execute at begin node=\iftikzcd@mathmode$\fi,%$
                         execute at end node=\iftikzcd@mathmode$\fi,%$
                         /tikz/commutative diagrams/.cd,every label,##2]{##1}}}}%
    \let\tikzcd@errmessage\errmessage% improve error messages
    \def\errmessage##1{\tikzcd@errmessage{##1^^J...^^Jl.\tikzcd@lineno\space%
        I think the culprit is a tikzcd arrow in cell \tikzcd@currentrow-\tikzcd@currentcolumn}}%
    \tikzcd@before@paths@hook%
    \tikzcd@savedpaths%
  \endgroup%
  \end{tikzpicture}%
  \ifnum0=`{}\fi}


\NewEnviron{mytikzcd}[1][]{%
  \def\@temp{\tikzcd@[#1]\BODY}%
  \expandafter\@temp\endtikzcd
}


\makeatother

\def\temp{&} \catcode`&=\active \let&=\temp \catcode`&=4

\begin{document}

\catcode`&=\active
\begin{mytikzcd}
  A \arrow{rd} \arrow{r}{\phi} & B \\ & C
\end{mytikzcd}

\begin{mytikzcd}
  A \arrow{rd} \arrow{r}{\phi} & B \\ & C
\end{mytikzcd}
\catcode`&=4

%re-add the material below to see the problem that results from global redefinition of the &

 \begin{center}
 \begin{tblr}{
   rows = {35pt}, columns = {35pt},
   rowsep = 1pt, colsep = 1pt, stretch = 0,
   cells = {c,font=\Huge},
   cell{odd}{even} = {azure7},
   cell{even}{odd} = {azure7},
   hlines, vlines,
 }
 a & b & c & d & e & f & g & h \\
 a & b & c & d & e & f & g & h \\
    &   &   &   &   &   &   &   \\
    &   &   &   &   &   &   &   \\
    &   &   &   &   &   &   &   \\
    &   &   &   &   &   &   &   \\
 a & b & c & d & e & f & g & h \\
 \end{tblr}
 \end{center}

\end{document}

在此处输入图片描述

相关内容