This question is related to another unanswered one. Mine is not specific to Overleaf, and I provide an MWE.
I have the following two files:
% file_a.tex
\documentclass{article}
\usepackage{xr}
\makeatletter
@addtofilelist{file_b.tex}
@addtofilelist{file_b.aux}
\makeatother
\externaldocument{file_b}
\begin{document}
\section{File A Section 1}\label{a:sec1}
File B Section 2: \ref{b:sec2}.
\section{File A Section 2}\label{a:sec2}
File B Section 1: \ref{b:sec1}.
\end{document}
% file_b.tex
\documentclass{article}
\usepackage{xr}
\makeatletter
\@addtofilelist{file_a.tex}
\@addtofilelist{file_a.aux}
\makeatother
\externaldocument{file_a}
\begin{document}
\section{File B Section 1}\label{b:sec1}
File A Section 2: \ref{a:sec2}.
\section{File B Section 2}\label{b:sec2}
File A Section 1: \ref{a:sec1}.
\end{document}
I can make the cross-references work by first compiling file_a.tex (thus producing file_a.aux), then compiling file_b.tex (thus producing file_b.aux) and then compiling file_a.tex again:
xelatex file_a.tex && xelatex file_b.tex && xelatex file_a.tex
However, I am not able to find a way to correctly compile both files with a single run of latexmk.
This is my .latexmkrc, which is similar to those found in OverLeaf's knowledge base and in the previously linked answer.
# Use XeLaTeX
$pdf_mode = 5;
$dvi_mode = 0;
$postscript_mode = 0;
add_cus_dep('tex', 'aux', 0, 'makeexternaldocument');
sub makeexternaldocument {
if(!($root_filename eq $[0])) {
system("latexmk -cd -xelatex "$[0]"");
}
}
If I run latexmk file_a.tex I do not get the correct output in file_a.pdf (references are replaced by ??) and I get no file_b.pdf at all.
Ideally, I would like to have both file_a.pdf and file_b.pdf (and with the proper references!) with a single run.
We can assume that these are the only two files I will ever need, and there will not be a graph of dependencies between any other file.


\@addtofilelist{file_a.tex}? It will no doubt be possible to run both files but why do that? All commands involved assume a single generated file, certainly a latexmk config file will be larger and harder to understand than a simple shell script that calls latexmk twice. – David Carlisle Jan 12 '23 at 15:11\@addtofilelistdoes. I tried to DuckDuckGo it quite extensively, but I could not find a single resource which explains it. So, I am guilty of blindly copy-pasting without understanding. O:-) – Alberto Santini Jan 12 '23 at 16:49