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 thecaption
package to setup a right margin which hopefully fits your specification:The syntax of the
calcmargin=
option is the same asmargin=
but its value will not be set immediately but calculated for every individual caption instead. (Unfortunatelycalcmargin=
isn't documented yet.)For example, the same is possible for tables with the same format:
and for subfigures in the format of
(a)
,(b)
, etc. using thesubcaption
packageUpdate: My first example code was crap, hopefully this one is better.
Update 2 (by riddleculous): added the same for tables and subcaptions