2

It seems that the \captionof command not only changes the current caption but also all following captions. That means one cannot easily swap captions around in a figure but has to remember what type the float is.

For example,

\documentclass{article}
\usepackage{capt-of}

\begin{document}

\begin{figure}
\captionof{table}{This is a table}
\caption{This is a figure}
\end{figure}

\end{document}

gives two Table captions while I would have expected the default caption command to just produce the default caption, i.e. Figure ...

Compare:

\documentclass{article}
\usepackage{capt-of}

\begin{document}

\begin{figure}
\caption{This is a figure}
\captionof{table}{This is a table}
\end{figure}

\end{document}

Which gives an expected result.

Edit:

Here is my sideways float use case:

\documentclass{article}

\usepackage{capt-of}
\usepackage{rotfloat}

\begin{document}
\begin{sidewaysfigure}
\captionof{table}{This is a table}
\caption{This is a figure}
\end{sidewaysfigure}

\end{document}
  • In what situation would you want two different captions? – oliversm Feb 20 at 11:04
  • The situation is a (whole page) sideways float with both a table and a figure. – Daniel Feb 20 at 11:17
6

The package information string says

standard captions outside of floats

The problem is that the definition of \captionof is rather simplistic:

\newcommand\captionof[1]{\def\@captype{#1}\caption}

Thus if you use \captionof{table}{...} inside a figure environment, the float type is set to table affecting also later \caption commands.

Use grouping (or a nested environment such as minipage).

\documentclass{article}
\usepackage{capt-of}

\begin{document}

\begin{figure}
\begingroup
\captionof{table}{This is a table}
\endgroup
\caption{This is a figure}
\end{figure}

\end{document}

enter image description here

  • Thanks, works great. I saw that the original purpose was to have captions outside of floats but it was also recommended elsewhere here that this is a way to have both figures and tables inside of one (whole page) float. I am wondering whether the \captionof command should be extended by your suggestion. But maybe there are unintended side-effects to this in other situations.... – Daniel Feb 20 at 11:21
  • @Daniel in that case you would normally have each part inside their own minipage and thus the \@captype change in one will never be seen in the other. – daleif Feb 20 at 13:30
  • @daleif Why would one have each part inside a minipage? What is the benefit of this (apart from captionof working as expected)? – Daniel Feb 21 at 16:34
  • Often: alignment – daleif Feb 21 at 16:36

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.