[Tex/LaTex] Configuring latexmk to use a preprocessor (lhs2TeX)

latexmklhs2tex

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:

  1. With your first solution (with the internal subroutine to run lhsTeX and pdflatex), arrange to add a line to the .fls file to inform latexmk that the .lhs file is an effective source file for running pdflatex. A suitable .latexmkrc file is

    # 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
      my $return = system('pdflatex', @_, $tex);
      system "echo INPUT $base.lhs >> $aux_dir1$base.fls";
      return $return;
    }
    

    I would also delete the line putting tex in @generated_exts. The .tex file is too important to latexmk for it to be useful to treat the .tex file as generated.

  2. With the custom dependency solution:

    1. In the .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 that latexmk knows that there is something to make. So removing the .tex file prevents latexmk from discovering the need for a custom dependency.
    2. Before your first run, apply lhs2TeX manually to create the .tex file. This primes the process of latexmk 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 to latexmk.

Related Question