I want to convert each EPS image imported from within the input file to a PDF version and save it with its original name separately. The code below simulates the real condition in which we don't want to make heavy modification. So I have to renew the definition of \includegraphics by appending the conversion process at the end.

The conversion make use of an external template (template.tex) that is created on the fly when compiling the inputfilename.tex. For each invocation of \includegraphics in inputfilename.tex,

\immediate\write18{latex -jobname=#2 \unexpanded{"\def\varone{#1} \def\vartwo{#2} \input{template}"} && dvips #2 && ps2pdf -dAutoRotatePages=/None #2.ps}

will be invoked. It uses the template template.tex and gets two parameters \varone and \vartwo from #1 and #2 of \includegraphics.

However, the following code does not compile with errors that you can see by yourself. The code must be compiled with pdflatex -shell-escape inputfilename.

% inputfilename.tex 



    %\immediate\write18{latex -jobname=#2 "\def\noexpand\varone{#1} \def\noexpand\vartwo{#2} \noexpand\input{template}" && dvips #2 && ps2pdf -dAutoRotatePages=/None #2.ps}%
    \immediate\write18{latex -jobname=#2 \unexpanded{"\def\varone{#1} \def\vartwo{#2} \input{template}"} && dvips #2 && ps2pdf -dAutoRotatePages=/None #2.ps}%

A \ldots

B \ldots

C \ldots


How to solve it?

1 Answer 1


keyval goes to some effort not to expand macros in the argument list so



  • Or, more briefly, \expandafter\includegraphics\expandafter[\varone]{\vartwo} since the file name is expanded.
    – egreg
    Nov 16, 2013 at 20:24
  • @egreg, actually mine's more brief, as \z is shorter than \expandafter Nov 16, 2013 at 20:25
  • Mine has less tokens. :P
    – egreg
    Nov 16, 2013 at 20:26
  • @egreg I know:-) Nov 16, 2013 at 20:26

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.