[Tex/LaTex] Pass a command as optional argument of environment

environmentsmacrosoptional arguments

Sorry for the beginner question, but I'd like to know if there is a robust way to do what the title says. The following MWE

\documentclass{book}

\usepackage[demo]{graphicx}
\newcommand{\myoptions}{h!tb}

\begin{document}

\begin{figure}[\myoptions]
  \centering
  \includegraphics[width=0.5\textwidth]{whatever}
  \caption[Short caption citing something]{Long caption.}
  \label{fig:whatever}
\end{figure}

\end{document}

gives the error ! LaTeX Error: Unknown float option '\'..

Actually I found a workaround (in my actual document the \figure environment is used by a new command that takes many parameters needed to define a figure and puts them inside \figure), but it gives problems with \cite command inside short caption, that's why I'm looking for a robust solution.

SOLUTION

Here is the solution to this question, that was given as a comment to the accepted answer:

\documentclass{book}

\usepackage[demo]{graphicx}
\newcommand{\myoptions}{h!tb}

\def\tmp#1{\begin{figure}[#1]}% use tmp, in the form below, as many times as needed

\begin{document}

\expandafter\tmp\expandafter{\myoptions}
  \centering
  \includegraphics[width=0.5\textwidth]{whatever}
  \caption[Short caption citing something]{Long caption.}
  \label{fig:whatever}
\end{figure}

\end{document}

Best Answer

The fact that you get an error in the texlive 2015 release but not earlier ones is due to it including fixes from the old fixltx2e package.

In earlier releases

\newcommand{\myoptions}{h!tb}
\begin{figure}[\myoptions]

Then \myoptions is not expanded and is just taken as the sequence of options \,m,y,o,p,t,i,o,n,s unknown options were silently ignored so this is equivalent to [pt] not, as you might expect from the definition, to [h!tb].

Had you done

\newcommand{\foo}{h!tb}
\begin{figure}[\foo]

then since no legal option is included (just\, f and two o), the figure is not allowed anywhere and so will always go to the end of the document or \clearpage.

The change in the 2015/01/01 release is that an error is raised for undefined float placement options so here you get an error on \.

Related Question