1

I am trying to export different pictures as PDFs. I have followed the answer here to give a condition on a figure. It works with a single picture/condition, but I need to combine multiple conditions.

mwe.tex:

\documentclass[11pt]{article}                                                    
\usepackage{tikz}                                                                
\usetikzlibrary{external}                                                        
\usepackage{pgfplots}                                                            
                                                                                 
\tikzexternalize                                                                 
                                                                                 
\begin{document}                                                                 
\ifdefined\varOne                                                                
\makeatletter                                                                    
  \tikzset{external/system call={pdflatex \tikzexternalcheckshellescape -halt-on-error
    -interaction=batchmode -jobname "\image" "\string\def\string\varOne{}\texsource"}}
\makeatother                                                                     
I want to include varOne                                                         
\begin{figure}[h]                                                                
    \centering                                                                   
    \begin{tikzpicture}                                                          
        \begin{axis} []                                                          
        \end{axis}                                                               
    \end{tikzpicture}                                                            
\end{figure}                                                                     
\fi                                                                              
\ifdefined\varTwo                                                                
\makeatletter                                                                    
  \tikzset{external/system call={pdflatex \tikzexternalcheckshellescape -halt-on-error
    -interaction=batchmode -jobname "\image" "\string\def\string\varTwo{}\texsource"}}
\makeatother                                                                     
I want to include varTwo with value \varTwo                                      
\begin{figure}[h]                                                                
    \centering                                                                   
    \begin{tikzpicture}                                                          
        \begin{axis} []                                                          
        \end{axis}                                                               
    \end{tikzpicture}                                                            
\end{figure}                                                                     
\fi                                                                              
\end{document}

I can currently decide which figure to extract with:

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

or

 pdflatex -shell-escape "\def\varTwo{2}\input{mwe}"

But I want to be able to handle combinations of multiple conditions e.g.

 pdflatex -shell-escape "\def\varOne{}\def\varTwo{2}\input{mwe}"

Fails. In this case I would expect to generate both figures.

1 Answer 1

2

I don't know exactly why, but having all the required \varXXX defined in /external/system call seems to work.

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

\tikzexternalize

\tikzset{external/system call={%
  pdflatex \tikzexternalcheckshellescape 
    -halt-on-error
    -interaction=batchmode 
    -jobname "\image" 
    "\detokenize\expandafter{\myVars}\texsource"}}

\begin{document}
\ifdefined\varOne
I want to include varOne
\begin{figure}[h]
    \centering
    \begin{tikzpicture}
        \begin{axis} []
        \end{axis}
    \end{tikzpicture}
\end{figure}
\fi

\ifdefined\varTwo
I want to include varTwo with value \varTwo
\begin{figure}[h]
    \centering
    \begin{tikzpicture}
        \begin{axis} []
        \end{axis}
    \end{tikzpicture}
\end{figure}
\fi
\end{document}

Then run with

pdflatex -shell-escape '\def\myVars{\def\varOne{}\def\varTwo{2}}\myVars\input{mwe}'

Alternatively you can put def\varOne{}\def\varTwo{2}}\myVars in a separate file, for example config.tex, then use pdflatex -shell-escape '\input{config}\input{mwe}'.

1
  • How do you come up with stuff like this. I have been looking at the manual for longer than I would like to admit without success. Thanks, it works perfectly. Commented Dec 7, 2021 at 1:37

You must log in to answer this question.

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