- The following marks the dependencies on the external
tex and aux files in the main file, even if those files do not already exist (there is otherwise no tex file dependency and the aux file dependency doesn't automatically happen if the file doesn't exist - the warning generated by the xr package is not processed by latexmk because it is different to the standard LaTeX warnings; namely it is missing a period after the filename).
- It then uses a custom dependency to
latexmk such that it knows how to build aux files. If an aux file with a different root filename needs to be regenerated, it causes latexmk to process the corresponding job (in a separate system call). Note that with this approach, any warnings from latexmk generated while processing the external documents will not be summarised at the end of the main run, and the concepts of $max_repeat and Run number $pass{$rule} of rule '$rule' are weakened!
- This works best when
latexmk has the recorder option activated (default since v4.31, otherwise enabled through the command line option -recorder=1 or appropriate line in .latexmkrc); without this, changes to the external aux files may be missed, though changes to the external tex files will still start the correct processing.
- Currently, this requires use of the command
\myexternaldocument instead of \externaldocument, though the original command could be patched.
I have provided the code in the context of a minimum example (though none was provided in the question) for better illustratation. Three files are shown below: file1.tex, file2.tex and .latexmkrc.
file1 references something in file2.
- Executing
latexmk file1 is sufficient to correctly build both file1 and file2.
- Remember that using the
-deps option to latexmk will show the dependency database and may help to verify how things are working.
- Note the commented line in
file2.tex to facilitate testing.
Here's file1.tex:
\documentclass{article}
%%% HELPER CODE FOR DEALING WITH EXTERNAL REFERENCES
\usepackage{xr}
\makeatletter
\newcommand*{\addFileDependency}[1]{% argument=file name and extension
\typeout{(#1)}% latexmk will find this if $recorder=0 (however, in that case, it will ignore #1 if it is a .aux or .pdf file etc and it exists! if it doesn't exist, it will appear in the list of dependents regardless)
\@addtofilelist{#1}% if you want it to appear in \listfiles, not really necessary and latexmk doesn't use this
\IfFileExists{#1}{}{\typeout{No file #1.}}% latexmk will find this message if #1 doesn't exist (yet)
}
\makeatother
\newcommand*{\myexternaldocument}[1]{%
\externaldocument{#1}%
\addFileDependency{#1.tex}%
\addFileDependency{#1.aux}%
}
%%% END HELPER CODE
% put all the external documents here!
\myexternaldocument{file2}
% just to see what's happening
\listfiles
\begin{document}
Label1 is on page \pageref{label1} of file2.
\end{document}
And file2.tex:
\documentclass{article}
\usepackage{lipsum}
\begin{document}
%\lipsum[1-10]% to test, comment or uncomment this line, and rerun `latexmk file1`. Note that both are then rebuilt as necessary and the page number in the generated `file1` is always correct.
\section{Label1}
\label{label1}
This is referenced by file1.
\end{document}
And finally .latexmkrc:
add_cus_dep( 'tex', 'aux', 0, 'makeexternaldocument' );
sub makeexternaldocument {
# if the dependency isn't one of the files that this latexmk run will consider, process it
# without this test, we would get an infinite loop!
if (!($root_filename eq $_[0]))
{
system( "latexmk \"$_[0]\"" );
}
}
\myexternaldocument[mapp:]{MovantsAppendix} `
– Daniel Doherty Jun 20 '22 at 20:21latexmkversions, namely: ` ``Latexmk: Possible bug: In linking rules, I already set from_rules_main{file1.aux} to 'cusdep tex aux file1' But now I also have a different rule 'latex' that also made the file.``` This is also mentioned here. – Oliver Freyermuth Sep 14 '22 at 18:52file1referencesfile2, andfile2referencesfile1)? I'm getting an apparently infinite loop. Manually runningpdflatexon both files a couple of times works fine though. – Jellby Dec 08 '22 at 08:50I also modified the
– Vegard Gjeldvik Jervell Feb 22 '23 at 10:20\myexternaldocumentcommand to add tags, and defined a\newcommand{\myexternalref}[1]{\ref{mytag:#1}}for each of the externals to simplify referencing with tags.