我希望得到一些相当简单且可以描述但也许不可能得到的东西。
我想给宏提供多个定义(\U
在我的情况下),以便根据环境它可以具有不同的含义。
我会得到enva
,envb
并且在某处有envc
其他的定义\Ua
,\Ub
以便\Uc
\begin{enva}
Some text, \U some other texts
\end{enva}
展开相同Some text, \Ua some other texts
答案1
您可以提供默认定义,\U
并在环境启动时更新它。
\newcommand{\U}{default definition} % maybe issuing error
\newcommand{\Ua}{definition for enva}
\newcommand{\Ub}{definition for envb}
\newcommand{\Uc}{definition for envc}
\newenvironment{enva}
{\renewcommand{\U}{\Ua}...}
{...}
\newenvironment{envb}
{\renewcommand{\U}{\Ub}...}
{...}
\newenvironment{envc}
{\renewcommand{\U}{\Uc}...}
{...}
由于环境形成群体,因此一旦环境结束,重新定义也将结束其效果。
答案2
\documentclass{article}
\newcommand\Ua{AAA}
\newcommand\Ub{BBB}
\newcommand\Uc{CCC}
\newcommand\U{} % Check that \U is not in use
\newenvironment{enva}{\let\U\Ua}{}
\newenvironment{envb}{\let\U\Ub}{}
\newenvironment{envc}{\let\U\Uc}{}
\begin{document}
\begin{enva}
Some text, \U\ some other texts
\end{enva}
\begin{envb}
Some text, \U\ some other texts
\end{envb}
\begin{envc}
Some text, \U\ some other texts
\end{envc}
\end{document}
答案3
也许用在环境之间变化的变量来定义。
\documentclass{scrartcl}
\usepackage{etoolbox}
\newrobustcmd\definevariable[2][]
{\providecommand#2{\csuse{_\environmentvariable_\detokenize{#2}}}%
\expandafter\newcommand\csname\detokenize{_#1_#2}\endcsname}
\definevariable[a]{\U}{whatever}
\definevariable[b]{\U}{whatever else}
\definevariable[c]{\U}[1]{\uppercase{#1}}
\newenvironment{enva}{\def\environmentvariable{a}}{}
\newenvironment{envb}{\def\environmentvariable{b}}{}
\newenvironment{envc}{\def\environmentvariable{c}}{}
\begin{document}
\begin{enva}
Some text, \U\ some other texts
\end{enva}
\begin{envb}
Some text, \U\ some other texts
\end{envb}
\begin{envc}
Some text, \U{some} other texts
\end{envc}
\end{document}
答案4
比其他的复杂得多......
\documentclass{article}
\usepackage{ifthen}
\newboolean{insideA}
\setboolean{insideA}{false}
\newboolean{insideB}
\setboolean{insideB}{false}
\newboolean{insideC}
\setboolean{insideC}{false}
\newcommand{\U}{%
\ifthenelse{\boolean{insideA}}{%
in A%
}{%
\ifthenelse{\boolean{insideB}}{%
in B%
}{%
\ifthenelse{\boolean{insideC}}{%
in C%
}{%
default%
}}}}
\newenvironment{enva}{\setboolean{insideA}{true}}{\setboolean{insideA}{false}}
\newenvironment{envb}{\setboolean{insideB}{true}}{\setboolean{insideB}{false}}
\newenvironment{envc}{\setboolean{insideC}{true}}{\setboolean{insideC}{false}}
\begin{document}
Some text, \U\ some other texts
\begin{enva}
Some text, \U\ some other texts
\end{enva}
\begin{envb}
Some text, \U\ some other texts
\end{envb}
\begin{envc}
Some text, \U\ some other texts
\end{envc}
\end{document}