0

I have this MWE (test.tex):

\documentclass[11pt]{article}
\begin{document}

Text.\footnote{Footnote text.}

\end{document}

I want to compile it this way:

pdflatex -draftmode \
-output-directory=/tmp \
-jobname=footnotecheck \
'\AtBeginDocument{\newcounter{MyFootnote} \NewCommandCopy\MyOldFootnote\footnote \renewcommand{\footnote}[1]{\stepcounter{MyFootnote}\label{FootnoteMark\arabic{MyFootnote}}\MyOldFootnote{\label{FootnoteText\arabic{MyFootnote}}#1}}} \input{test}'

In this way I have this kind of strings in the .aux file (https://tex.stackexchange.com/a/381667/33634):

\newlabel{FootnoteMark1}{{}{1}{}{}{}}
\newlabel{FootnoteText1}{{1}{1}{}{}{}}

I use draftmode because I don't want/need a .pdf file to be produced (and it works).

Now I need to run the command the appropriate number of times. Ho can I prepare a .latexmkrc file for this command? I tryed:

$pdflatex = 'pdflatex -draftmode %O %S \
   "\AtBeginDocument{\newcounter{MyFootnote} \let\MyOldFootnote\footnote \renewcommand{\footnote}[1]{\stepcounter{MyFootnote}\label{FootnoteMark\arabic{MyFootnote}}\MyOldFootnote{\label{FootnoteText\arabic{MyFootnote}}#1}}}" \
   \input %S';

$out_dir = '/tmp'; $jobname = 'footnotecheck';

and the command:

latexmk -pdf test.tex

It works for $out_dir and $jobname but it doesn't redefine the \footnote command, and gives me the warning:

Failure to make 'footnotecheck.pdf'
Latexmk: Errors, so I did not complete making targets
Collected error summary (may duplicate other messages):
  pdflatex: failed to create output file

Latexmk: If appropriate, the -f option can be used to get latexmk to try to force complete processing.

Gabriele
  • 1,815
  • 1
    There are two problems with your definition of $pdflatex: 1. You've got %S; that gets substituted by the source filename, so the command line isn't the one you are expecting: So remove that first %S. 2. Using -draftmode implies that no pdf file is created, and latexmk treats that as an error; put touch %D; at the front of the command line to solve that problem. – John Collins Nov 16 '23 at 19:15
  • A comment about debugging situations with complicated command line specifications like the one here fore $pdflatex: Look at earlier parts of the output from latexmk where it reports the command line(s) it actually passed to the command shell. That way you can check whether it actually matches what you intended. – John Collins Nov 16 '23 at 19:31

2 Answers2

1

Latexmk has a built-in way of putting TeX code on the command line given to *latex, followed by a command to input the desired source file. On the command line to latexmk, you can use the -usepretex option. In a latexmkrc file, you can set the $pre_tex_code variable, provided you also redefine the variables for the command lines for pdflatex etc to use the %P placeholder instead of the %S placeholder.

Basic usage: With the code given by the OP, the simplest command line to latexmk is:

latexmk -pdf -usepretex="\AtBeginDocument{\newcounter{MyFootnote} \let\MyOldFootnote\footnote \renewcommand{\footnote}[1]{\stepcounter{MyFootnote}\label{FootnoteMark\arabic{MyFootnote}}\MyOldFootnote{\label{FootnoteText\arabic{MyFootnote}}#1}}}" test.tex

The -usepretex option does two things: 1. It sets the code to be passed to *latex. 2. It changes the definition of $pdflatex to be pdflatex %O %P, where the %P indicates that the specified code is passed on the command line to pdflatex, followed by the code to read the source file. (The -usepretex option also causes $latex, $lualatex and $xelatex to be set following the same pattern.)

When checking/debugging situations like this, it's a good idea to look at the output from latexmk, where it reports the command line which latexmk uses to invoke pdflatex, and to verify that latexmk is doing what you wanted it to do.

OP's requirements: The OP wanted to use a -jobname argument, an output directory, and to use the -draftmode option to pdflatex to prevent the production of a pdf file. A problem with the last requirement is that latexmk treats a missing pdf file as an error; to satisfy latexmk, one can arrange the command line to make a dummy pdf file. This can be done (for a Unix-like OS) by

latexmk -pdf -usepretex="\AtBeginDocument{\newcounter{MyFootnote} \let\MyOldFootnote\footnote \renewcommand{\footnote}[1]{\stepcounter{MyFootnote}\label{FootnoteMark\arabic{MyFootnote}}\MyOldFootnote{\label{FootnoteText\arabic{MyFootnote}}#1}}}" -pdflatex="touch %D; pdflatex -draftmode %O %P" -output-directory=/tmp -jobname=footnotecheck test.tex

Settings in a latexmkrc file Following the pattern in the latexmk documentation, you can do

$pre_tex_code = '\AtBeginDocument{'.
                '\newcounter{MyFootnote}'.
                '\NewCommandCopy\MyOldFootnote\footnote'.
                '\renewcommand{\footnote}[1]{'.
                    '\stepcounter{MyFootnote}'.
                    '\label{FootnoteMark\arabic{MyFootnote}}'.
                    '\MyOldFootnote{\label{FootnoteText\arabic{MyFootnote}}#1}'.
                    '}'.
                '}';
alt_tex_cmds();

followed by any desired redefinition of the $pdflatex variable. Here latexmk's alt_tex_cmds() sets $pdflatex to be pdflatex %O %P (and similarly for $latex, $lualatex, and $xelatex), so that the given code gets uses on the pdflatex command line. I've used Perl's . operator to concatenate strings, so that the statement specifying the TeX code to be supplied to *latex can be read more easily. I've used single quotes since in Perl that is how one avoids needing to quote the \ characters.

John Collins
  • 11,183
  • 3
  • 32
  • 33
0

I found this solution:

  1. To pass the redefined commands to pdflatex in latexmk I defined a function (in the file footnotecheck.sh):
#!/bin/bash

footnoteCheck() { local options="" local filename=""

if [[ $# -eq 1 ]]; then
    filename="$1"
elif [[ $# -gt 1 ]]; then
    options="${@:1:$(($#-1))}"
    filename="${!#}"
else
    echo "Usage: $0 [options] filename"
    exit 1
fi

pdflatex $options -draft "\AtBeginDocument{
    \newcounter{MyFootnote}
    \NewCommandCopy\MyOldFootnote\footnote
    \renewcommand{\footnote}[1]{
        \stepcounter{MyFootnote}
        \label{FootnoteMark\arabic{MyFootnote}}
        \MyOldFootnote{\label{FootnoteText\arabic{MyFootnote}}#1}
    }
}\input{$filename}"

}

  1. Then I wrote my .latexmkrc file (file footnotecheck.latexmkrc):
$pdflatex = "touch %D && bash -c 'source footnotecheck.sh; footnoteCheck %O  %S'";
$out_dir = '/tmp';
$jobname = 'footnotecheck';
  1. The command:

    latexmk -pdf -r footnotecheck.latexmkrc test.tex

did the work.

Note that I used -draftmode in footnoteCheck command and touch %D, that creates an empty jobname.pdf file to avoid the warning about "failed to create output file".

I don't know if this is the best solution to solve my problem and I don't understand why I need to source my bash function in the .latexmkrc file (it doesn't work otherwise).

Please let me know I did right or if there is a better solution.

Edit. https://tex.stackexchange.com/users/8495/john-collins comment simplify a lot may problem. I can have it all in a single command line:

latexmk -pdf -usepretex="\AtBeginDocument{\newcounter{MyFootnote} \let\MyOldFootnote\footnote \renewcommand{\footnote}[1]{\stepcounter{MyFootnote}\label{FootnoteMark\arabic{MyFootnote}}\MyOldFootnote{\label{FootnoteText\arabic{MyFootnote}}#1}}}" -pdflatex="touch %D; pdflatex -draftmode %O %P" -output-directory=/tmp -jobname=footnotecheck test.tex
Gabriele
  • 1,815
  • 1
  • Getting complex command lines correctly defined in $pdflatex is tricky, so using a script like this is reasonable, even if non-optimal. 2. For a more systematic way of getting code like your \AtBeginDocument... onto the command line, look at the latexmk documentation for the -usepretex option, and for the variable $pre_tex_code. I'd recommend this second method; but you'd have to modify $pdflatex to include the touching of the pdf file (e.g., $pdflatex = 'touch %D; pdflatex -draftmode %O %P').
  • – John Collins Nov 16 '23 at 19:24
  • Oh! This is beautiful! Thanks! I can have it in a single command line: latexmk -pdf -usepretex="\AtBeginDocument{\newcounter{MyFootnote} \let\MyOldFootnote\footnote \renewcommand{\footnote}[1]{\stepcounter{MyFootnote}\label{FootnoteMark\arabic{MyFootnote}}\MyOldFootnote{\label{FootnoteText\arabic{MyFootnote}}#1}}}" -pdflatex="touch %D; pdflatex -draftmode %O %P" -output-directory=/tmp -jobname=footnotecheck test.tex – Gabriele Nov 16 '23 at 21:20
  • @JohnCollins If you like write a the comment as an answer and I will accept it as the solution to my problem. Thank you! – Gabriele Nov 16 '23 at 21:26