# How to make pdfLaTeX multithreaded when --shell-escape is enabled?

I am using \write18 enabled (or --shell-escape) with externalization to compile a bunch of PGFplots. The first run takes much time, and I noticed that pdflatex only runs on one CPU core. Is there any way to have pdflatex use multiple cores? (at least when \write18 is enabled)

-
Since you explicitly mentioned the use of external: you can configure the external library to run in mode=list and make. The resulting makefile can be processed in parallel using make -j 8 -f <file>.makefile (in this case on 8 threads). Only feasible if you can resort to make, though... –  Christian Feuersänger Oct 6 '13 at 14:12
Note that the keyword "external library" means that this question is no duplicate. –  Christian Feuersänger Oct 6 '13 at 14:13
What OS are you using? At least on UNIX-like systems it may be enough to end the command line passed to \write18 with an ampersand character (&) to have the shell spawn a new process for the external command. –  Daniel Nov 21 '13 at 13:20
Could you give a minimal working example (MWE) that illustrates your problem, in particular, allows us to experiment with your method of "compiling a bunch of PGFplots"? –  Stephan Lehmke Nov 26 '13 at 13:45
@StephanLehmke your feature request with asynchronous processing sounds interesting; but I fear that it would easily run out-of-control unless you restrict the number of background processes. And that, in turn, sounds like a very complicated method in TeX ... compiling twice would be simple. If you want to experiment with the code; feel free to do so. –  Christian Feuersänger Dec 25 '13 at 14:30

The externalize in tikz may generated individual picture for every \begin{tikzpicture}...\end{tikzpicture}.

• \usepgfplotslibrary{external} let all pgf-plots outside of the compile.

Let pdflatex know those pictures are compiled with make.

• \tikzexternalize[mode=list and make] let tikz to know we would use the make. Because at present only the make support multi-thread.

I'm lazy to set the name for every pgfplots figure, so I let it done by system.

• external/system call set names for individual picture file, or the pdflatex doesn't know what file name should be set. (Origin code is from package tikz&pgf Manual for Version 2.10 page 345.)

My tex file is named test.tex

    \documentclass{standalone}

\usepackage{pgfplots} %use tikz based pgfplots
\usepgfplotslibrary{external}
\tikzexternalize[mode=list and make]
\tikzset{external/system call={pdflatex \tikzexternalcheckshellescape
-halt-on-error -interaction=batchmode -jobname "\image" "\texsource"}
} % to let pdflatex work
%% compile picture: pdflatex --shell-escape xxxxxxx.tex

\begin{document}

\begin{tikzpicture}
\begin{axis}[ xlabel=$x$, ylabel={$f(x) = x^2 - x +4$} ]
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{loglogaxis}[xlabel=Cost,ylabel=Gain]
{ (10,100) (20,150) (40,225) (80,340) (160,510) (320,765) (640,1150) };
\end{loglogaxis}
\end{tikzpicture}

\end{document}


After first pdflatex compiling, do the make -j 4 -f test.makefile. 4 compiles the pictures with 4 thread.

Then run pdflatex second time.

The option --shell-escape also work fine in my system.

My system is:

1. Win 7(x64) + Miktex 2.9 + make under cygwin environment
2. Linux(x64) + Texlive2013 + make
-
Interesting, but could you elaborate what you are doing here? What is the sense of tikzexternalize ? –  Keks Dose Dec 25 '13 at 11:38
I try explain the option I used, But it looks a bit weird in style. –  selwyndd21 Dec 26 '13 at 6:15