3

Here's a MWE of my problem. Using the following document

\documentclass{report}
\AtBeginDocument{\listoffigures}
\usepackage{cleveref}

\begin{document} \begin{figure} \caption{This caption cross-references \cref{eqn}.} \label{fig} \end{figure}

\begin{equation} F = ma \label{eqn}\end{equation} \end{document}

results in a document that looks like this:

enter image description here

However, if I remove the \listoffigures command from \AtBeginDocument and instead directly call it in the tex file (right after \begin{document}), I get this:

enter image description here

Evidently, in the first case cleveref can't figure out the type of reference, while in the second case it can. What is the difference between these two?

My real use case is a large LaTeX document class file that provides various customizations to the report class, one of which is to create the list of figures automatically, i.e, without the user needing to remember to insert the instruction \listoffigures. How can I get the correct information embedded in the document for cleveref to parse when using the first approach?

Mico
  • 506,678
Vyas
  • 1,385
  • I've taken the liberty of editing your code to simplify it to the absolute minimum needed to generate the screenshot you posted. Feel free to revert. – Mico Oct 03 '20 at 10:18
  • 1
    @Mico Thanks! I cut down a 1400 line class file and a 10000 line document to my original MWE trying to isolate the problem... it didn't occur to me to take the last step and move the AtBeginDocument out of a class file entirely :) – Vyas Oct 03 '20 at 10:59

1 Answers1

2

As you've noted, the instruction \AtBeginDocument{\listoffigures} ends up making sure that the \listoffigures is "queued up" before LaTeX has a chance to "learn" what \cref means.

A solution would be to instruct the class file to load the cleveref package as well -- and to do so before issuing the directive \AtBeginDocument{\listoffigures}.

\usepackage{cleveref}
\AtBeginDocument{\listoffigures}
Mico
  • 506,678
  • 1
    How does AtBeginDocument actually work? The way it's written, I assumed it was analogous to a preprocessor macro in C, i.e. the commands it contained would be textually expanded when \begin{document} is called. However, this problem clearly indicates that all the commands it contains are actually executed immediately, but somehow "queued up" like you said. The naming made me expect a lazier execution pattern, how does it handle the queuing? – Vyas Oct 03 '20 at 11:02
  • 1
    @Vyas - Just how \AtBeginDocument works is a very interesting question indeed. :-) Even though I think I have a pretty good grasp of what this instruction does, my understanding is nowhere near as thorough as that of some of the other, truly expert-level users who frequent this site. May I therefore suggest that you post a new query, in which you'd ask for an in-depth explanation of what \AtBeginDocument does? (To motivate why you're asking the new query, you could provide a link to the present query.) I'm sure that this new query would be of great interest to many users. – Mico Oct 03 '20 at 11:25
  • Done! Please add whatever tags you think might be appropriate to get attention from the right people, I thought documentclass-writing was about right but maybe there are other useful tags for builtin Latex2e commands. – Vyas Oct 03 '20 at 11:39
  • @Vyas why do you say that this indicates the commands are executed immediately? they are not, they are simply stored and executed at begin document/ – David Carlisle Oct 03 '20 at 14:20
  • @DavidCarlisle the original sequence of my commands was AtBeginDocument{...} \usepackage{cleveref} \begin{document}.... If the contents of AtBeginDocument are "simply stored and executed at \begin{document}", then switching the order to \usepackage{cleveref} AtBeginDocument{...} \begin{document}... should have no effect. Therefore, my conclusion was that something must be happening prior to the \begin{document} that is triggered by AtBeginDocument for this sequence change to fix the problem. – Vyas Oct 03 '20 at 18:10
  • @DavidCarlisle I think this post answers my question, though. You (and my original assumption) are probably right that execution is delayed until \begin{document}, but the issue is that cleveref is also probably depending on adding commands to AtBeginDocument and the sequence in which they're added is causing the problems. – Vyas Oct 03 '20 at 18:15
  • 1
    @Vyas yes look at cleverref.sty it is essentially \AtBeginDocument{a}\AtBeginDocument{b} so moving your atbegindocument is just asking why \AtBeginDocument{you}\AtBeginDocument{a}\AtBeginDocument{b} is different to \AtBeginDocument{a}\AtBeginDocument{b}\AtBeginDocument{you} they all get delayed then at begin document, one does you a b and the other does a b you. – David Carlisle Oct 03 '20 at 18:54