4

When I place a subfigure caption using \captionof, the format differs from the one appearing when placed via \caption inside a subfigure environment.

I suppose this is a bug in the \captionof command? Trying to force it via \captionsetup doesn't work either.

\documentclass{article}
\usepackage{subcaption}


\begin{document}

\begin{figure}
    \begin{subfigure}{\textwidth}
        \caption{A}
    \end{subfigure}
\end{figure}

\captionsetup[sub]{labelformat=parens}
\captionof{subfigure}{B}


\end{document}
Niclas
  • 453
  • 4
  • 14
  • 3
    I would place \captionsetup[subfigure]{labelformat=parens,labelsep=space} (note the use of subfigure, not just sub) in the preamble and then use \begingroup \captionof{subfigure}{B} \endgroup. Note the use of the group. – Werner May 18 '16 at 20:12
  • ...perhaps even add size=small to the \captionsetup. – Werner May 18 '16 at 20:19
  • @Werner - I take it the group is to prevent a subsequent \caption misplaced outside a figure from running (no error message)? – John Kormylo May 18 '16 at 20:50
  • @JohnKormylo: \caption uses \@captype to know what caption to set. \captionof redefines \@captype and then sets a usual \caption. Without a limited scope, you could use \caption outside a float. It's unlikely to happen, but you'll see a warning to that effect in the .log when compiling without grouping \captionof. – Werner May 18 '16 at 20:52
  • Thanks a lot @Werner, the subfigure instead of sub part already did the trick! Should have read the subcaption manual more carefully. Also, maybe I should have been more clear: I do not plan on placing the subcaption somewhere outside a float, it was just the most simple way to reproduce the error. I actually use it in an xlabel of a PGFplot, so no need for a grouping there. Adding \captionsetup[subfigure]{labelformat=parens,labelsep=space} to the preamble solved it globally, although I still consider it odd behavior that two formats exist otherwise. You could put it as an answer – Niclas May 19 '16 at 01:18

1 Answers1

6

\captionof{subfigure} was never meant to work correctly. (IMHO it should give an error message instead.) \captionof is only valid for main environments which offer captions, like figure.

If one need sub-captions for non-floating environments, I recommend using \captionsetup{type=...} instead:

\documentclass{article}
\usepackage{subcaption}

\begin{document}

\begin{figure}
    \begin{subfigure}{\textwidth}
        \caption{A}
    \end{subfigure}
\end{figure}

\captionsetup[sub]{labelformat=parens}
\begingroup
    \captionsetup{type=figure}
    \begin{subfigure}{\textwidth}
        \caption{B}
    \end{subfigure}
\endgroup

\end{document}

BTW: I recommand using \captionsetup{type=...} plus \caption instead of \captionof anyway, even when used for main captions.

  • https://sourceforge.net/p/latex-caption/tickets/44/ –  Jun 09 '16 at 07:02
  • Since caption.sty v3.4g (2020-01-02) the usage of \captionof with a sub-type produces an error instead of weird behavior. –  Jan 02 '20 at 19:25