\tikzexternalize 在 \ifdefined 中不起作用

\tikzexternalize 在 \ifdefined 中不起作用

我正在尝试将不同的图片导出为 PDF。我已按照答案这里对图形设置条件。它适用于单张图片/条件,但我需要组合多个条件。

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}

我现在可以决定提取哪个图形:

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

或者

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

但我希望能够处理多种条件的组合,例如

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

失败。在这种情况下,我希望生成两个数字。

答案1

我不知道具体原因,但定义所有必需的内容\varXXX似乎/external/system call有效。

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

然后运行

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

或者,您可以放入def\varOne{}\def\varTwo{2}}\myVars一个单独的文件,例如config.tex,然后使用pdflatex -shell-escape '\input{config}\input{mwe}'

相关内容