[Tex/LaTex] Macro for figure position

floatsmacrospositioning

Why is the following minimal example not working?

I.e., what do I need to do, in order to define the float placement specifier of a figure within a macro? (I know that I should use \newcommand, but in the "full" example the error is part of an involved "superfigure" command. \newcommand is not working too btw.)

\documentclass{scrreprt}
\usepackage{blindtext}

\def\PosNotWorking{h}
\def\FramboxNotWorking{5cm}
\def\CaptionWorking{Caption is working.}

\begin{document}

\Blindtext[1]

\begin{figure}[\PosNotWorking]
  \centering
    \framebox[5cm]{My special figure. (Pos: \PosNotWorking)}        
    \caption{\CaptionWorking}
\end{figure}

\Blindtext[1]

\end{document}

If I compile this, the figure is at the top (default behaviour). If I enter \figure[h] instead of \figure[\PosNotWorking], the figure is of course between the two paragraphs.

Best Answer

The optional argument is not expanded, but used inside a loop which checks every containing letter. In your case it sees only the macro not the included letter. However, if you want to simply change the default positioning you can do this by redefining \fps@figure (for tables \fps@table):

\makeatletter
% Make all figure use 'h' position by default:
\def\fps@figure{h}
\makeatother

If you don't want to change it globally AND still want to use a \begin{figure} environment you could use \edef to expand it first:

\documentclass{scrreprt}
\usepackage[english]{babel}
\usepackage{blindtext}

\def\PosNowWorking{h}
\def\FramboxNotWorking{5cm}
\def\CaptionWorking{Caption is working.}

\begin{document}

\Blindtext[1]

\edef\efigure{\noexpand\begin{figure}[\PosNowWorking]}%
\efigure
  \centering
    \framebox[5cm]{My special figure. (Pos: \PosNowWorking)}        
    \caption{\CaptionWorking}
\end{figure}

\Blindtext[1]

\end{document}

or:

\def\efigure{\begin{figure}}%
\expandafter\efigure\expandafter[\PosNowWorking]

Or redefine the figure environment to expand its optional argument first:

\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][h]{%
   \expandafter\origfigure\expandafter[#1]%
}{%
   \endorigfigure
}

This way you can also easily set h as default value or put \PosNowWorking in there.

Related Question