# Is there a way to have latex detect forward references?

I've got a very large document (which I've broken into "chapters" and such) which I've recently moved a few things around in. Is there a nice way to get latex to output a list of forward references? (like, things in chapter 5 that reference a result in chapter 10)

Here's a way to write out all of your references, forward, backward, and undefined to \jobname.refs.

\newwrite\refs
\openout\refs=\jobname.refs
\makeatletter
\renewcommand\@setref[3]{%
\ifx#1\relax
\write\refs{'#3' \thepage\space undefined}%
\protect \G@refundefinedtrue
\nfss@text{\reset@font\bfseries ??}%
\@latex@warning{Reference #3' on page \thepage\space
undefined}%
\else
\write\refs{'#3' \thepage\space
\expandafter\@secondoftwo#1}%
\expandafter#2#1\null
\fi
}
\makeatother


LaTeX doesn't keep track of chapter, so this writes out pages instead. The format is

'ref name' page refpage


You could easily change the \else case to do the \write\refs only if the second number was later than the first. Basically, change the \else clause to the following.

                \begingroup
\count@\expandafter\@secondoftwo#1\relax
\ifnum\c@page<\count@
\write\refs{'#3' \thepage\space
\expandafter\@secondoftwo#1}%
\fi
\endgroup
\expandafter#2#1\null

• This reminds me: one of the things I've always wanted to do is write a list of undefined or duplicate references at the end of the console output. Some day! (P.S. Great answer.) – Will Robertson Oct 17 '10 at 3:25
• Great answer! Just for those not familiar with \makeatletter, could you please add \makeatother at the end of your code? – Hendrik Vogt Oct 17 '10 at 7:53

TH. already gave a nice answer (I would even say that that is the way it ought to be done). But here a technologically cheap way since you've already broken it up in terms of chapters:

1. Remove all .aux files
2. \includeonly{chapter1} and compile. See if there are broken references. Then
3. \includeonly{chapter2} and compile. See if there are broken references.
4. Rinse and repeat.
• I thought the point of \includeonly was that it worked correctly with references. – TH. Oct 17 '10 at 0:30
• @TH Yes, but only if the .aux files are present: you have to \includeonly{chapterN} each chapter at least once in order to produce the chapterN.aux file, so Willie's recipe should indeed work. But your answer still is better in my opinion. – Lev Bishop Oct 17 '10 at 2:38
• @Lev Bishop: Ah, okay. I've never actually used \includeonly. – TH. Oct 17 '10 at 3:30
• Of course, TH.'s answer is much better. In my answer above, I counted on \includeonly` working with references already present in .aux files to only show broken forward references and not working backward references. This is just the sort of cheap thing one can put together at one in the morning without thinking too hard. – Willie Wong Oct 17 '10 at 13:27