I want to preprocess an input file before compiling it with, say, pdflatex.
I found one way to do this using the "internal" specification described in the latexmk manual, but I expected there to be a simpler and more flexible way using custom dependencies.
I'm using the preprocessor lhs2TeX (version 1.18.1) with the file extension .lhs
. My latexmk version is 4.37 (2 July 2013).
The "internal" way
A document input.lhs
:
\documentclass{article}
%include polycode.fmt
\begin{document}
Hello, |world|! % The | and | are used by lhs2TeX.
\end{document}
The .latexmkrc
:
push @generated_exts, 'tex'; # Remove generated .tex on clean-up
# Use subroutine to do preprocessing and running pdflatex
$pdflatex = 'internal mylatex %B %O';
sub mylatex {
my $base = shift @_;
my $tex = "$base.tex";
# Run the preprocessor
system('lhs2TeX', '--poly', '-o', $tex, "$base.lhs") == 0 or return $?;
# Run pdflatex
return system('pdflatex', @_, $tex);
}
At the command line:
$ latexmk -silent -pdf input.lhs
Latexmk: Run number 1 of rule 'pdflatex'
Latexmk: calling mylatex( input -interaction=batchmode -recorder )
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013)
restricted \write18 enabled.
entering extended mode
This successfully produces an appropriate input.pdf
. But it also required me to hardcode pdflatex
.
A failed attempt using custom dependencies
The .latexmkrc
:
$cleanup_includes_cusdep_generated = 1; # Remove generated .tex on clean-up
# Set preprocessor as a custom dependency
add_cus_dep('lhs', 'tex', 1, 'run_lhs2TeX');
sub run_lhs2TeX {
my $base = shift @_;
# Run the preprocessor
return system('lhs2TeX', '--poly', '-o', "$base.tex", "$base.lhs");
}
The rule created here never fires. I'm guessing this doesn't work because latexmk doesn't use dependencies for the initial inputs, only for files included within other files (e.g. via \input
).
Conclusion
Is there a simple way, possibly similar to the above custom dependency attempt, to configure latexmk to use a preprocessor? It shouldn't require hardcoding the compiler. (I could do this with a Makefile, but if I'm using latexmk anyway, then I would prefer to do everything with latexmk.)
Best Answer
You can make either solution work:
With your first solution (with the internal subroutine to run
lhsTeX
andpdflatex
), arrange to add a line to the.fls
file to informlatexmk
that the.lhs
file is an effective source file for runningpdflatex
. A suitable.latexmkrc
file isI would also delete the line putting
tex
in@generated_exts
. The.tex
file is too important tolatexmk
for it to be useful to treat the.tex
file as generated.With the custom dependency solution:
.latexmkrc
file, remove the line setting$cleanup_includes_cusdep_generated
. This is important. For creation of the custom dependency rule, the output file of the custom dependency needs to exist, so thatlatexmk
knows that there is something to make. So removing the.tex
file preventslatexmk
from discovering the need for a custom dependency.lhs2TeX
manually to create the.tex
file. This primes the process oflatexmk
detecting the dependency between the.lhs
and.tex
files.Better solutions can be made, but the ones I can think of need use of
latexmk
internals or modifications tolatexmk
.