# Latexmk using -jobname and a command line \def

I am having a problem converting this pdflatex command to use latexmk:

pdflatex --file-line-error --shell-escape --synctex=1 \
-jobname=foobar "\def\MyCustomDef{} \input{file-name.tex}"


I attempt to use this with latexmk as:

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


but this results in

   Latexmk: Could not find file [\def\MyCustomDef{} \input{file-name.tex}].
-- Use the -f option to force complete processing.


Removing the double quotes in the above results in:

Latexmk: Need to specify at most one filename if jobname specified,
but 2 were found (after defaults and wildcarding).


My earlier attempt was:

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


but after having figured out the syntax to get pdflatex to work, I am assuming that one must use \input{} to specify the file name when defining macros on the command line.

## Update:

I was able to get the solution form mph working on the command line, but then when I attempt to place that in my script I run into further issues. I suspect these are due to the need to escape things until the right moment that David Carlisle mentioned. Hmmm... This sounds a bit like TeX's expansion related issues -- no wonder I am having trouble. :-)

Here is my shell script :

#!/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 " set PDFLATEX_OPTIONS_WITH_DEF = "$PDFLATEX_OPTIONS %O '\def\MyCustomDef{}\input{%S}'"

set TEX_FILE = "mf2"

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

echo "----- Custom build (with jobname & def):"

## 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. • This seems to work fine on the command line, but I am not able to get it to work if it is in a script. – Peter Grill Jul 23 '12 at 2:07 • @PeterGrill: I’ve updated my answer. – mhp Jul 23 '12 at 11:56 • @PeterGrill: A (subjective) tip of a Bash user: Use Csh only if it’s really required – quoting and escaping might give a hard time. – mhp Jul 23 '12 at 12:23 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 {

• I tried this solution but was not able to figure out the correct number of \  (tried quite a few) to use with a csh script. The solution of @mhp seems cleaner though. – Peter Grill Jul 23 '12 at 2:08
• Hmph if I'd known you were a csh user I wouldn't have helped, next you'll be telling me you use vi not emacs:-) did you avoid all spaces in the argument as well as quoting characters? the other answer suggests the author might have used latexmk before, or read the manual or something, surely that's an unfair advantage. If this cygwin update ever finishes I might have (t)csh and will give it a try didn't even have it installed it seems. – David Carlisle Jul 23 '12 at 8:51
• tcsh version added – David Carlisle Jul 23 '12 at 9:20
• It’s astonishing that this works! Apparently, the Latexmk argument parser is much more capable than it officially claims to be :-) – mhp Jul 23 '12 at 17:33

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.