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.


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 :


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:"

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


and test file:


    My CustomDef was used.
    My CustomDef was not used.
  • don't you want to lose the double quotes which makes the whole thing a file name called literally "\def\MyCustomDef{} \input{file-name.tex}" – David Carlisle Jul 22 '12 at 1:10
  • @DavidCarlisle: That results in a different error -- see updated question. I had assumed the double quotes were required as they are required by pdflatex. – Peter Grill Jul 22 '12 at 3:36

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).


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


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:'

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


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


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\}"


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

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

> \mydef=macro:
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.

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
  • 1
    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
  • 1
    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


and then running latexmk foobar.tex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.