1

Having the following MWE:

\documentclass{scrartcl}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}
    \begin{figure}
%       \captionsetup[subfigure]{justification=centering}
        \centering
        \begin{subfigure}[t]{0.31\textwidth}
            \includegraphics[width=\textwidth]{example-image-a}
            \caption{\texttt{image 1}}%
            \label{fig:image1}
        \end{subfigure}
        ~%
        \begin{subfigure}[t]{0.31\textwidth}
            \includegraphics[width=\textwidth]{example-image-b}
            \caption[\texttt{image 2}]{\texttt{image 2}\\*
                Longer text containing description of the image contents.}
            \label{fig:image1}
        \end{subfigure}
        ~%
        \begin{subfigure}[t]{0.31\textwidth}
            \includegraphics[width=\textwidth]{example-image-c}
            \caption[\texttt{image 3}]{\texttt{image 3 having a name longer than the other two images}\\*
                Longer text containing description of the image contents.}
            \label{fig:image1}
        \end{subfigure}
    \end{figure}
\end{document}

I wonder if it is possible to change the formatting of subfloat captions so that the first line of (b) will be centered (as in (a)). In general, if there are multiple lines defined in a caption, each of them should be centered if it occupies less than one \linewidth and justified if it is longer – like a default behavior for a caption, but applied separately for different lines.

enter image description here

Optionally, second and further lines need not to be indented for the label, so they can take all the width of their subfloat.

1

Maybe the following gives you the desired result:

enter image description here

\documentclass{scrartcl}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}
    \begin{figure}
       \captionsetup[subfigure]{justification=raggedright,font=tt}
        \centering
        \begin{subfigure}[t]{0.31\textwidth}
            \includegraphics[width=\textwidth]{example-image-a}
            \caption{image 1}%
            \label{fig:image1}
        \end{subfigure}
        ~%
        \begin{subfigure}[t]{0.31\textwidth}
            \includegraphics[width=\textwidth]{example-image-b}
            \caption{image 2}
            Longer text containing description of the image contents.
            \label{fig:image2}
        \end{subfigure}
        ~%
        \begin{subfigure}[t]{0.31\textwidth}
            \includegraphics[width=\textwidth]{example-image-c}
            \caption{image 3 having a name longer than the other two images}
                Longer text containing description of the image contents.
            \label{fig:image3}
        \end{subfigure}
    \end{figure}
\end{document}
  • Part of the captons is not inside the caption macro, but as long as it is not a problem for hyperref links etc., this solves my question. Nevertheless, as I use class that changes font size of captions to small, to maintain consistency it may be better to use captionfont, i.e., a declaration like: \parbox{\linewidth}{\captionfont Longer text containing description of the image contents.}.Then, also, \captionsetup[subfigure]{justification=raggedright} is unnecessary and \centering may be used inside subfigure. – Peter Jul 25 at 18:59
  • Or, for even better handling of second line, i.e., to center it if too short: \usepackage{varwidth} and \begin{varwidth}[t]{\linewidth}\captionfont Longer text containing description of the image contents.\end{varwidth} % https://stackoverflow.com/a/12541369. – Peter Jul 25 at 19:17

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.