[Solved] Tikz external handling optimization

Sunsheep Asks: Tikz external handling optimization
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 the every text and graphic elements. Therefore, other tikzpicture environments are also read and in and processed. I wonder now 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 (it does not compile because I am just trying). Basically I try to dynamicaly comment out the tikz code, 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 of you 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

Code:
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
deftikzsetnextfilename#1{gdeftikzexternal@nextfile{#1}gdefmytikznext{#1}}
makeatother

% New if condition states (ifshowtikztrue / ifshowtikzfalse)
newififshowtikz

% Adapt tikzpicture enviroment and make use of verbatim
letoldtikzpicturetikzpicture
letoldendtikzpictureendtikzpicture

renewenvironment{tikzpicture}{%
    expandafterifstrequalexpandafter{jobname}{mytikznext}{showtikztrue}{showtikzfalse}
    expandafterifstrequalexpandafter{jobname}{main}{showtikztrue}{nope}
    
    ifshowtikzexpandafteroldtikzpicture%
    elseexpandaftercomment%
    fi
}{%
    expandafterifstrequalexpandafter{jobname}{mytikznext}{showtikztrue}{showtikzfalse}
    expandafterifstrequalexpandafter{jobname}{main}{showtikztrue}{nope}
    
    ifshowtikzexpandafteroldendtikzpicture%
    elseexpandafterendcomment%
    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
        newdimenR
        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 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:

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

therefore the if statement does not work…

Edit3: Added the folowing option to external/system call

Code:
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.:

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

New full “MWE”:

Code:
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
deftikzsetnextfilename#1{gdeftikzexternal@nextfile{#1}gdefmytikznext{#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)
newififshowtikz

% Adapt tikzpicture enviroment and make use of verbatim
letoldtikzpicturetikzpicture
letoldendtikzpictureendtikzpicture

renewenvironment{tikzpicture}{%
    typeout{=== JOB: {jobname} ===}
    typeout{=== TIKZ: {mytikznext} ===}
    expandafterifstrequalexpandafter{jobname}{mytikznext}{typeout{=== SAME ===}}{typeout{=== NOTSAME ===}}

    expandafterifstrequalexpandafter{jobname}{mytikznext}{showtikztrue}{showtikzfalse}
    expandafterifstrequalexpandafter{jobname}{main}{showtikztrue}{nope}
    
    ifshowtikzexpandafteroldtikzpicture%
    elseexpandaftercomment%
    fi
}{%
    
    ifshowtikzexpandafteroldendtikzpicture%
    elseexpandafterendcomment%
    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
        newdimenR
        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}

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.