# Aberrant footnote numbering behavior with footnoted captions

I don't have an explanation for the behavior shown by the following two MWEs. Clearly, the footnote number is altered by the length of text in the caption. Please advise me as to why this strange behavior is occurring and how to avoid it?

MWE1:

MWE2:

MWE1 code:

\documentclass{article}
\usepackage[hmargin=2in,vmargin=5in]{geometry}
\begin{document}
\begin{figure}
\label{fig:temp}
\caption[Caption for LOF]{Caption\footnotemark. Some more text added here changes the footnote number in a length-dependent manner.}
\end{figure}
\footnotetext{Footnote text.}
\end{document}


MWE2 code:

\documentclass{article}
\usepackage[hmargin=2in,vmargin=5in]{geometry}
\begin{document}
\begin{figure}
\label{fig:temp}
\caption[Caption for LOF]{Caption\footnotemark. Less text rectifies the foonote number.}
\end{figure}
\footnotetext{Footnote text.}
\end{document}


Edit. In response to a comment, I provide a demonstration that the aberrant numbering can occur even if the caption is but a single line.

MWE3:

MWE3 code:

\documentclass{article}
\usepackage[hmargin=1.9in,vmargin=5in]{geometry}
\begin{document}
\begin{figure}
\label{fig:temp}
\caption[Caption for LOF]{Caption\footnotemark. Some more text added here changes the footnote number.}
\end{figure}
\footnotetext{Footnote text.}
\end{document}

-
The caption text will be evaluated twice if the length of the text does exceed one single line. It's a known "feature" of the default implementation offered by the standard document classes. Other document classes, or packages which patches \caption (like my caption package) offers a different behaviour. –  Axel Sommerfeldt Feb 8 '12 at 6:07
Thank you very much @AxelSommerfeldt for your insight and for suggesting your caption package, which indeed resolved the issue. I had suspected that the change in the footnote number was related to the number of lines in the caption, but I have been able to reproduce the errant numbering with a single-line caption, as you will see in my edited post. How can this be explained? Perhaps a new line was inserted since the caption text occupies nearly the entire first line? Lastly, could you speculate on why evaluating the caption text twice if it exceeds a single line would be a useful feature? –  user001 Feb 8 '12 at 7:13
The exact criteria is: If the natural width of the caption text will exceed a single line, the caption is typeset as a paragraph and therefore evaluated twice. (Once for the check and once when typesetting.) If the natural width will fit into a single line, the box used for the check will be reused for typesetting, and therefore the caption text will only be evaluated once. (tbc) –  Axel Sommerfeldt Feb 8 '12 at 7:31
Now back to your questions: As you see I have written "natural width". If the line is only a very small amount wider than the place available for the line, if will fail the "single-line-test" but be typeset in a single line anyway. (See also: line-breaking and hyphenation rules of TeX) (BTW: captions are typeset with \sloppy.) Regarding your 2nd question: It's just a side-effect of the implementation, I don't think this behaviour was intended. –  Axel Sommerfeldt Feb 8 '12 at 7:39
Thank you Axel, your insight was very helpful, and your package resolved the issue. –  user001 Feb 8 '12 at 7:46

As mentioned by @AxelSommerfeldt, the default document classes evaluates the \caption{<stuff>} twice in the case when <stuff> is wider than a single line. Here's where the decision is made - \caption is originally defined in latex.ltx, which subsequently calls \@makecaption; taken from article.cls:

\long\def\@makecaption#1#2{%
\vskip\abovecaptionskip
\sbox\@tempboxa{#1: #2}%
\ifdim \wd\@tempboxa >\hsize
#1: #2\par
\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\vskip\belowcaptionskip}


It first stores the entire caption (say, Figure 1: <stuff>) inside a box:

\sbox\@tempboxa{#1: #2}%


In your case, this evaluates \footnotemark. Then it tests to see whether the width of the box is wider than \hsize (the width of the text block). If this is true, it sets the caption again which evaluates \footnotemark for the second time:

\ifdim \wd\@tempboxa >\hsize
#1: #2\par


If the box width is less than \hsize, it centres it in another box of width \hsize:

\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi


The latter part motives the dual evaluation - the possibility to either set the caption as a paragraph block, or as a single, centred line. It's both a feature and a drawback in your particular case.

You may question that you were able to provide an example of a single caption that also yielded an incorrect \footnotemark. This is possible because the box-storing command

\sbox\@tempboxa{#1: #2}


is set without regard for \hsize (the text block width). This implies that no stretching/shrinking occurs between words. If you add the geometry option showframe to your example, you'll note that the caption fits exactly within the text block because of the inter-word glue that can shrink. However, setting the same in a box that doesn't require any glue usage shows that it is actually wider than \hsize:

\documentclass{article}
\usepackage[hmargin=1.9in,vmargin=5in,showframe]{geometry}% http://ctan.org/pkg/geometry
\begin{document}
\begin{figure}
\makebox[\hsize][l]{Figure 1: Caption\textsuperscript{2}. Some more text added here changes the footnote number.}
\caption[Caption for LOF]{Caption\footnotemark. Some more text added here changes the footnote number.}\par
\label{fig:temp}
\end{figure}
\footnotetext{Footnote text.}
\end{document}


How do you avoid this? You can either load the caption package that redefines \caption appropriately (preferred), or set the \footnotemark in a box that doesn't re-expand (or re-evaluate) when the caption is set twice:

\sbox0{\footnotemark}% Store \footnotemark
\caption[Caption for LOF]{Caption\usebox0. Some more text added here changes the footnote number.}


Although this works, it removes any hyperref capability.

-
Thank you Werner for your clear explanation of the code. The step-by-step walkthrough was very helpful. If I understand correctly, the reason my single-line caption triggers an increase in the footnote number is that the caption exceeds \hsize, triggering a second evaluation of the caption text ( #1: #2\par), but inter-word glue shrinks to allow the caption to be typeset as a single line. I suppose I don't understand why the caption would be placed in a box before checking its length. Couldn't one just check the length and then either typeset as a centered line or as a paragraph? –  user001 Feb 9 '12 at 5:49
@user001: TeX works with has no regard for characters in the literal sense. Instead, everything is measured in terms of boxes that have a width, height and depth. The way to measure things is therefore via boxing. That is, "check the length" requires you to box it and then check the width of the box. I'm assuming at some lower level, other (programming) languages do exactly the same. –  Werner Feb 9 '12 at 5:56