# add right margin in caption with the width of the caption label

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}
\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{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]{
\caption[#1]{#2}
}


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
\caption[#1]{#2}
\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?

• Using package caption you can specify the width and the margin of captions. But i am not completely sure, if you can specifiy just the right margin. Feb 1, 2016 at 13:48
• according to the documentation of the package, it will always set both margins. I had the idea to set both margins and then set negative indention for the first line containing the label, but still don't really know how to do that Feb 1, 2016 at 14:17
• \newcommand{\mycaption}[1]{...} and you can use \thefigure for the figure number. Feb 1, 2016 at 15:26
• Thanks for the hint with \thefigure, that will help a lot to get the right spacing (however, it would be even more elegant to get the label directly). I will try this later in combination with an if-else structure for determining, if the caption is larger or smaller than the linewidth Feb 1, 2016 at 17:51
• @riddleculous No, according to the caption package documentation one can set both margins individually, e.g. "margin={0pt,10pt}". See section "2.4 Margins and further paragraph options".
– user2574
Feb 1, 2016 at 18:46

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.)

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

• thanks for the excellent answer, works like a charm! I've been so free as to add code snippets for table and subfigure, hope that's alright Feb 12, 2016 at 19:53