1

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?

  • 1
    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. – Johannes_B Feb 1 '16 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 – riddleculous Feb 1 '16 at 14:17
  • \newcommand{\mycaption}[1]{...} and you can use \thefigure for the figure number. – John Kormylo Feb 1 '16 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 – riddleculous Feb 1 '16 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". – Axel Sommerfeldt Feb 1 '16 at 18:46
3

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

  • 1
    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 – riddleculous Feb 12 '16 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.