Why can't I compile the following even though I have enabled -shell-escape?

\def\paramB{Hello World}

\immediate\write18{pdflatex "\def\paramA{standalone}\input{inputfile}"}

Log File

This is pdfTeX, Version 3.1415926-2.4-1.40.13 (TeX Live 2012/W32TeX)
 \write18 enabled.

<< the unnecessary texts are intentionally removed by me for the sake of simplicity >>

No file executor.aux.
! Undefined control sequence.
<write> pdflatex "\def \paramA 
                               {standalone}\input {inputfile}"
l.13 ...\def\paramA{standalone}\input{inputfile}"}

  • 2
    As the error message says: \paramA is not defined. \write18 tries to expand it before sending it to the shell (which it shouldn’t in this example). It needs therefore to be protected by \noexpand or \string or \unexpanded: \immediate\write18{pdflatex "\def\noexpand\paramA{standalone}\noexpand\input{inputfile}"} or \immediate\write18{\unexpanded{pdflatex "\def\paramA{standalone}\input{inputfile}"}} – Qrrbrbirlbel Feb 13 '13 at 1:50
  • 2
    Or \immediate\write18{pdflatex "\def\string\paramA{standalone}\string\input{inputfile}"} or \immediate\write18{\detokenize{pdflatex "\def\paramA{standalone}\input{inputfile}"}}. The error is caused by the expansion of \immediate\write. – Heiko Oberdiek Feb 13 '13 at 1:56

As the error message describes, the macro \paramA is undefined.

\write18 tries to expand its content before forwarding it to the shell, so you need to “protect” (not \protect!) the macros \paramA and \input with

  • \noexpand (single use),
  • \string (single use),
  • \unexpanded (multiple uses), or
  • \detokenize (multiple uses).

The easiest solution in this case would be to use \unexpanded or \detokenize as you actually have no macros that needs to get expanded:

\immediate\write18{\unexpanded{pdflatex "\def\paramA{standalone}\input{inputfile}"}}

Single macros needs to be protected every time:

\immediate\write18{pdflatex "\def\string\paramA{standalone}\string\input{inputfile}"}

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.