8

Using an up-to-date TeXlive 2012 (with all updates) this small document will produce a strange AUX file:

\documentclass{book}
\usepackage[list=on]{subcaption} 

% Suppress writing the definition of \caption@xref to the AUX file (does not help)
\makeatletter
\let\caption@@@xlabel\@empty
\makeatother

% Show the order of \addcontentsline use
\let\xxxxx\addcontentsline
\renewcommand\addcontentsline[3]{%
  \typeout{#1 - #2}%
  \xxxxx{#1}{#2}{#3}}

\begin{document} 
\listoffigures 
\begin{figure}
  \centering
  \subcaptionbox{\label{A}}{A}
  \subcaptionbox{\label{B}}{B}
  \caption{X}
\end{figure}
\end{document}

As you see from the output of the redefined \addcontentsline the order of the usage of \addcontentsline by the caption package is:

lof - figure
lof - subfigure
lof - subfigure

But the AUX file contains afterwards:

\relax 
\@writefile{lof}{\contentsline {subfigure}{\numberline {a}{\ignorespaces \caption@gobble  {A}\relax }}{1}}
\@writefile{lof}{\contentsline {subfigure}{\numberline {b}{\ignorespaces \caption@gobble  {B}\relax }}{1}}
\newlabel{A}{{1a}{1}}
\newlabel{sub@A}{{a}{1}}
\newlabel{B}{{1b}{1}}
\newlabel{sub@B}{{b}{1}}
\@writefile{lof}{\contentsline {figure}{\numberline {1}{\ignorespaces X\relax }}{1}}

So the order of code lines within the AUX file does not correspond to the order of the usage of \addcontentsline.

Does anybody know what is going on here and how to fix this?

P.S.: For a reason I don't understand yet the example document works fine when using v3.2f of my caption package but fails with version v3.3 -- although I haven't changed the algorithm for delaying the sub-figure LOF entries.

1 Answers1

9

If you use

\renewcommand\addcontentsline[3]{%
  \immediate\write20{!!#1 - #2}%
  \write20{??#1 - #2}%
  \xxxxx{#1}{#2}{#3}}

You get

!!lof - figure
!!lof - subfigure
!!lof - subfigure

??lof - subfigure
??lof - subfigure
??lof - figure

The immediate write reflects the order of execution but the toc needs a delayed write to get the page numbers, and that reflects the order of the \write nodes in the box at \shipout

edit: In this case the main problem is that the subcaption toc lines are delayed until outside the figure which mean that they are on the main vertical list of page 1 so come before the main caption which is in teh figure box so shipped out on page 2 (if you add [p] as suggested in the comments.

You can force the writing to be inside the figure but I'm not sure why the package is delaying it so this probably breaks something

\documentclass{book}
\usepackage[list=on]{subcaption} 

% Suppress writing the definition of \caption@xref to the AUX file (does not help)
\makeatletter
\let\caption@@@xlabel\@empty


% Show the order of \addcontentsline use
\let\xxxxx\addcontentsline
\renewcommand\addcontentsline[3]{%
  \immediate\write20{!!#1 - #2}%
  \write20{??#1 - #2}%
  \xxxxx{#1}{#2}{#3}}
%\showoutput
\begin{document} 
\listoffigures 

%\tracingall
xxx
{%
\begin{figure}[p]
  \centering

  \subcaptionbox{\label{A}zzz}{Accc}
  \subcaptionbox{\label{B}}{B}
  \caption{X}
 %%%% Keep writes inside the float
\caption@subcontentslines
\global\let\caption@subcontentslines\@empty
\end{figure}

}



\end{document}
David Carlisle
  • 757,742
  • 2
    I think Axel's problem is the page number: If you add the [p]-option to the figure you can see that the subcaptions points to page 1 while the figure and main caption is on page two. So what is delaying the \addcontentsline command of the main caption? – Ulrike Fischer Mar 09 '13 at 19:41
  • 1
    @UlrikeFischer the subcaption is apparently doing a non immediate write of the toc line outside the figure, if you look with \showoutput after adding [p] then the write node for the main caption is in the figure box, but the two write nodes for the sub caption are outside the figure box at the end of the first page. – David Carlisle Mar 09 '13 at 21:38
  • @Axel probably has a better idea what the package is doing there.... – David Carlisle Mar 09 '13 at 21:38
  • Code updated with a workaround – David Carlisle Mar 09 '13 at 22:21
  • 1
    @DavidCarlisle What my package is doing there? Horseplay I would say... I was so confused by the change of order of \addcontentsline yesterday that I was not even considering that it actually could be a very simple error of mine, and therefore I did not do investigations in other directions. In fact I forgot to redefine an internal macro after changing a hook within my add-contentline-stuff. So maybe we should close this question as too localized? –  Mar 10 '13 at 09:53
  • @AxelSommerfeldt Nice to see that it's solved! It's good that there's an accepted answer now, but your thought to additionally close as too localized is good because people don't need to further deal with the question. – Stefan Kottwitz Mar 10 '13 at 10:26