Latexmk using -jobname and a command line \def

Pass a proper Latexmk rule as the argument of the -pdflatex option. Thus:

latexmk -pdf -silent    \
        -jobname=foobar \
        -pdflatex="/usr/texbin/pdflatex --file-line-error --shell-escape --synctex=1 %O '\def\MyCustomDef{}\input{%S}'" file-name.tex

Alternatively, instead of using the -pdflatex option, you could set the $pdflatex variable in one of the Latexmk initialization files (e.g. ./latexmkrc or ./.latexmkrc).

Update:

A working Csh script similar to the one used by the OP:

#!/bin/csh

set LATEX_MAKE                = latexmk
set PDFLATEX                  = pdflatex

set LATEX_MAKE_OPTIONS        = '-f -silent '
set PDFLATEX_OPTIONS          = '-pdf -pdflatex="'"$PDFLATEX --file-line-error --shell-escape --synctex=1 %O %S"'"'
set PDFLATEX_OPTIONS_WITH_DEF = '-pdf -pdflatex="'"$PDFLATEX --file-line-error --shell-escape --synctex=1 %O '\def\MyCustomDef {} \input { %S }'"'"'

set TEX_FILE                  = 'mf2'

echo '----- Normal build:'
eval $LATEX_MAKE $LATEX_MAKE_OPTIONS $PDFLATEX_OPTIONS $TEX_FILE.tex

echo '----- Custom build (with jobname & def):'
eval $LATEX_MAKE -jobname=$TEX_FILE-def $LATEX_MAKE_OPTIONS $PDFLATEX_OPTIONS_WITH_DEF $TEX_FILE.tex

exit(0)

Support for nested quotes is rather limited in Csh. Moreover, it’s not easy to escape braces – you have to surround them with spaces.


Given this file

\show\mydef
\documentclass{article}
\begin{document}
hello
\end{document}

The following works for me using bash shell in cygwin as my command line. Note the command line is interpreted for { and \ in different ways in different shells and operating systems, so basically start with the simplest thing and add \\ until it stops complaining.

latexmk -f  -jobname=mf2 "\\\\def\\\\mydef\{a\}\\\\input\{mf2\}"

produces

Latexmk: All targets (mf2.dvi) are up-to-date

but if I remove mf2.dvi and do it again I get

> \mydef=macro:
->a.
l.1 \show\mydef

which shows it worked.

The command line is first parsed by the shell and then by latexmk and then by pdftex itself, and you have to make the \ and { pass through them all. Also you need the -f option to latexmk to force it to do something and stop worrying so much.


using tcsh you need a different quoting order

 $ latexmk -f -jobname=mf2 "\\def\\mydef\{a\}\\input\{mf2\}" 

The easiest way to see what you need in any particular shell is to let latexmk do whatever ot does and then look in the TeX log file to see what commandline TeX actually saw

If the log shows

 restricted \write18 enabled.
 %&-line parsing enabled.
**\def\mydefa\inputmf2

You need some more \ to preserve the {


You can avoid shell backslash quoting hell and the need to specify -jobname by creating a file called foobar.tex containing your code

\def\MyCustomDef{} 
\input{file-name.tex}

and then running latexmk foobar.tex.

Tags:

Latexmk