# Conditional environment

In a huge document, I would like to use 'env1' or 'env2' environment depending on the value of a global variable 'flagenv'. How could I define an environment 'foo' so that I can write something like :

flagenv = 1    % 1 for env1 and 2 for env2
...
\begin{document}
\begin{foo}        % will be read as \begin{env1}
...
\end{foo}
\end{document}


Thanks.

• Welcome to TeX S.E. ! Please rewrite your MWE so that it will be fully compiliable with a simple copy-paste. In your case, please write your \newenvironment macro, it will be easier to get help (we will just have to correct your macro). I am not a macro wizzard, but I would also set a new counter (newcounter) flagenv. AFAIK, flagenv=1 is not the proper way to declare a variable in TeX. You could set your flagenv to 1 and use conditionnals depending on the fact that your counter is odd or even. – sztruks Nov 26 '18 at 7:30
• Thanks but using the conditionals is the help I am requesting :-) – Wissme Nov 26 '18 at 8:20
• Does it depend on the text of env1 ou env2 ? I suppose not ? Say that env1 and env2 are environments I defined, derived from Verbatim environment. – Wissme Nov 26 '18 at 8:22
• @Wissme The information that env1 and env2 are derived from Verbatim is crucial. Can you please give a short example of how you define them? – egreg Nov 26 '18 at 8:57
• If you are just asking about conditionals see Conditional typesetting / build. – Peter Grill Nov 26 '18 at 22:37

Something like:

\documentclass{...
\usepackage{...
...
%\newcommand\flagenv{0 }%
\newcommand\flagenv{1 }%
%\newcommand\flagenv{2 }%
%\newcommand\flagenv{3 }%
...
\ifcase\flagenv
⟨ definition of environment foo in case \flagenv=0 ⟩
\or
⟨ definition of environment foo in case \flagenv=1 ⟩
\or
⟨ definition of environment foo in case \flagenv=2 ⟩
\or
⟨ definition of environment foo in case \flagenv=3 ⟩
\else
⟨ definition of environment foo in case \flagenv<0 or \flagenv>3 ⟩
\fi
...
\begin{document}
...
\begin{foo}
...
\end{foo}
...
\end{document}


When you write \begin{env1}...\end{env1} roughly what happens is that LaTeX starts a "group" and then inserts commands \env1...\endenv1. So to do this it is enough to make the commands \foo and \endfoo execute \env1 and \endenv1 etc, which is easy to do using the \letcs command from the etoolbox package. In more detail,

\documentclass{article}
\usepackage{etoolbox}
\usepackage{verbatim}

\newenvironment{env1}{Environment 1\csuse{verbatim*}}{\csuse{endverbatim*}}
\newenvironment{env2}{Environment 2\verbatim}{\endverbatim}

\newcommand\SetFoo[1]{\letcs{\foo}{env#1}\letcs{\endfoo}{endenv#1}}

\begin{document}

\SetFoo{1}
\begin{foo}
My little environment
\end{foo}

\SetFoo{2}
\begin{foo}
My little environment
\end{foo}

\end{document}


produces the output

As in the MWE, you can use the command \SetFoo to change the meaning of the foo environment as many times as you like in the document, including just once in the preamble. In effect, the \SetFoo{?} macro defines the foo environment so it needs to be "called" before the first foo environment.

In the code above I have shown two different example uses of verbatim environments. This might fit your use-case but without knowing more about what you are trying to do this is not guaranteed - this is why it is always better to include as much detail as possible in a minimal working example as part of any question.

Side note As a general rule, LaTeX commands should not have numbers in them but, as shown, this works OK for environments although such usage may offend purists.