[Tex/LaTex] Centering a subcaption

captionssubcaptionsubfloats

I have three figures that I want to place next to each other. That is fine. In addition, however, each subfigure has a caption on its own and the crucial thing with these is that I have to have two lines for each of them generated by a linebreak. I managed that using \newline, but now the subcaption always starts on the left edge of the subfigures and is not centered any more (which does not look nice).

What can I do to resolve my problem?

\documentclass{scrartcl}
\usepackage{graphix,subcaption}

\begin{document}
  \begin{figure}[t]
    \centering
      \begin{subfigure}{0.31\textwidth}
        \includegraphics[width=\textwidth]{image1}
          \caption{\centering Nice image1.\newline Another line.}
          \label{fig:NiceImage1}
      \end{subfigure}
      \begin{subfigure}{0.31\textwidth}
        \includegraphics[width=\textwidth]{image1}
          \caption{\centering Nice image 2.\newline Another line.}
          \label{fig:NiceImage2}
      \end{subfigure}
      \begin{subfigure}{0.31\textwidth}
        \includegraphics[width=\textwidth]{image1}
          \caption{\centering Nice image 2.\newline Another line.}
          \label{fig:NiceImage3}
      \end{subfigure}

Best Answer

You can use justification=centering for \captionsetup:

\documentclass{scrartcl}
\usepackage[demo]{graphicx}
\usepackage{subcaption}

\begin{document}
  \begin{figure}
\captionsetup[subfigure]{justification=centering}
    \centering
      \begin{subfigure}{0.31\textwidth}
        \includegraphics[width=\textwidth]{image1}
          \caption{Nice image1. \\ Another line.}
          \label{fig:NiceImage1}
      \end{subfigure}
      \begin{subfigure}{0.31\textwidth}
        \includegraphics[width=\textwidth]{image1}
          \caption{Nice image 2. \\ Another line.}
          \label{fig:NiceImage2}
      \end{subfigure}
      \begin{subfigure}{0.31\textwidth}
        \includegraphics[width=\textwidth]{image1}
          \caption{Nice image 3. \\ Another line.}
          \label{fig:NiceImage3}
      \end{subfigure}
\end{figure}

\end{document}

enter image description here

The demo option for graphicx simply replaces actual figures with black rectangles; do not use that option in your actual document.

Related Question