2

I have a document that I will print in two different page sizes (A5, A4). Some figures are always printed on a separate page, regardless of the page size. This is fine when printing in A5, but when printing in A4 there is too much whitespace around them. If I force the top position with \begin{figure}[t], it works in A4, but the figure gets pushed at the end of the chapter in A5.

I want to specify the float position as a conditional, something like this

\documentclass{scrartcl}
\RequirePackage{etoolbox}
\newtoggle{a4}
\togglefalse{a4}
\begin{document}
\begin{figure}[\iftoggle{a4}{t}{p}]
Figure
\end{figure}
\end{document}

However, this raises an error

! LaTeX Error: Unknown float option `\'.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ->\errmessage  LaTeX Error: Unknown float option `\reserved@a '.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help\@err@                                                                   

l.7 \begin{figure}[\iftoggle{a4}{t}{p}]

Option `\' ignored and `p' used.

I know I can wrap the entire figure twice in a \iftoggle{a4}{\begin{figure}[t]...}{\begin{figure}[p]...} command, specifying different positions, but I want to avoid duplicating code.

Is there any way to achieve this?

  • 1
    Also, you don't need to wrap the entire figure in the conditional, just the \begin{figure}[...]is enough: \iftoggle{a4}{\begin{figure}[t]}{\begin{figure}[p]}. – Phelype Oleinik Feb 6 at 10:48
  • This somehow feels wrong, but it works quite well. I did not think it was possible to "split" environments like that. You should add your comment as an answer. – Claudio Feb 6 at 11:05
  • I added that and an explanation to my answer. – Phelype Oleinik Feb 6 at 11:21
3

In latex.ltx the float position specifier is no expanded, thus it doesn't work. However, it can be easily patched to \edef the argument, so the \iftoggle (which is expandable) gives the correct result:

\documentclass{scrartcl}
\RequirePackage{etoolbox}

\makeatletter
\patchcmd\@xfloat
  {\def\@fps{#2}}
  {\edef\@fps{#2}}
  {}{\ERROR! Failed to patch.}
\makeatother

\newtoggle{a4}
\togglefalse{a4}
\begin{document}
\begin{figure}[\iftoggle{a4}{t}{p}]
Figure
\end{figure}
\end{document}

Beware: There are many packages which access this part of the LaTeX kernel, so this solution might break something.


With a little code repetition you can do:

\documentclass{scrartcl}
\RequirePackage{etoolbox}
\newtoggle{a4}
\togglefalse{a4}
\begin{document}
\iftoggle{a4}
 {\begin{figure}[t]}%
 {\begin{figure}[p]}% < Don't forget this % here
    Figure
  \end{figure}
\end{document}

This one works because \begin{environment} essentially does \begingroup\environment, which is perfectly fine to be put in a conditional like that.

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.