我希望有一个 LaTeX 2e 环境,当它出现在另一个环境中时,它会“知道”周围环境的名称。这可能吗?
我知道这\@currenvir
提供了当前环境的名称。
我寻求某种\@surroundenvir
能够提供周围环境名称的东西。
例如
\documentclass{article}
\makeatletter
\newenvironment{FOO}{\message{\@currenvir-begin: Surrounding environment's name is \@surroundenvir}}%
{\message{\@currenvir-end: Surrounding environment's name is \@surroundenvir}}
\newenvironment{BAR}{\message{\@currenvir-begin: Surrounding environment's name is \@surroundenvir}}%
{\message{\@currenvir-end: Surrounding environment's name is \@surroundenvir}}
\newenvironment{FOOBAR}{\message{\@currenvir-begin: Surrounding environment's name is \@surroundenvir}}%
{\message{\@currenvir-end: Surrounding environment's name is \@surroundenvir}}
\makeatother
\begin{document}
\begin{FOO}
\end{FOO}
\begin{BAR}
\end{BAR}
\begin{FOOBAR}
\end{FOOBAR}
\begin{FOO}
\begin{BAR}
\begin{FOOBAR}
\end{FOOBAR}
\end{BAR}
\end{FOO}
\end{document}
应提供控制台输出:
FOO-begin: Surrounding environment's name is document
FOO-end: Surrounding environment's name is document
BAR-begin: Surrounding environment's name is document
BAR-end: Surrounding environment's name is document
FOOBAR-begin: Surrounding environment's name is document
FOOBAR-end: Surrounding environment's name is document
FOO-begin: Surrounding environment's name is document
BAR-begin: Surrounding environment's name is FOO
FOOBAR-begin: Surrounding environment's name is BAR
FOOBAR-end: Surrounding environment's name is BAR
BAR-end: Surrounding environment's name is FOO
FOO-end: Surrounding environment's name is document
答案1
环境通常使用\begin
-\end
子句来调用。你可以\begin
使用etoolbox
\@currenvir
)在更新之前存储其值:
\documentclass{article}
\usepackage{etoolbox}
\makeatletter
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\begin}{\begingroup}{\begingroup\let\@surroundenvir\@currenvir}{}{}
\newenvironment{FOO}{\message{\@currenvir-begin: Surrounding environment's name is \@surroundenvir}}%
{\message{\@currenvir-end: Surrounding environment's name is \@surroundenvir}}
\newenvironment{BAR}{\message{\@currenvir-begin: Surrounding environment's name is \@surroundenvir}}%
{\message{\@currenvir-end: Surrounding environment's name is \@surroundenvir}}
\newenvironment{FOOBAR}{\message{\@currenvir-begin: Surrounding environment's name is \@surroundenvir}}%
{\message{\@currenvir-end: Surrounding environment's name is \@surroundenvir}}
\makeatother
\begin{document}
\begin{FOO}
\end{FOO}
\begin{BAR}
\end{BAR}
\begin{FOOBAR}
\end{FOOBAR}
\begin{FOO}
\begin{BAR}
\begin{FOOBAR}
\end{FOOBAR}
\end{BAR}
\end{FOO}
\end{document}
得出的结果是.log
:
FOO-begin:周围环境的名称是文档 FOO-end: 周围环境的名称是文档 BAR-begin:周围环境的名称是文档 BAR-end: 周围环境的名称是文档 FOOBAR-begin:周围环境的名称是文档 FOOBAR-end: 周围环境的名称是 document FOO-begin:周围环境的名称是文档 BAR-begin:周围环境的名称是FOO FOOBAR-begin:周围环境的名称为BAR FOOBAR-end: 周围环境名称为BAR BAR-end: 周围环境的名称是 FOO FOO-end: 周围环境的名称是文档
请注意,\@currenvir
默认值为document
,在LaTeX2e 内核。
答案2
例如“test.tex”:
\documentclass{article}
\usepackage{etoolbox}
\makeatletter
\newcommand*\@surroundenvir{}
\newcommand*\@surroundenvirtemp{}
\BeforeBeginEnvironment{FOO}{\let\@surroundenvirtemp=\@currenvir}
\AtBeginEnvironment{FOO}{\let\@surroundenvir=\@surroundenvirtemp}
\BeforeBeginEnvironment{BAR}{\let\@surroundenvirtemp=\@currenvir}
\AtBeginEnvironment{BAR}{\let\@surroundenvir=\@surroundenvirtemp}
\BeforeBeginEnvironment{FOOBAR}{\let\@surroundenvirtemp=\@currenvir}
\AtBeginEnvironment{FOOBAR}{\let\@surroundenvir=\@surroundenvirtemp}
\newenvironment{FOO}{\message{Begin \@currenvir: Surrounding env is: \@surroundenvir^^J}}%
{\message{End \@currenvir: Surrounding env is: \@surroundenvir^^J}}%%
\newenvironment{BAR}{\message{Begin \@currenvir: Surrounding env is: \@surroundenvir^^J}}%
{\message{End \@currenvir: Surrounding env is: \@surroundenvir^^J}}%%
\newenvironment{FOOBAR}{\message{Begin \@currenvir: Surrounding env is: \@surroundenvir^^J}}%
{\message{End \@currenvir: Surrounding env is: \@surroundenvir^^J}}%%
\makeatother
\begin{document}
\begin{FOO}
\end{FOO}
\begin{BAR}
\end{BAR}
\begin{FOOBAR}
\end{FOOBAR}
\begin{FOO}
\begin{BAR}
\begin{FOOBAR}
\end{FOOBAR}
\end{BAR}
\end{FOO}
\end{document}