[Tex/LaTex] subfigure caption format differs for \captionof

captionssubcaption

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}

Best Answer

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