2

Am I missing something obvious, or is \captionof not supposed to work in a \makebox? I have tried \protect, \MakeRobustCommand, among others.

\documentclass{article}
\usepackage{capt-of}
\begin{document}
    \makebox{\captionof{figure}{X}}
\end{document}

I receive this error:

! Missing \endgroup inserted.
<inserted text> 
                \endgroup 
l.4     \makebox{\captionof{figure}{X}}
2

\makebox provides a horizontal box for content and therefore has difficulty coping with vertical content. In particular, \captionof eventually calls \@makecaption to set the actual caption. This starts and ends with vertical skips inserted via \vskip \abovecaptionskip and \vskip \belowcaptionskip. \vskips within a \makebox causes the problem:

\hrulefill

\mbox{\vspace{10pt}}% Acceptable...

\hrulefill

\mbox{\vskip 10pt}% Problematic...

\hrulefill

The above code snippet highlights the background legwork performed by \vspace in order to accommodate for non-vertical-mode skipping.

On the other hand, \parbox (and minipage, say) provide the option to incorporate horizontal and vertical content. So, if you want to place \captionof in a box, place it in a box that can manage its content, like a \parbox.

|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.