9

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

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

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.

8

enter image description here

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

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. – Manuel Apr 9 '17 at 13:08
  • Yes, this way it's compilable. – Skillmon Apr 9 '17 at 13:09
6

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

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}

enter image description here

  • I like this best, it's what I would've done ;) – Moriambar Apr 9 '17 at 12:45
  • 1
    @Moriambar but it gets really tedious when you want to expand it to more environments… (image ten instead of three) – clemens Apr 9 '17 at 12:49
  • Of course… but I still like it best. I don't know why, it appeals me. – Moriambar Apr 9 '17 at 12:50
  • 2
    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. – Skillmon Apr 9 '17 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. – user36296 Apr 9 '17 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.