# Environment as local namespaces

I wish for something quite simple to describe but maybe impossible to have.

I would like to give multiple definitions to a macro (\U in my case) such that depending on the environment it can have different meaning.

I would have enva, envb and envc somewhere other definitions for \Ua, \Ub and \Uc so that

  \begin{enva}
Some text, \U some other texts
\end{enva}


expands the same as Some text, \Ua some other texts

• What should happen if \U is used e.g. in a tabular inside enva? Apr 9, 2017 at 13:15
• @UlrikeFischer, I am not sure i understand; in my intention enva would have no other effect than redefine locally \U. Apr 9, 2017 at 17:07
• See my answer to understand why I wanted to know this. Apr 9, 2017 at 17:10

You can provide a default definition for \U and renew it when the environment starts.

\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}...}
{...}


Since environments form groups, the redefinition will end its effect as soon as the environment ends.

\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}


Maybe define with a variable that changes between environments.

\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}

• @Skillmon Sorry, I cannot compile now, but I believe it's fixed. Apr 9, 2017 at 13:08
• Yes, this way it's compilable. Apr 9, 2017 at 13:09

The definition text can make use of the name of the current environment. But it is unclear if this is useful, as it would change the command in inner environments:

\documentclass{article}
\newcommand\Uenva{AAA}
\newcommand\Uenvb{BBB}
\newcommand\Uenvc{CCC}

\newcommand\U{\csname U\csname @currenvir\endcsname\endcsname} %
\newenvironment{enva}{}{}
\newenvironment{envb}{}{}
\newenvironment{envc}{}{}
\begin{document}
\begin{enva}
Some text, \U\ some other texts
\end{enva}

\begin{envb}
Some text, \U\ some other texts

\begin{tabular}{l}
in tabular empty: \U
\end{tabular}
\end{envb}

\begin{envc}
Some text, \U\ some other texts
\end{envc}
\end{document}


Much more complicate then the other ones...

\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}


• I like this best, it's what I would've done ;) Apr 9, 2017 at 12:45
• @Moriambar but it gets really tedious when you want to expand it to more environments… (image ten instead of three) Apr 9, 2017 at 12:49
• Of course… but I still like it best. I don't know why, it appeals me. Apr 9, 2017 at 12:50
• While it looks good, it's the worst answer so far. Just use envb inside of enva. While the macro should be in mode of envb it only uses the stuff of enva. This is not the desired behavior. Apr 9, 2017 at 13:03
• @Skillmon Sorry to hear that you don't like my answer. But the question did not mention the possibility of nested environments, and without a bit more background information, it is hard to guess what should happen in this case. If envb is inside enva, \U is still used inside enva - so without more specification regarding the macro output, I don't see where the current behaviour is wrong. Apr 9, 2017 at 14:19