[Tex/LaTex] Shell escape after PDF is finished and closed

compilinghooksshell-escape

I like to escape to the shell using \write18 at the very end of the compilation after the PDF file got completely written and was closed, so that I can use it as part the shell command. Is this somehow possible or does TeX simply closed the PDF at the very last thing? Placing it direct before \end{document} or in \AtEndDocument doesn't help (unsurprisingly).
AFAIK \writes are actually whatsit objects and would be normally be part of the document. So I don't have much hopes.

Background

This is used for automatic PDF-to-PNG conversion as a new feature of standalone. At the moment I do this in the preamble in the second run when the old PDF did not yet got opened. This works fine, but I would like to avoid the need to compile standalone diagrams twice.

Best Answer

You do something like this (based on an idea of Ulrich Diez):

\documentclass{article}

\ifx\conditionmacro\undefined
  \immediate\write18{%
    pdfLaTeX --jobname="\jobname"
    \gdef\string\conditionmacro{1}\string\input\space\jobname
  }%
  \immediate\write18{%
    LaTeX --jobname="\jobname"
    \gdef\string\conditionmacro{1}\string\input\space\jobname
  }%
   \immediate\write18{dvips \jobname}
   \expandafter\stop
\fi

\begin{document}
abc
\end{document}

I'm not quite sure if it a very good idea to use \jobname in the subprocesses, it seems to work but \jobname1 or something like this is perhaps safer.

Btw: I'm one of uncool kids who uses windows.

Related Question