[Tex/LaTex] Aberrant footnote numbering behavior with footnoted captions

captionsfloatsfootnotesnumbering

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:

enter image description here

MWE2:

enter image description here

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:
enter image description here

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}

Best Answer

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:

enter image description here

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

Related Question