2

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.

2
  • I separate the \def and the input in my scripts and that seems to work for me, but not sure if that will resolve your issue. So, try pdflatex -shell-escape "\def\myRandomVariable{}" "\input{mwe}" Commented Dec 6, 2021 at 6:26
  • @PeterGrill It didn't work for me. Does it work for you with my MWE? Commented Dec 6, 2021 at 6:35

1 Answer 1

2

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}
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .