0

It seems that caption.sty expands the argument of \caption twice, which leads to incorrect or at least unexpected values of counters that are increased within the caption:

\documentclass{article}
  \usepackage{caption}
  \newcounter{mycounter}
  \setcounter{mycounter}{0}

  \begin{document}
    \begin{figure}
      \caption[]{%
        \stepcounter{mycounter}\themycounter
      }
    \end{figure}
  \end{document} 

I would expect that \themycounter would result in 1, but in this example it's 2 instead. caption.sty is quite a big file and I don't find the corresponding macro that expands the content of \caption twice. Is there any way to prevent this behavior or a hook that allows resetting the counter after the first expansion? Or do I have to redefine the \caption macro to reset the counter to its previous value?

  • Why do you print a counter inside a caption? – Bernard Jan 23 '17 at 0:37
  • Because it counts (and allows labeling and referencing) panels in the figure. As in "(A) Important Data. OVerview of the data (a) and zoom into some region (b). (B) Some more important data, at linear scale (a) and logarithmic scale (b)". No, the subfigure and subcaption packages are not what i'm looking for. Furthermore, I simply want to understand what's happening here. – Patrick Happel Jan 23 '17 at 0:41
  • Put \stepcounter{mycounter} inside the figure, but before \caption. – Bernard Jan 23 '17 at 0:43
  • This does not really explain what's happening. Furthermore, I want to use the \stepcounter (\refstepcounter, to be honest) inside another macro which also prints the labels for panels. If one has to put the definition outside the \caption macro, this would be not very handy and close to labeling panels manually. – Patrick Happel Jan 23 '17 at 0:47
  • I can't explain what's happening.If it has to be done for every caption, you can use etoolbox, and write in your preamble \pretocmd{\caption}{\refstepcpounter{mycounter}}{}{}. – Bernard Jan 23 '17 at 0:51
0

The standard latex caption code sets the content twice to see if it can be centred as one line or needs to be set as a paragraph.

Normally the caption package does the same but you can use

\captionsetup{singlelinecheck=off}

to turn off this behaviour.

  • However, this will make the caption flushleft, I suppose. – Bernard Jan 23 '17 at 0:53
  • @Bernard well it depends you can specify the paragraph formatting as centred for example. – David Carlisle Jan 23 '17 at 0:55
  • Yes, this works (and makes a one-line caption flusehd left). Interestingly, the standard latex \caption code seems to handle this differently than caption.sty. If you remove the caption package from the example above, the result of \themycounter is 1, not 2. – Patrick Happel Jan 23 '17 at 0:56
  • @PatrickHappel the standard code sets it once if it fits in a line (as here) and twice if it does not. – David Carlisle Jan 23 '17 at 1:01
  • Ah, ok, I see. This, of course, leads to the next question: How to circumvent this without using caption.sty? But the LaTeX kernel is much better to read than caption.sty, I'll try to find a solution to that myself. – Patrick Happel Jan 23 '17 at 1:12

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.