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