定义特定环境范围内的命令

定义特定环境范围内的命令

是否可以在单独的命名空间中定义命令,以便它们仅在特定环境中工作?

例如,我可以创建一个名为的包foo,定义一个foo环境,并且\newfoocommand其工作方式如下:

\documentclass{article}

\newcommand{\asdf}{do something}
\newcommand{\zzz}{zzz}

\usepackage{foo}
% doesn't conflict with the above \asdf command because it's in a different
% namespace.
% this actually does \newcommand{\@foo@cmd@asdf}{do something else}
\newfoocommand{\asdf}{do something else}
\newfoocommand{\jkl}{blahblah}

\begin{document}

% expands to "do something"
\asdf
% expands to "zzz"
\zzz
% causes: ERROR: Undefined control sequence.
\jkl

\begin{foo}
  % is interpreted as \@foo@cmd@asdf and expands to "do something else"
  \asdf
  % expands to "zzz" because \@foo@cmd@zzz isn't defined
  \zzz
  % is interpreted as \@foo@cmd@jkl and expands to "blahblah"
  \jkl
\end{foo}

% expands to "do something"
\asdf
% expands to "zzz"
\zzz
% causes: ERROR: Undefined control sequence.
\jkl

\end{document}

答案1

\checkenvcommands通过此实现,任何命令在其“开始”部分的环境中都可以具有不同的含义:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\newenvcommand}{ m m } % #1 = env name, #2 = command name
  {
   \cs_if_exist:cF { g_envc_#1_list_tl } { \tl_new:c { g_envc_#1_list_tl } }
   \tl_gput_right:cn { g_envc_#1_list_tl } { #2 }
   \exp_after:wN \newcommand \cs:w envc_#1_\cs_to_str:N #2 \cs_end:
  }
\NewDocumentCommand{\checkenvcommands}{ }
  {
   \cs_if_exist:cT { g_envc_\use:c {@currenvir} _list_tl }
     {
      \tl_map_inline:cn { g_envc_\use:c {@currenvir} _list_tl }
        { \cs_set_eq:Nc ##1 { envc_\use:c {@currenvir} _\cs_to_str:N ##1 } }
     }
  }
\ExplSyntaxOff

\newcommand{\asdf}[1]{OUTER DEF - arg is #1}
\newenvcommand{foo}{\asdf}[1]{FOO INNER DEF - arg is #1}
\newenvcommand{baz}{\asdf}[1]{BAZ INNER DEF - arg is #1}

\newenvironment{foo}{\checkenvcommands}{}
\newenvironment{baz}{\checkenvcommands}{}

\begin{document}
\show\asdf

\begin{foo}
\show\asdf
\end{foo}

\show\asdf

\begin{baz}
\show\asdf
\end{baz}

\end{document}

输出为

> \asdf=\long macro:
#1->OUTER DEF - arg is #1.
l.28 \show\asdf

? 
> \asdf=\long macro:
#1->FOO INNER DEF - arg is #1.
l.31 \show\asdf

? 
> \asdf=\long macro:
#1->OUTER DEF - arg is #1.
l.34 \show\asdf

? 
> \asdf=\long macro:
#1->BAZ INNER DEF - arg is #1.
l.37 \show\asdf

该实现与 David 的类似,但不需要为\<env>@let每个需要“本地”含义的环境定义一个宏。\checkenvcommands在这些环境中只需要一个宏。

“本地”命令存储在每个环境的不同标记列表变量中(在示例\g_envc_foo_list_tl和中\g_envc_baz_list_tl),并\checkenvcommands进行映射,以便命令的“私有”版本(例如)\envc_foo_asdf等同于\asdf。如果列表不存在,则不执行任何操作。

答案2

您可以将定义保存在内部宏中,然后在foo环境中本地公开它们。

\documentclass{article}

\newcommand{\asdf}{do something}
\newcommand{\zzz}{zzz}

%\usepackage{foo}
\makeatletter
\long\def\foo{%
\par FOO\par\hrule
\the\foo@defs}

\newtoks\foo@defs

\def\newfoocommand#1{%
\addto@hook\foo@defs{\foo@let#1}%
\expandafter\newcommand\csname foo\string#1\endcsname}

\def\foo@let#1{%
\expandafter\let\expandafter#1\csname foo\string#1\endcsname}

%\makeatother

% doesn't conflict with the above \asdf command because it's in a different
% namespace.
% this actually does \newcommand{\@foo@cmd@asdf}{do something else}
\newfoocommand{\asdf}{do something else}
\newfoocommand{\jkl}{blahblah}


\begin{document}

% expands to "do something"
\asdf
% expands to "zzz"
\zzz
% causes: ERROR: Undefined control sequence.
\jkl

\begin{foo}
  % is interpreted as \@foo@cmd@asdf and expands to "do something else"
\show\asdf
  \asdf
  % expands to "zzz" because \@foo@cmd@zzz isn't defined
  \zzz
  % is interpreted as \@foo@cmd@jkl and expands to "blahblah"
  \jkl
\end{foo}

\end{document}

答案3

这是 David Carlisle 解决方案的扩展。我已将其包含在期权包裹。

\documentclass[a4paper]{article}
\usepackage{catoptions}
\usepackage{xcolor}
\makeatletter
\robust@def*\AtBeginColony#1{\grightaddtocsn{atbegin@#1@colonyhook}}
\robust@def*\AtEndColony#1{\grightaddtocsn{atend@#1@colonyhook}}
\new@def*\colonyarg#1{\@nameuse{colony@arg@\romannumeral#1}}
\robust@def*\newcolony{\cpt@starorlong\new@colony}
\def\new@colony#1{\cpt@testopt{\newcolony@a#1}0}
\def\newcolony@a#1[#2]{%
  \cpt@ifbrack{\newcolony@b{#1}{#2}}{\newcolony@c{#1}{[#2]}}%
}
\def\newcolony@b#1#2[#3]{\newcolony@c{#1}{[#2][{#3}]}}
\robust@def*\renewcolony{\cpt@starorlong\renew@colony}
\def\renew@colony#1{%
  \ifcsndefTF{#1}{}{\cpt@err{Environment '#1' is undefined}\@ehc}%
  \expandafter\let\csname#1\endcsname\relax
  \expandafter\let\csname end#1\endcsname\relax
  \new@colony{#1}%
}
\long\def\newcolony@c#1#2#3#4{%
  \ifcsndefTF{#1}{}{\letcsntocsn{#1}{end#1}}%
  \def\reserved@a[##1]##2\@nil{%
    \chardef\@tempa\z@pt
    \loop\ifnum\@tempa<##1\relax
      \edef\@tempa{\the\numexpr\@tempa+1}%
      \cptexpandarg{\rightaddtocs\@tempb}{%
        \csn@edef{colony@arg@\romannumeral\@tempa}%
        {\noexpand\unexpanded{################\number\@tempa}}%
      }%
    \repeat
  }%
  \reserved@a#2\@nil
  \cptexpandbracenext{\aftercsname\new@command{#1}#2}{%
    \@tempb
    \@nameuse{atbegin@#1@colonyhook}%
    \@nameuse{#1@env@defs}#3%
  }%
  \l@ngrel@x\csn@def{end#1}{%
    #4%
    \@nameuse{atend@#1@colonyhook}%
  }%
}
% #1 = environment name; #2 = command
\robust@def*\newcolonycmd{\cpt@testst\new@colonycmd}
\robust@def\new@colonycmd#1#2{%
  \aftercsname\rightaddtocs{#1@env@defs}{\colonyletcs{#1}{#2}}%
  \cptexpanded{\noexpand\newcommand\ifcpt@st*\fi
    \noexpandcsn{#1@\cptgobblescape#2}}%
}
\def\colonyletcs#1#2{\letcstocsn{#2}{#1@\cptgobblescape#2}}

% Examples:
\newcommand{\asdf}{do something}
\newcommand{\zzz}{zzz}
\newcolony{foo}[2][\hsize]{%
  \endgraf\hrule width#1 depth0pt height.4pt
  \endgraf
  \textsc{This is \texttt{\textcolor{red}{foo}} environment}\\[.5ex]
  Arg 1: {\tt\detokenize{#1}}\\Arg 2: {\tt\detokenize{#2}}\\
}{%
  % Use argument 2 of this colony at exit. This is usually not possible
  % by LaTeX's \newenvironment:  
  \edef\y{\colonyarg{2}}%
}
\AtEndColony{foo}{\def\z#1{#1}\ignorespacesafterend}
\newcolonycmd*{foo}{\asdf}[1][bbb]{%
  Do something else. Arg 1 of {\tt\string\asdf}: {\tt\detokenize{#1}}%
}
\newcolonycmd{foo}{\jkl}{blah blah}

\makeatother
\begin{document}
\parindent0pt
% \asdf expands to "do something": we aren't in environment 'foo'
\asdf
\par
% \zzz expands to "zzz"
\zzz
% Undefined control sequence: we aren't in environment 'foo' and 
% \jkl isn't defined outside 'foo'.
%\jkl

\par\medskip
\def\showcmd#1{{\tt\string#1}: #1}

\begin{foo}[.5\hsize]{this value}
\showcmd\asdf
\endgraf
\showcmd\zzz
\endgraf
\showcmd\jkl
\end{foo}

\end{document}

相关内容