9

Following minimal example doesn't work and generates an error

\documentclass{article}
\usepackage{graphicx}
\usepackage{amssymb}
\begin{document}
\begin{figure}
  FIGURE
\caption{$\blacksquare$ result of; \rotatebox[origin=c]{45}{$\blacksquare$}}%
\end{figure}
\rotatebox[origin=c]{45}{$\blacksquare$}
\end{document}

The error message says:

rotatebox-in-caption.tex|7| Argument of \@caption has an extra }.
|| <inserted text> 
||                 \par 
|| l.7 ...; \rotatebox[origin=c]{45}{$\blacksquare$}}

Do I have to wrap the \rotatebox in a certain box to make it work?

Heiko Oberdiek
  • 271,626
Hotschke
  • 5,300
  • 5
  • 33
  • 63
  • You can read for the underlying cause http://tex.stackexchange.com/questions/12698/what-is-the-purpose-of-protect – percusse Jun 19 '13 at 15:20
  • 1
    It'll probably come down to using \caption[Stuff for the list of figures goes here]{$\blacksquare$ result of; \rotatebox[origin=c]{45}{$\blacksquare$}}. You might be able to \protect some of it if you really want it in the lof – cmhughes Jun 19 '13 at 15:21
  • 2
    Just add \protect in front of \rotatebox, that is, \protect\rotatebox[origin=c]{45}{...}, when it's in the argument of \caption. – egreg Jun 19 '13 at 15:26
  • using \protect solves the problem. @percusse I checked your link. What does \protect actually do to allow the fragile(?) command \rotatebox within a moving env. such as the \caption. What does it have to do with the lof what cmhughes was mentioning (there is no list of figures)? – Hotschke Jun 19 '13 at 15:39

1 Answers1

10

\rotatebox is fragile, see also: What is the difference between Fragile and Robust commands?

Inside "moving arguments" as in the argument of \caption fragile macros can be protected by prefixing them with \protect:

\documentclass{article}
\usepackage{graphicx}
\usepackage{amssymb}
\begin{document}
\listoffigures
\begin{figure}
  FIGURE
\caption{$\blacksquare$ result of;
 \protect\rotatebox[origin=c]{45}{$\blacksquare$}}%
\end{figure}
Text: \rotatebox[origin=c]{45}{$\blacksquare$}
\end{document}

Result

Making fragile macros robust

  • Package makerobust defines macro \MakeRobustCommand. It adds the LaTeX's protection layer for the macro in the same way as LaTeX's \DeclareRobustCommand:

      \usepackage{graphicx}
      \usepackage{makerobust}
      \MakeRobustCommand\rotatebox
      ...
      \caption{... \rotatebox ...}
    

    \rotatebox is no longer fragile and can be used without \protect.

  • Package etoolbox defines \robustify that redefines a macro with e-TeX's \protected:

      \usepackage{graphicx}
      \usepackage{etoolbox}
      \robustify\rotatebox
      ...
      \caption{... \rotatebox ...}
    

    This also makes \rotatebox robust without the need of \protect for this macro.

David Carlisle
  • 757,742
Heiko Oberdiek
  • 271,626