23

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)

2 Answers2

16

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
TH.
  • 62,639
  • 1
    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 03: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 07:53
  • 2
    How would one adjust this to work when the referencing command is \autoref from the hyperref package? – Chris Dec 27 '20 at 22:06
  • Rookie here, where do I "run" this, or am I just adding this to my document - if so where does it save it? So far I was just able to get an empty .refs file. – mrk Jul 31 '22 at 13:35
  • 1
    @mrk: the code should be added to the preamble of your document. If you tex file has name myfile.tex, then the output would be in myfile.refs. If it doesn't work: please ask a new question and link to this one. – Willie Wong Aug 02 '22 at 15:35
  • @Chris: if I were you I would probably just comment out \usepackage{hyperref}, and \let\autoref\ref and run the code above to generate the reference list. And then undo the source modification to build your real document. (The way hyperref is written, you will have to search through hyperref.sty and apply the same patch to every command that looks like \HyRef@<something>setref.) – Willie Wong Aug 02 '22 at 15:53
7

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.
Willie Wong
  • 24,733
  • 8
  • 74
  • 106
  • 1
    I thought the point of \includeonly was that it worked correctly with references. – TH. Oct 17 '10 at 00:30
  • 3
    @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 02:38
  • @Lev Bishop: Ah, okay. I've never actually used \includeonly. – TH. Oct 17 '10 at 03: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