Problem Description
I am trying to setup latexmk using a .latexmkrc configuration file for "precompiling" the preamble of my documents, and reusing that during reruns.
According to a comment by David Carlisle, using precompliation doesn't really save enough time to be worth the bother [he] wrote mylatex.ltx back in 1988.
However, I have a lot of externalized graphics in my documents, and references inside pgfplots, so the loading times grow quite fast in my case, and I want to explore how much precompilation could speed up the entire process.
Requirements
My current requirements are:
- preamble and document must be able to reside inside the same document, for this question let it be
mwe.tex. - All compilation must be done from one single call to
latexmk.
Literature Review
In particular, I have looked into the following questions and answers:
https://tex.stackexchange.com/a/37730 is a bit dated, but the general principle can be taken to more recent versions of LaTeX. The main problem with this proposed solution is, that as stated, [y]ou'll need to run
pdflatex -ini ...manually the first time, to create the.fmtfile, which contradicts my second requirement.https://tex.stackexchange.com/a/269052 shows how the preamble my be dumped from one single file, which is
pdflatex -ini \&pdflatex mylatex.ltx mwe.texIn particular, this is helpful in view of requirement 1.
After own research, I have found that a system call
pdflatex -ini \&pdflatex mylatexformat.ltx mwe.texwould be the more recent way to do this.
My custom minimal non-working example
Using the previous information and shortcomings in view of my requirements, I intend so sole this problem in the following steps:
- Modify the
$pdflatexvariable in.latexmkrcto require a precompiled preamblemwe.fmtfile. - Use
add_cus_dep('tex', 'fmt', 1, 'precompilepreamble');to add a dependency. - Define the perl subroutine
precompilepreambleto create themwe.fmtfile if necessary.
I expect the program to behave in the following way:
- Call
latexmk, which loads my custom.latexmkrcconfiguration. - First run of
pdflatexfails, and will log the missing required filemwe.fmt. latexmkdecides an additional run is needed, and will look up the dependencyies for.fmt.- Accordingly,
latexmkwill run the custom rule I added previously to generatemwe.fmt. - Afterwards,
latexmkwill run my modified$pdflatexagain and continue sucessfully.
Attached you find the required files:
mwe.tex
\documentclass{scrbook}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{lipsum}
\begin{document}
\title{Shakespeare's Externalize}
\subtitle{To externalize or not to externalize?}
\subject{Case Studies}
\date{\today}
\maketitle{}
\lipsum[1-20]
\end{document}
.latexmkrc
# ensure_path( 'TEXINPUTS', './src/' );
# ensure_path( 'TEXINPUTS', '../src/' );
Set the program used to generate the PDF
1: pdflatex
2: postscript conversion, don't use this
3: dvi conversion, don't use this
4: lualatex
5: xelatex
$pdf_mode = 1;
Add custom dependency.
latexmk checks whether a file with ending as given in the 2nd
argument exists ('toextension'). If yes, check if file with
ending as in first argument ('fromextension') exists. If yes,
run subroutine as given in fourth argument.
Third argument is whether file MUST exist. If 0, no action taken.
add_cus_dep('tex', 'fmt', 1, "compilepreamble");
show custom dependencies for debugging
show_cus_dep();
parse missing files
push @file_not_found, '^I can't find the format file `(.*)'!';
sub compilepreamble {
my ( $base) = @_;
# just output a simple debug string to console
print ("**** compilepreamble: ( $base ) ****\n");
# create the fmt file
$ret = system("pdflatex",
"-jobname", "$base",
"-ini",
"&pdflatex" ,
"mylatexformat.ltx",
$base
);
# if there were errors, return the error codes
return $ret;
}
modify $pdflatex to use custom format
$pdflatex = 'pdflatex -fmt mwe %B %O %S';
Observed Behaviour
Instead of pdflatex failing, it tries to run mktexfmt:
$ latexmk -g
Custom dependency list:
tex fmt 1 compilepreamble
Rc files read:
.latexmkrc
Latexmk: This is Latexmk, John Collins, 7 Jan. 2023. Version 4.79.
Force everything to be remade.
Latexmk: applying rule 'pdflatex'...
Rule 'pdflatex': Reasons for rerun
Category 'other':
Rerun of 'pdflatex' forced or previously required
Run number 1 of rule 'pdflatex'
Running 'pdflatex -fmt mwe -recorder "mwe.tex"'
This is pdfTeX, Version 3.141592653-2.6-1.40.24 (TeX Live 2022) (preloaded format=mwe)
restricted \write18 enabled.
kpathsea: Running mktexfmt mwe.fmt
mktexfmt: mktexfmt is using the following fmtutil.cnf files (in precedence order):
...
I can't find the format file `mwe.fmt'!
Latexmk: fls file doesn't appear to have been made.
Latexmk: Getting log file 'mwe.log'
Latexmk: Couldn't read log file 'mwe.log':
No such file or directory
Question
- How can I prevent
latexmk, or rather,pdflatexfrom trying to runmktexfmt? - The line
I can't find the format file `mwe.fmt'!is only output to console and nomwe.logis generated, which makes reruns impossible, and does not detect my custom dependency. How can I change this behaviour? - I could inject a custom subroutine into
$pdflatex, which would then check if the format file exists, and possibly create it if not. This could even be combined with a hashing process, to ensure only recompilation if needed and ... oh my, I notice I am copying the functionality oflatexmk. So, is there a way to do this inlatexmkin the first place?
I'm also looking forward to hearing other suggestions.
[-no]-mktex=FMT disable/enable mktexFMT generation (FMT=tex/tfm/pk)commandline option (seepdftex --help) – David Carlisle Jul 05 '23 at 16:28pdflatex --no-mktex=tex --fmt=mwe mwe.tex(which I believe is what @DavidCarlisle suggested), it seems that options-fmtand--no-mktexare exclusive/overriding each other. I couldn't find documentation on this.Just to clarify, I want
– marc Jul 05 '23 at 17:04latexmkto try to use the format on the first run and just fail with appropriate error in log file.pdftex --no-mktex=fmt --fmt mweseems to work, i getI can't find the format file \mwe.fmt'!` – David Carlisle Jul 05 '23 at 17:29--no-mktex=fmt, this choice was not documented. – marc Jul 06 '23 at 13:23