# \captionof in a box

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}


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

• Maybe helpful tex.stackexchange.com/a/222516/124842. – Bobyandbob Apr 28 '17 at 12:09
• You can place it in a \parbox. – Steven B. Segletes Apr 28 '17 at 12:27
• Yes you can. Though I wonder what added benefit wrapping the \parbox in a \makebox provides that the bare \parbox does not? – Steven B. Segletes Apr 28 '17 at 12:59
• You might find \abovecaptionskip=0pt useful. – John Kormylo Apr 28 '17 at 13:53
• @StevenB.Segletes I use a hacky combination of three boxes, see tex.stackexchange.com/a/367225/30810. If that could be simplified, I would like that, but I haven't found shorter a solution yet. – bers Apr 28 '17 at 14:08

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