# Align a figure caption lines

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?

• Welcome to TeX.SX! It is usually quite helpful to add a complete working code example, as document classes, loaded packages etc. can influence the problem and its solution. – mafp Jul 5 '13 at 8:34

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}


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.
• Looks pretty handy! – Jagath Jul 5 '13 at 8:45

Here is one way to attain this:

\documentclass{article}

\makeatletter
\def\figwidth{15pc}% Adjust the caption width here
\long\def\@makecaption#1#2{%
\vskip\abovecaptionskip
\sbox\@tempboxa{#1: #2}%
\ifdim \wd\@tempboxa >\figwidth
\mbox{}\hfill\parbox{\figwidth}{%
#1: #2\par}\hfill\mbox{}%
\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\vskip\belowcaptionskip}

\begin{document}

\begin{figure}[H]
\centering
\fbox{\hbox to 20pc{\vbox to 5pc{\mbox{}}}}
\caption{Text text text text text text
On the basis of: }
\label{fig:fig}
\end{figure}

\end{document}


Please set the width of the caption as required by adjusting the macro \figwidth.