# How to use \ref to refer to a \label in another document?

I have two documents and am trying to use the package xr to create a reference, in the second document, to a \label in the first. But ?? appears in the PDF instead. How do I enable the second to see the first's \labels?

Minimal example:

xr2a.tex

\documentclass[12pt,a4paper]{article}
\begin{document}
\section{A}\label{a}
\end{document}


xr2b.tex

\documentclass[12pt,a4paper]{article}
\usepackage{xr}
\externaldocument{xr2a}
\begin{document}
\ref{a}
\end{document}


I am using pdfLaTeX and TeXworks.

The files are in the same directory.

I don't want hyperlinks.

There is very little in the xr package's documentation -- have I used it correctly?

Some similar older questions on the same topic have got answers which say that xr needs to read the .aux file. pdfLaTeX lately runs OK without any .aux files. Does xr still need to read the .aux file nowadays? If so, how do I get TeXworks to get pdfLaTeX to a) keep the .aux file b) revert to the default behaviour of not keeping an .aux file?

• I am confused. Surely, the default behaviour is keeping the .aux file? Without it, even internal cross references don't work. Anyhow, the example works for me. Feb 1, 2019 at 11:32
• If pdfLaTeX runs and makes a .pdf, only the .tex, .synctex.gz and .pdf remain. I might have changed a setting, but nothing in TeXworks's Edit\Preferences menus seem relevant. The default with my installation has been to delete the .aux, since 15 Jun 2018. Behaviour sometimes changes automatically because a new version automatically installs itself. Internal cross-references within a document work fine. Feb 1, 2019 at 11:43
• Just to clarify: It is definitely not the default behaviour of pdfLaTeX (or any other LaTeX engine) to clean up the .aux files after itself. It must be a setting of your editor or your compilation tool (if you use that). The .aux file is required not only to get cross references with xr right, but also to be able to produce proper cross references within the document. Feb 1, 2019 at 11:46
• latex needs the aux file always whether or not you use xr otherwise no references or table of contents or lists of tables etc will work. Feb 1, 2019 at 11:49
• Eureka. I will explain in an answer. Feb 1, 2019 at 12:12

As David Carlisle pointed out, TeX engines need to write .aux files. So, XY problem, the Y now being "How do I stop the automatic deletion of the .aux file after a successful run?". I use TeXworks, and the solution in my case is: when using TeXworks to make a document containing \labels to which other documents \refer, select the compilation mode pdfLaTeX (not pdfLaTeX+MakeIndex+BibTeX). I had been using the compilation mode pdfLaTeX+MakeIndex+BibTeX -- presumably MakeIndex or BibTeX cleans up the .aux and .log files if the run succeeded. As moewe says, the pdfLaTeX phase does not delete those files, so it must be one of the later phases.
• If you are using MikTeX pdfLaTeX+MakeIndex+BibTeX runs texify. Usually texify should not delete .aux files after a successful run, but there is an option to enable that (-c). So maybe check your configurations that the -c is not included. Feb 1, 2019 at 13:55