# How to change the behavior of user-defined environments using overlays?

first I have to give a link to a related question+answer. I am trying to create a beamer presentation and defined an environment. The environment has an optional parameter and I want that beamer sets a frame using an overlay such that on frame 1 the default value is used and on frame 2 another value shall be used.

Clarification: The environment so far takes an optional argument and typesets a optimization problem (to be consistent). If the optional variable is set, it becomes a min-max problem while the argument itself will be the maximization variables. Thus depending on the existence of the optional parameter some static text plus the argument itself is typeset.

Now in beamer I want to show how to transform one problem into the other. In the first overlay there should be a min-max and in the following overlays a min problem typeset. My idea was to change the value of the optional argument to fit it the defualt one to indicate a pure min problem. As a result the overlay specifications etc are handled outside the environment. This has also the benefit, that I could change the value over time (a la \only<1>{a}\only<2>{b}\only<3>{c}) with no restrictions.

I could in fact give the explicit overlay specification for one overlay and hardcode the two different cases directly into the environment. But this seems infexible and not the perfect solution. I am wondering too, why it works perfectly for the mandatory arguments (see updated MWE).

I have tried to give a minimal (non)working example below.

I get compile errors about wrong definition of the alt macro.

Now I it might work for the actual case that I define the environment to be overlay aware (I am not sure, if everything works out well). In any case I can only give one overlay specification to the environment. If I want the parameter to take more than 2 values this is going to be a problem.

Can you give me an idea how to solve such a requirement?

\documentclass{beamer}
\usepackage{xifthen,xparse}
% \NewDocumentEnvironment{MyEnv}{m O{}}{Start (#1\ifthenelse{\equal{#2}{}}{}{, #2})\par}{End}
% \NewDocumentCommand{\mycmd}{m O{}}{Argument: "#1\ifthenelse{\equal{#2}{}}{}{, #2}\fi"}
\NewDocumentEnvironment{MyEnv}{m O{}}{Start (#1\ifx!#2!\else{, #2}\fi)\par\begingroup\bfseries}{\endgroup\par End\par}
\NewDocumentCommand{\mycmd}{m O{}}{Argument: "#1\ifx!#2!\else{, #2}\fi"}
\begin{document}
\begin{frame}{As commands}
\mycmd{x} (should be "")\par
\mycmd{x}[a] (should be "a")\par
\mycmd{\alt<2>{m2}{m1}} (should be "\alt<2>{m2}{m1}")\par
\mycmd{\alt<2>{m2}{m1}}[x] (should be "\alt<2>{m2}{m1}, x")\par

\vspace{1cm}
Error:\\
\mycmd{x}[\alt<2>{2}{}] (should be "x\alt<2>{, 2}{}")
\end{frame}

\begin{frame}{Using Environments}
\begin{MyEnv}{m}
Should be (m)
\end{MyEnv}
\begin{MyEnv}{m}[o]
Should be (m, o)
\end{MyEnv}
\begin{MyEnv}{\alt<2>{m1}{m2}}[o]
Should be (\alt<2>{m1}{m2}, o)
\end{MyEnv}

\vspace{1cm}
Error: \\
\begin{MyEnv}{m}[\alt<2->{b}{}]
Should be (m\alt<2>{, b}{})
\end{MyEnv}
\end{frame}
\end{document}

• The \alt error is caused by the way that \equals treats its argument, if you change the \ifthenelse part with \ifx!#2!\else{, #2}\fi it works as expected. Your requirements are very unclear however, can you provide an example of the desired output? Also, maybe you are interpreting the effect of \alt wrongly here: it gets expanded inside the body of your environment, not before getting passed as an argument, so the effect is not the same as two calls one with \begin{MyEnv}{m}[b] and one with \begin{MyEnv}{m}[]. Does this make sense? – Bordaigorl Jan 22 '14 at 10:39
• I changed my example so that the expected result is manually simulated. About the fact that the arguments get not evaluated before hand: I tought (please correct me), that using overlays for each frame the content is placed in one separate page in the PDF and that the overlay aware commands are evaluated according to the actual overlay number. By the way it doesn't make sense completely to me: I do compare inside the environment with {}. Why should this conditional code not branch in the right way? – Christian Wolf Jan 22 '14 at 20:16
• The intuition is: when constructing the frame, latex expands your definitions first, i.e. it expands the calls to \mycmd with the body, substituting the parameters with the unexpanded arguments. Therefore from \mycmd{x}[\alt<2>{2}{}] you end up with Argument: "x\ifx!\alt<2>{2}{}!\else{, \alt<2>{2}{}}\fi", then \ifx checks if the argument is empty but always finds an \alt which is not nothing! – Bordaigorl Jan 23 '14 at 19:35

I don't know if I understood what you wish. But here is a command with optional argument (the overlay specification). If some optional overlay specification is passed then the \alt<#1> is used.

\documentclass{beamer}
\usepackage{xifthen,xparse}

\newcommand{\myfoo}[2][]{%
\par%
Start \ifthenelse{\equal{#1}{}}{(#2)}%
{\alt<#1>{(#2,b)}{(#2)}}\par%
End}

\begin{document}
\begin{frame}
\myfoo{m}

\myfoo[2-]{m}

\end{frame}
\end{document}


• Sorry I may have written my question unclear. Just editing... – Christian Wolf Jan 21 '14 at 16:01