Problem tikzexternalize with \def

graphicsshell-escapetikz-externaltikz-pgf

I am trying to save some plots as individual PDFs, but I want to decide which ones to save with a condition.

mwe.tex:

\documentclass[11pt]{article}                                                    
\usepackage{tikz}                                                                
\usetikzlibrary{external}                                                        
\usepackage{pgfplots}                                                            
                                                                                 
\tikzexternalize                                                                 
                                                                                                                                    
\begin{document} 
\ifdefined\myRandomVariable                                                                   
\begin{figure}[h]                                                                
    \centering                                                                   
    \begin{tikzpicture}                                                          
        \begin{axis} []                                                          
        \end{axis}                                                               
    \end{tikzpicture}                                                            
\end{figure}                                                                     
\fi                                                                              
\end{document} 

If I compile it as:

pdflatex -shell-escape "\def\myRandomVariable{}\input{mwe}"

It fails.

I can \def\myRandomVariable{} at the beginning of my file and it works without any problems:

\def\myRandomVariable{}                                                          
\documentclass[11pt]{article}                                                    
\usepackage{tikz}                                                                
\usetikzlibrary{external}                                                        
\usepackage{pgfplots}                                                            
                                                                                 
\tikzexternalize                                                                 
                                                                                                                                     
\begin{document} 
\ifdefined\myRandomVariable                                                                  
\begin{figure}[h]                                                                
    \centering                                                                   
    \begin{tikzpicture}                                                          
        \begin{axis} []                                                          
        \end{axis}                                                               
    \end{tikzpicture}                                                            
\end{figure}                                                                     
\fi                                                                              
\end{document}

And compiling as:

pdflatex -shell-escape mwe

How can I achieve what I want? I need to give the condition i.e. \def with the compilation command because I need to do this in a bash script with multiple different conditions every time.

Best Answer

The following log lines indicate the problem is, when external calls extra latex commands, \myRandomVariable is always undefined.

===== 'mode=convert with system call': Invoking 'pdflatex -shell-escape -halt-on-error -interaction=batchmode -jobname "pgfplots-external-sx625091-figure0" "\def\tikzexternalrealjob{pgfplots-external-sx625091}\input{pgfplots-external-sx625091}"' ========
This is pdfTeX, Version 3.141592653-2.6-1.40.23 (TeX Live 2022/dev) (preloaded format=pdflatex)
 \write18 enabled.
entering extended mode
system returned with code 256
===== The last system call resulted in an EMPTY output file. Maybe it is part of \ref. Rescheduling it for \end{document}. ========

Configuring the key external/system call seems to work:

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{external}
\usepackage{pgfplots}
\tikzexternalize

\makeatletter
\ifdefined\myRandomVariable
  \tikzset{external/system call={pdflatex \tikzexternalcheckshellescape -halt-on-error
    -interaction=batchmode -jobname "\image" "\string\def\string\myRandomVariable{}\texsource"}}
\fi
\makeatother

\begin{document}
\ifdefined\myRandomVariable
\begin{figure}[h]
    \centering
    \begin{tikzpicture}
        \begin{axis} []
        \end{axis}
    \end{tikzpicture}
\end{figure}
\fi
\end{document}