1

If I define a new command like

\newcommand{\command}{htb}

why can't I use this command to set for example the float options of an figure enviroment?

\begin{figure}[\command]
  \centering
  \[
    f(x) = x^2
  \]
  \caption{Some equation}
\end{figure}

this won't compile but this

\begin{figure}[htb]
  \centering
  \[
    f(x) = x^2
  \]
  \caption{Some equation}
\end{figure}

will.

Could someone explain this and maybe how one can set a common setting for all figures

  • 1
    If you want to change the positioning of every figure environment, you might redefine \fps@figure. The optional argument of float environments is not expanded to check which letters occur in it, you'd have to expand it beforehand, which would be a lot of \expandafters. You could use \newcommand*\myfigure[1]{\begin{figure}[{#1}]} and then use \expandafter\myfigure\expandafter{\command}. – Skillmon likes topanswers.xyz Oct 9 '19 at 16:34
2

This is primarily based on how LaTeX processes the optional argument to a float. In essence, it captures the optional argument in a different macro (\@fps, specifically), then attempts to expand that macro by looking at it's \meaning. Then it sequentially processes the characters of the meaning. However, the capturing process uses \def, similar to the following setup:

\usepackage[T1]{fontenc}
\def\abc{abc}
\def\ghi{\abc}
\meaning\ghi

which returns

enter image description here

In the above output, macro:-> is considered "the prefix", while the exact meaning is \abc. With this analogy, your \command will result in LaTeX processing \, c, o, m, m, a, n, d sequentially and not find any match with the existing set of approved float specifications.

Simple solutions would be to define the default using the kernel functions:

\makeatletter
\def\fps@figure{htb}
\makeatother

Or you could use the float package:

\usepackage{float}
\floatplacement{figure}{htb}

Or, if you're extremely dedicated, you could try a patch on \@xfloat:

\usepackage{etoolbox}

\makeatletter
\patchcmd{\@xfloat}% <cmd>
  {\def \@fps}% <search>
  {\edef\@fps}% <replace>
  {}{}% <success><failure>
\makeatother

The above replaces the traditional \def with an expanded version. It allows you to use your setup

\begin{figure}[\command]
  % <your figure>
\end{figure}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.