5

This is about caption package. I read the well-written manual of this package (but not thoroughly, because I am new to LaTex) and found not enough about number(s) associated with caption label. So,

  1. How one can substitute the period between chapter and figure numbers with a custom symbol, say an emdash (i.e. as FIGURE 7—1 and without \renewcommand)?
  2. Likewise, what about putting a custom symbol right before caption number, something like FIGURE P7—1?

And the third question about caption alignment,

  1. Is it possible to align the caption with figure borders (here I mean left border)? How?

This is a typical MWE:

\documentclass{book}

\usepackage{blindtext}
\usepackage[demo]{graphicx}

\usepackage{caption}
    \DeclareCaptionStyle{mystyle}[]{
        name=FIGURE,
        labelsep=newline
        }

\setcounter{chapter}{7}

\begin{document}
    \begin{figure}
        \centering
        \includegraphics[width=10cm]{testimage}
        \captionsetup{style=mystyle}
        \caption{Caption of my figure}
    \end{figure}
\end{document}

And the output,

enter image description here


Of course I read those questions about using emdash instead of period or caption alignment, but if possible I like to use amsmath and caption package only.

  • \DeclareCaptionLabelSeparator{emdash}{\textemdash} for example for the emdash feature ;-) – user31729 Jun 17 '16 at 18:59
  • @ChristianHupfer I appreciate your comment and previous answer, but this put the emdash as a label separator not between chapter and figure numbers. – Shaqpad Jun 17 '16 at 19:37
  • Well, I deleted my answer. It was an error of course – user31729 Jun 17 '16 at 19:41
3

It depends on how you want the references to be printed.

If you want that the reference to figure or table 7–1 is printed as “7.1”, here's a possibility:

\documentclass{book}

\usepackage{graphicx}
\usepackage{caption}

\makeatletter
\DeclareCaptionLabelFormat{pcdn}{% Prefix Chapter Dash Number
  \MakeUppercase{#1} P\arabic{chapter}--\arabic{\@captype}%
}
\makeatother

\DeclareCaptionStyle{mystyle}{
  labelformat=pcdn,
  labelsep=newline
}

\setcounter{chapter}{7}

\begin{document}

We see in figure~\ref{ei}

\begin{figure}[htp]
\captionsetup{style=mystyle}

\centering

\begin{minipage}{10cm}
\includegraphics[width=\textwidth]{example-image}
\caption{Caption of my figure}\label{ei}
\end{minipage}

\end{figure}

\end{document}

enter image description here

The caption package allows defining various formats; in this case we want to change the format for the label (by default “Figure n” or “Table n”, where n is the number). In the second argument to \DeclareCaptionLabelFormat one can use #1 that stands for the appropriate environment name and #2 for the value of the associated counter. Since we want the label in uppercase, \MakeUppercase{#1} is the obvious choice. However since the number should be typeset in a special way, detached from the reference, we can use explicitly the counters, so P\arabic{chapter}--\arabic{figure} might seem the good choice; we can do better, though: in a float environment, \@captype stands either for figure or for table, so \arabic{\@captype} will expand to the needed counter. Since we're using a command with @ in its name, the code must be surrounded by \makeatletter and \makeatother, see What do \makeatletter and \makeatother do?.


If the reference should be “P7–1” just like in the caption, the code can be simpler.

\documentclass{book}

\usepackage{graphicx}
\usepackage{caption}

\DeclareCaptionLabelFormat{pcdn}{\MakeUppercase{#1} #2}

\renewcommand{\thefigure}{P\arabic{chapter}--\arabic{figure}}

\DeclareCaptionStyle{mystyle}{
  labelformat=pcdn,
  labelsep=newline
}

\setcounter{chapter}{7}

\begin{document}

We see in figure~\ref{ei}

\begin{figure}[htp]
\captionsetup{style=mystyle}

\centering

\begin{minipage}{10cm}
\includegraphics[width=\textwidth]{example-image}
\caption{Caption of my figure}\label{ei}
\end{minipage}

\end{figure}

\end{document}

enter image description here

  • 1
    Instead of the minipageone could use \captionbox{<caption>}{<content>} as an alternative. – Axel Sommerfeldt Jun 18 '16 at 8:37
  • Yes this is exactly the solution I was looking for. Could you please direct me to an intermediate-level reference on \makeatletter and \makeatother used above? I don't see what they do. Is that a kind of hacking the caption package? – Shaqpad Jun 18 '16 at 18:15
  • @Shaqpad The standard reference is What do \makeatletter and \makeatother do?; I'll add some comments to the first solution later. – egreg Jun 18 '16 at 18:18

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.