1

Since I have many and elaborate diagrams (PGFPlots) as well as Tikz images in my document, I have come across a limitation of Tikzexternalize. It takes a long time to compile the document if I make changes to more than two tikzpictures at a time -- at least 15 minutes.

This is because tikzexternalize reads the whole document and tries to typeset every text and graphic element. Therefore, other tikzpicture environments are also read and processed unnecessary. I wonder, if this can be optimised by a simple query before reading in the tikzpicture environment using \tikzexternal@nextfile.

Take a look at my first try (MWE - does not compile). Basically I try to dynamicaly comment out the tikz code using a verbatim environment when the jobname is not equal to the tikzexternal name. Therefore I want to skip all other tikzpictures while creating a specific external tikzpicture. Maybe someone know a better solution or can use and optimize my mwe.

Thank you in advise!

You need to create the folder "tikz" inside the latex project -- or adapt the prefix. Also compile with --shell-escape option

\documentclass[
    titlepage=true,
    fontsize=11pt,
]{article}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{external}
\tikzexternalize[mode=convert with system call,optimize=false]
\usepackage{verbatim}
\usepackage{etoolbox} % for ifstrequal


% First adapt tikzexternal@nextfile and define valso mytikznext
\makeatletter
\def\tikzsetnextfilename#1{\gdef\tikzexternal@nextfile{#1}\gdef\mytikznext{#1}}
\makeatother

% New if condition states (ifshowtikztrue / ifshowtikzfalse)
\newif\ifshowtikz

% Adapt tikzpicture enviroment and make use of verbatim
\let\oldtikzpicture\tikzpicture
\let\oldendtikzpicture\endtikzpicture

\renewenvironment{tikzpicture}{%
    \expandafter\ifstrequal\expandafter{\jobname}{\mytikznext}{\showtikztrue}{\showtikzfalse}
    \expandafter\ifstrequal\expandafter{\jobname}{main}{\showtikztrue}{nope}
    
    \ifshowtikz\expandafter\oldtikzpicture%
    \else\expandafter\comment%
    \fi
}{%
    \expandafter\ifstrequal\expandafter{\jobname}{\mytikznext}{\showtikztrue}{\showtikzfalse}
    \expandafter\ifstrequal\expandafter{\jobname}{main}{\showtikztrue}{nope}
    
    \ifshowtikz\expandafter\oldendtikzpicture%
    \else\expandafter\endcomment%
    \fi
}

