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 Answers 2

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
6
  • 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.) Oct 17, 2010 at 3:25
  • Great answer! Just for those not familiar with \makeatletter, could you please add \makeatother at the end of your code? Oct 17, 2010 at 7:53
  • 2
    How would one adjust this to work when the referencing command is \autoref from the hyperref package?
    – Chris
    Dec 27, 2020 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, 2022 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. Aug 2, 2022 at 15:35
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.
4
  • 1
    I thought the point of \includeonly was that it worked correctly with references.
    – TH.
    Oct 17, 2010 at 0: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, 2010 at 2:38
  • @Lev Bishop: Ah, okay. I've never actually used \includeonly.
    – TH.
    Oct 17, 2010 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. Oct 17, 2010 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .