3

I'm trying to center subcaption subfigures as outlined this answer.

However, compilation fails with

! Argument of \subcaption@minipage has an extra }.
<inserted text> 
                \par 
l.6 \g@addto@macro\subcaption@minipage\centering

? 
Process interrupted by user

Any ideas?

\documentclass{article}

\usepackage{subcaption}

\makeatletter
\g@addto@macro\subcaption@minipage\centering
\makeatother

\begin{document}
\begin{figure}
\begin{minipage}[b]{.5\linewidth}
\large A
\subcaption{A subfigure}\label{fig:1a}
\end{minipage}%
\begin{minipage}[b]{.5\linewidth}
\large B
\subcaption{Another subfigure}\label{fig:1b}
\end{minipage}
\caption{A figure}\label{fig:1}
\end{figure}
\end{document}
thpani
  • 493
  • \g@addto@macro is for macro with no arguments. But, \subcaption@minipage is a macro with two arguments. Try (untested) \renewcommand*\subcaption@minipage[2]{\minipage#1{#2}\captionsetup{subtype}\centering} (check no spaces at all in definition after copy paste from here). Ok I tried (on an old TeX installation). It seems to work with usepackage{caption} added (maybe as old installtion) and the above within makeatletter...\makeatother –  Nov 13 '13 at 18:01
  • [again, this is on an old distribution, perhaps definition of \subcaption@minipage has changed since] –  Nov 13 '13 at 18:07

2 Answers2

5

The function \g@addto@macro can only be used with parameterless macros.

You'll be luckier with \apptocmd from etoolbox:

\usepackage{etoolbox}
\makeatletter
\apptocmd\subcaption@minipage{\centering}{}{}
\makeatother

but, of course, you have to use subfigure and not minipage.

Full example:

\documentclass{article}

\usepackage{subcaption}
\usepackage{etoolbox}
\makeatletter
\apptocmd\subcaption@minipage{\centering}{}{}
\makeatother

\begin{document}
\begin{figure}
\begin{subfigure}[b]{.5\linewidth}
\large A
\subcaption{A subfigure}\label{fig:1a}
\end{subfigure}%
\begin{subfigure}[b]{.5\linewidth}
\large B
\subcaption{Another subfigure}\label{fig:1b}
\end{subfigure}
\caption{A figure}\label{fig:1}
\end{figure}
\end{document}

enter image description here

egreg
  • 1,121,712
1

In year 2020, subcaption is using minipage instead of subcaption@minipage to create a new "sub-" environment. This means \apptocmd\subcaption@minipage .... would not alter the actual behavior of the package, since \subcaption@minipage is never used.

From the source of subcaption version 1.3 -

\caption@For{subtypelist}{%
  \newenvironment{sub#1}%
    { ....
       \subcaption@iminipage
      ....}%
    {\subcaption@endminipage
     \endminipage}}%
\def\subcaption@iminipage[#1]{%
  \caption@withoptargs{\subcaption@iiminipage{#1}}}
\newcommand*\subcaption@iiminipage[3]{%
    ....
    \minipage[#1]#2{#3}%
    ....}

Patching subcaption@iiminipage achieves the same thing as done seven years ago.

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{subcaption} \usepackage{etoolbox} \usepackage{lipsum}

\makeatletter \apptocmd\subcaption@iiminipage{\centering}{}{} \makeatother

\begin{document}

\section{Introduction} \lipsum[1][1-2]

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

\end{document}

enter image description here

Yvon
  • 171