0

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.

David Carlisle
  • 757,742
  • why \@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
  • @DavidCarlisle good point. The truth is that I don't understand what \@addtofilelist does. 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

1 Answers1

1

On the commandline it is simpler to run latexmk on each file, but in Overleaf or other systems where you have no direct access to the commandline you could do

latexmkrc

$xelatex="xelatex %O file_a;xelatex %O file_b"

file_a.tex

\documentclass{article}
\usepackage{xr}

\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}

\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}

A single call to

latexmk --xelatex file_a

then produces

enter image description here

enter image description here

David Carlisle
  • 757,742
  • Thanks @DavidCarlisle. I think your approach works. I was under the (mistaken?) impression, however, that latexmk should work a bit like Makefile: you specify relations among objects and it takes care of the rest, you don't have to provide explicit command lines. I also think that redefining $xelatex (this is what you are doing, right?) is a bit like putting alias gcc=gcc [...] in a script file. The purpose of a Makefile is that you don't have to resort to this kind of hacks, so I was hoping to avoid it with latexmk. Does it make sense? – Alberto Santini Jan 12 '23 at 16:53
  • @AlbertoSantini you are asking for a hack to make a system designed for a single purpose of making one pdf file, to make two. So if it works it is an answer, you shouldn't look for beauty. This only makes sense on overleaf or similar where you have no control over the commandline. overleaf is going to call latexmk with arguments to make one pdf from file_a, this config says ignore the arguments overleaf supplies, and do what you want to do, run xelatex on both files. You can not use make, or a simple bash file that calls xelatex on each file, you just have latexmk..... – David Carlisle Jan 12 '23 at 17:44
  • Yeah, I think I misunderstood the purpose of latexmk. I was under the impression it would be more like make, but I am starting to think that is more like a back-end/biblatex agnostic version of pdflatex. – Alberto Santini Jan 12 '23 at 17:53