# Identifying long-distance float and footnote references

Many people have asked questions about how to locate float references such that the float will appear near the first reference (note that a similar - but less common - problem exists for footnotes, as described in "Footnote being pushed to after a float page."). I'm hoping for an automated tool to identify potential problem floats and footnotes.

In a small document, it's relatively easy to identify and fix any problematic references, but in a large document (e.g. a dissertation or a book), the proofreading effort is considerable, and the problems may change considerably after minor editing. At the extreme, adding a sentence or a paragraph early in a chapter may affect float placement throughout that chapter - and I don't want to repeat the laborious proofing process after each compilation.

I'm imagining a PDF tool that would output a list of potentially problematic placements, for manual review. E.g. "Table 2.3 located 3 pages after the first reference", "Footnote 3 in chapter 4 located 2 pages after reference".

Such a tool could presumably use the PDF hyperlinks produced by the hyperref package to trace references within the document. Alternatively (and more simply), is there a pdflatex option to output a warning when placing a float or footnote more than x pages from the first reference?

Update: I've built myself a simple tool using the iText library, but it's pretty rudimentary, and I'd still be interested in a more sophisticated proofing tool.

This warns if the page of the first ref is no equal to the page with the thing being referenced

It makes

first ref {f2} on page 2 -> page 3
first ref {f5} on page 2 -> page 8


You could modify it in various ways eg change the equality test here

\ifnum#2=#3\relax\else


to something testing distance eg

\ifnum#2>\numexpr#3+5\relax


to only warn if it's more than 5 pages ahead.

If you use non integer page numbers (eg roman numerals) it would need a bit of extra code to make those \ifnum safe, and if you use hyperref there are four rather than 2 fields in the internal ref structure so

 \@secondoftwo\csname r@#1\endcsname\@empty\@empty


would probably need to be

 \@secondoffour\csname r@#1\endcsname\@empty\@empty\@empty\@empty
\documentclass{article}
\setlength\textwidth{.5\textwidth}
\setlength\textheight{20\baselineskip}
\def\a{One two three four five six. }
\def\b{Red yellow. \a Green blue black white \a\a}
\def\c{Something \stepcounter{enumi} \roman{enumi} \a\b\a\b\par}
\def\d{\c\c$\frac{\theenumi}{x}$\a\a}

\def\f#1#2{%
\begin{figure}[#1]
\centering FFF\\FFF \caption{fff\label{#2}}
\end{figure}}

\let\oldref\ref
\makeatletter
\def\ref#1{%
\expandafter\ifx\csname first@#1\endcsname\relax
\global\expandafter\let\csname first@#1\endcsname\@empty
\protected@write\@auxout{}{\protect\refcheck
{#1}%
{\expandafter\expandafter\expandafter
\@secondoftwo\csname r@#1\endcsname\@empty\@empty}%
{\thepage}}%
\fi
\oldref{#1}}

\def\refcheck#1#2#3{%
\ifx\\#2\\\else
\ifnum#2=#3\relax\else
\typeout{first ref {#1}  on page #3 -> page #2}%
\fi
\fi}

\begin{document}

\d
aaa\ref{f1}  and \ref{f2} and \ref{f5}

\f{h}{f1}
\f{t}{f2}
\d
bbb \ref{f5} again
\f{t}{f3}
\f{H}{f4}% end of document
\f{t}{f5}
\d\d\d

\end{document}