There are many questions on this web site and elsewhere about ensuring that floats are displayed in the order in which they are placed in the source. What I want is different—I have all my floats at the end of my document and want to ensure that floats are displayed in the order in which they are referenced using \ref or \autoref. Using something like the endfloat package is not sufficient, because that requires me to make sure that the source order of the floats is the same as the reference order. It is easy to disrupt this ordering during editing.

It would suffice to have some sort of process that checked this for me automatically and required manual adjustments. Just as long as I don't have to go through and manually check again that Figure 13 is referenced in the output text before Figure 14.

1 Answer 1


Commenting out the \figuresatendtrue the paper will compile as normally (with figures in place). Otherwise, they will go at the end in order of references.




  % #1 = placement options
  % #2 = key
  \global\expandafter\def\csname figureatend@store@#2\expandafter\endcsname









\caption{Figure BBB}

\caption{Figure AAA}




The idea is to gather both the code for the figures and the references. Each command \ref{label} (and also \autoref{label} if hyperref is loaded) will update the internal variable \figureatend@save by adding \figureatend@do{label} to it. At the end we will execute this macro, see later.

Each figureatend environment takes as optional argument the usual positioning specifications; they will be ignored if we are in "endfigures" mode, but are handy for printing the document with figures in place. The mandatory argument is the label with which the figure will be referred to that mustn't be placed after \caption, contrary to the usual format. (It would be quite difficult to intercept a \label command in this environment and a label is necessary anyway.)

The contents of the environment becomes, in endfigures mode, the replacement text of the macro \figureatend@store@label, where label is the argument to the environment.

At the end of the run (with \AtEndDocument) we check whether the \figureatend@save macro is non empty and we execute it, that is we do


for each label found during the LaTeX run (in the exact order of appearance). The macro \figureatend@do{label} just check if \figureatend@store@label is defined and, if so, it does

<contents of the figureatend environment labeled by label> \label{label}\end{figure}

if the macro is defined; otherwise it does nothing.

A couple of LaTeX runs are required, as usual.

If the \figuresatend line is commented out, the figureatend environment becomes just a figure environment and nothing is added to the \ref related macro \@setref.

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.