[Tex/LaTex] Align a figure caption lines

captionsfloatshorizontal alignment

I have an image with caption in LaTeX:

\begin{figure}[H]
\centering
   \includegraphics[width=0.8\textwidth,height=0.8\textheight,keepaspectratio]{img/image9}
   \caption{Text text text text text text\\*
   On the basis of: \cite{Smi00}}
\label{fig:fig}
\end{figure}

it looks like that:

|                                                           |
|___________________________________________________________|
          Figure 3.3 Text text text text text text
                   On the basis of: [4]

...

I'd like to align both lines of caption, but keep the centering of the whole caption, i.e. to change it to something like that:

|                                                           |
|___________________________________________________________|
          Figure 3.3 Text text text text text text
          On the basis of: [4]

...

Not:

|                                                           |
|___________________________________________________________|
Figure  3.3    Text    text    text     text    text     text
On the basis of: [4]

...

Or:

|                                                           |
|___________________________________________________________|
          Figure 3.3 Text text text text text text
On the basis of: [4]

...

…but all my attempts failed.

Does anybody know how it can be done?

Best Answer

Package varwidth provides environment varwidth that is a minipage, but the width is reduced, if the lines are shorter. The caption text can be formatted with environment varwidth by an own caption format that can be defined with package caption:

\documentclass{article}

\usepackage{caption}
\usepackage{varwidth}
\DeclareCaptionFormat{varwidth}{%
  \begin{varwidth}{\linewidth}#1#2#3\end{varwidth}%
}
\captionsetup{format=varwidth}

\begin{document}
\begin{figure}
  \centering
  \rule{.8\linewidth}{1ex}% simulating \includegraphics
  \caption{Text text text text text text\\*
    On the basis of [1]}
 \label{fig:fig}
\end{figure}
\end{document}

Result

Remarks:

  • For the caption entry in the list of figures you can use the optional argument of \caption to provide a (shorter) version without an explicit line break.
Related Question