Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{inputfile.tex}
%\def\paramA{standalone}
\def\paramB{Hello World}
\documentclass{\paramA}
\begin{document}
\paramB
\end{document}
\end{filecontents*}

\begin{document}
\immediate\write18{pdflatex "\def\paramA{standalone}\input{inputfile}"}
\immediate\write18{inputfile.pdf}
done!
\end{document}

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

? 
share|improve this question
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

1 Answer 1

up vote 8 down vote accepted

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}"}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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