\begin{document}
\typeout{=== JOB: {\jobname} ===}
\begin{figure}[!t]
    \centering
    \tikzsetnextfilename{test_1}
    \typeout{=== TIKZ: {\mytikznext} ===}
    \tikzset{partial ellipse/.style args =
  {#1:#2:#3}{insert path={+ (#1:#3) arc (#1:#2:#3)}}}
        \begin{tikzpicture}[>=latex]
        %  ellipses
        \draw [fill=white!90!red]    (3,-1.8) ellipse    (4cm and 1 cm);
        \draw [fill=yellow!90!green] (3,-1.8) ellipse (3cm and 0.75 cm);
        \draw [fill=white!90!green]  (3,-1.8) ellipse  (2cm and 0.5 cm);

        % -- Soleil
        \shade [ball color=gray!10!yellow] (3,-1.8) circle (1);
        \node (soleil) at (3,-1.8) {\bf Soleil};
        % partial ellipse pour tracé devant le Soleil
        \draw (3,-1.8) [partial ellipse=220:320:2cm and 0.5cm]
                (3,-1.8) [partial ellipse=220:320:3cm and 0.75cm];

        % Venus
        \shade [ball color=gray!10!orange] (1.6,-1.8) circle (.2);
        \node (venus) at (1.5,-1.45) {Venus}; 

        % ombre de Venus
        \draw[color=white!70!black,fill=white!70!black]
            (1.6,-2.3) ellipse (2mm and 0.5mm);

        % Mercure
        \shade [ball color=gray!10!orange] (5,-1.225) circle (.25);
        \node (mercure) at (5,-0.8) {Mercure}; 

        % Earth
        \shade [ball color=white!50!blue] (5.75,-2.5) circle (.33);
        \node (terre) at (6.6,-2.6) {\bf Terre};

        % Lune
        \shade [ball color=yellow] (5.25,-2.8) circle (.1);
        \node (lune) at (5.25,-3) {Lune};
            
        % Mars
        \draw (3,-1.8) [partial ellipse=45:120:9cm and 2.5cm];
        \shade [ball color=black!50!red] (5,0.66) circle (.15);
        \node (mars) at (5,1) {\bf Mars};   
        % trajet
        \draw [line width=2pt,blue,->,>=latex] (terre) to[out=0,in=0] (mars);   
    \end{tikzpicture}
    \caption[Test1]{Test1.}
    \label{fig:test_1}
\end{figure}

\begin{figure}[!t]
    \centering
    \tikzsetnextfilename{test_2}
    \begin{tikzpicture}[scale=4,cap=round,>=latex]
        % Radius of regular polygons
        \newdimen\R
        \R=0.8cm
        \coordinate (center) at (0,0);
        \draw (0:\R)
            \foreach \x in {60,120,...,360} {  -- (\x:\R) }
                    -- cycle (300:\R) node[below] {$\csc \theta$}
                    -- cycle (240:\R) node[below] {$\sec \theta$}
                    -- cycle (180:\R) node[left] {$\tan \theta$}
                    -- cycle (120:\R) node[above] {$\sin \theta$}
                    -- cycle (60:\R) node[above] {$\cos \theta$}
                    -- cycle (0:\R) node[right] {$\cot \theta$};
        \draw { (60:\R) -- (120:\R) -- (center) -- (60:\R) } [fill=gray];
        \draw { (180:\R) -- (240:\R) -- (center) -- (180:\R) } [fill=gray];
        \draw { (0:\R) -- (300:\R) -- (center) -- (0:\R) }  [fill=gray];
        \R=0.1cm
        \draw (0:\R) \foreach \x in {60,120,...,360} { -- (\x:\R) }
            [fill=white] -- cycle (center) node {1};
    \end{tikzpicture}
    \caption[Test1]{Test2.}
    \label{fig:test_1}
\end{figure}
\end{document}


Examples taken from https://texample.net -- credits to the authors.

Edit: I compile with lualatex -shell-escape -synctex=1 -interaction=nonstopmode main.tex

Edit2: You have to compile two times, then it is working..... But not as indented: When compiling the tikzpicture in a separate call it sill says:

=== JOB: {main} ===
=== TIKZ: {test_1} ===

therefore the if statement does not work...

Edit3: Added the folowing option to external/system call

\makeatletter
    \tikzset{%
        external/prefix=tikz/,
        external/system call={%
            lualatex %
                \tikzexternalcheckshellescape %
                -shell-escape %
                -interaction=nonstopmode%
                -interaction=batchmode %
                %-output-directory="tikz" %
                -jobname "\mytikznext" %
                "\texsource"%
        },
        %/pgf/images/include external/.code={%
        %    \includegraphics{tikz/#1}%
        %},%
    }
\makeatother

But the output is still not correct, respectively the comparison of the strings does not work as intended.:

=== JOB: {test1} ===
=== TIKZ: {test1} ===
=== NOTSAME === <---------- WHY?
=== JOB: {test1} ===
=== TIKZ: {test2} ===
=== NOTSAME ===

New full "MWE":

\documentclass[
    fontsize=11pt,
]{article}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{external}
\tikzexternalize%[prefix=tikz/,mode=convert with system call,optimize=false]
\usepackage{verbatim}
\usepackage{etoolbox} % for ifstrequal


% First adapt tikzexternal@nextfile and define valso mytikznext
\makeatletter
\def\tikzsetnextfilename#1{\gdef\tikzexternal@nextfile{#1}\gdef\mytikznext{#1}}
\makeatother

\makeatletter
    \tikzset{%
        external/prefix=tikz/,
        external/system call={%
            lualatex %
                \tikzexternalcheckshellescape %
                -shell-escape %
                -interaction=nonstopmode%
                -interaction=batchmode %
                %-output-directory="tikz" %
                -jobname "\mytikznext" %
                "\texsource"%
        },
        %/pgf/images/include external/.code={%
        %    \includegraphics{tikz/#1}%
        %},%
    }
\makeatother

% New if condition states (ifshowtikztrue / ifshowtikzfalse)
\newif\ifshowtikz

% Adapt tikzpicture enviroment and make use of verbatim
\let\oldtikzpicture\tikzpicture
\let\oldendtikzpicture\endtikzpicture

\renewenvironment{tikzpicture}{%
    \typeout{=== JOB: {\jobname} ===}
    \typeout{=== TIKZ: {\mytikznext} ===}
    \expandafter\ifstrequal\expandafter{\jobname}{\mytikznext}{\typeout{=== SAME ===}}{\typeout{=== NOTSAME ===}}

    \expandafter\ifstrequal\expandafter{\jobname}{\mytikznext}{\showtikztrue}{\showtikzfalse}
    \expandafter\ifstrequal\expandafter{\jobname}{main}{\showtikztrue}{nope}
    
    \ifshowtikz\expandafter\oldtikzpicture%
    \else\expandafter\comment%
    \fi
}{%
    
    \ifshowtikz\expandafter\oldendtikzpicture%
    \else\expandafter\endcomment%
    \fi
}

\begin{document}
\begin{figure}[!t]
    \centering
    \tikzsetnextfilename{test1}
    \tikzset{partial ellipse/.style args =
  {#1:#2:#3}{insert path={+ (#1:#3) arc (#1:#2:#3)}}}
        \begin{tikzpicture}[>=latex]
        %  ellipses
        \draw [fill=white!90!red]    (3,-1.8) ellipse    (4cm and 1 cm);
        \draw [fill=yellow!90!green] (3,-1.8) ellipse (3cm and 0.75 cm);
        \draw [fill=white!90!green]  (3,-1.8) ellipse  (2cm and 0.5 cm);

        % -- Soleil
        \shade [ball color=gray!10!yellow] (3,-1.8) circle (1);
        \node (soleil) at (3,-1.8) {\bf Soleil};
        % partial ellipse pour tracé devant le Soleil
        \draw (3,-1.8) [partial ellipse=220:320:2cm and 0.5cm]
                (3,-1.8) [partial ellipse=220:320:3cm and 0.75cm];

        % Venus
        \shade [ball color=gray!10!orange] (1.6,-1.8) circle (.2);
        \node (venus) at (1.5,-1.45) {Venus}; 

        % ombre de Venus
        \draw[color=white!70!black,fill=white!70!black]
            (1.6,-2.3) ellipse (2mm and 0.5mm);

        % Mercure
        \shade [ball color=gray!10!orange] (5,-1.225) circle (.25);
        \node (mercure) at (5,-0.8) {Mercure}; 

        % Earth
        \shade [ball color=white!50!blue] (5.75,-2.5) circle (.33);
        \node (terre) at (6.6,-2.6) {\bf Terre};

        % Lune
        \shade [ball color=yellow] (5.25,-2.8) circle (.1);
        \node (lune) at (5.25,-3) {Lune};
            
        % Mars
        \draw (3,-1.8) [partial ellipse=45:120:9cm and 2.5cm];
        \shade [ball color=black!50!red] (5,0.66) circle (.15);
        \node (mars) at (5,1) {\bf Mars};   
        % trajet
        \draw [line width=2pt,blue,->,>=latex] (terre) to[out=0,in=0] (mars);   
    \end{tikzpicture}
    \caption[Test1]{Test1.}
    \label{fig:test_1}
\end{figure}

\begin{figure}[!t]
    \centering
    \tikzsetnextfilename{test2}
    \begin{tikzpicture}[scale=4,cap=round,>=latex]
        % Radius of regular polygons
        \newdimen\R
        \R=0.8cm
        \coordinate (center) at (0,0);
        \draw (0:\R)
            \foreach \x in {60,120,...,360} {  -- (\x:\R) }
                    -- cycle (300:\R) node[below] {$\csc \theta$}
                    -- cycle (240:\R) node[below] {$\sec \theta$}
                    -- cycle (180:\R) node[left] {$\tan \theta$}
                    -- cycle (120:\R) node[above] {$\sin \theta$}
                    -- cycle (60:\R) node[above] {$\cos \theta$}
                    -- cycle (0:\R) node[right] {$\cot \theta$};
        \draw { (60:\R) -- (120:\R) -- (center) -- (60:\R) } [fill=gray];
        \draw { (180:\R) -- (240:\R) -- (center) -- (180:\R) } [fill=gray];
        \draw { (0:\R) -- (300:\R) -- (center) -- (0:\R) }  [fill=gray];
        \R=0.1cm
        \draw (0:\R) \foreach \x in {60,120,...,360} { -- (\x:\R) }
            [fill=white] -- cycle (center) node {1};
    \end{tikzpicture}
    \caption[Test1]{Test2.}
    \label{fig:test_2}
\end{figure}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.