[Tex/LaTex] add right margin in caption with the width of the caption label

captionsmargins

Currently, my figure/table captions look like this: The should start with the label Fig. XX and then the text with the following caption lines hanging behind the label. If there is only one line, the caption is centered.

Now, I would like to add an extra margin on the right with the same width as the caption label (i.e. the same width as the indention of the second/third… line of the caption). But if there is only one line necessary, I would like to keep it the old way: centered with no extra right margin.
Manually, I could do this by adding a margin for the respective caption, e.g. for Fig. 3.6:

\begin{figure}
    \centering
    \includegraphics{figure3.6.pdf}
    \begin{addmargin}[0em]{\widthof{\small \textbf{Fig. 3.6: }}}
        \caption{This is a long caption spanning multiple lines so that I would like this to be formatted as stated above. But no shorter captions, where this is unnecessary.}
    \end{addmargin}
\end{figure}

Now the question is, if and how this can be automated (in the preamble) so that I don't have to do it for each figure or table.

Update:
what works is the following:

\newcommand{\myfigcaption}[2][\#2]{
    \begin{addmargin}[0em]{\widthof{\small \textbf{Fig. \thefigure: }}}
        \caption[#1]{#2}
    \end{addmargin}
}

and the same with tables. What I haven't got yet is

(a) how to compare the length of the caption (argument #2) with \textwidth. I tried the following:

\newcommand{\myfigcaption}[2][\#2]{
    \ifdim\widthof{{\small \textbf{Fig. \thefigure: }}#2}>\textwidth    
        \begin{addmargin}[0em]{\widthof{\small \textbf{Fig. \thefigure: }}}
            \caption[#1]{#2}
        \end{addmargin}
    \else
        \caption[#1]{#2}
    \fi
}

(b) how to determine whether I am in a figure or a tabular environment.

Or is there a much easier way?

Best Answer

As announced in my comment above here comes a MWE which uses the option calcmargin= of the caption package to setup a right margin which hopefully fits your specification:

\documentclass{article}

\usepackage{calc}
\usepackage[figurename=Fig.]{caption}
\DeclareCaptionStyle{figstyle}
  [format=plain,margin=0pt,justification=centering]
  {format=hang,calcmargin={0pt,\widthof{\captionfont\captionlabelfont\figurename~\thefigure: }},
   font=small,labelfont=bf}
\captionsetup[figure]{style=figstyle}

\begin{document}
\begin{figure}
\hrulefill
\caption{Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah\ldots}
\hrulefill
\end{figure}
\begin{figure}
\hrulefill
\caption{Blah blah blah blah blah blah blah blah blah blah blah\ldots}
\hrulefill
\end{figure}
\end{document}

The syntax of the calcmargin= option is the same as margin= but its value will not be set immediately but calculated for every individual caption instead. (Unfortunately calcmargin= isn't documented yet.)

Blah

For example, the same is possible for tables with the same format:

\DeclareCaptionStyle{tabstyle}
[format=plain,margin=0pt,justification=centering]
{format=hang,calcmargin={0pt,\widthof{\captionfont\captionlabelfont\tablename~\thetable: }},
  font=small,labelfont=bf}
\captionsetup[table]{style=tabstyle}

and for subfigures in the format of (a), (b), etc. using the subcaption package

\DeclareCaptionStyle{subfigstyle}
[format=plain,margin=0pt,justification=centering]
{format=hang,calcmargin={0pt,\widthof{\captionfont\captionlabelfont(\thesubfigure) }},
  font=small,labelfont=bf}
\captionsetup[subfigure]{style=subfigstyle}

Update: My first example code was crap, hopefully this one is better.

Update 2 (by riddleculous): added the same for tables and subcaptions