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}
\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?{}
. Why should this conditional code not branch in the right way?\mycmd
with the body, substituting the parameters with the unexpanded arguments. Therefore from\mycmd{x}[\alt<2>{2}{}]
you end up withArgument: "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!