tikzexternalize 与 \def 的问题

tikzexternalize 与 \def 的问题

我正在尝试将一些图保存为单独的 PDF,但我想根据条件决定保存哪些图。

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} 

如果我将其编译为:

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

它失败。

我可以\def\myRandomVariable{}在文件的开头执行此操作,并且它可以毫无问题地运行:

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

并编译为:

pdflatex -shell-escape mwe

我怎样才能实现我想要的?我需要使用编译命令给出条件,\def因为我每次都需要在具有多个不同条件的 bash 脚本中执行此操作。

答案1

以下日志行表明问题是,当external调用额外的 latex 命令时,\myRandomVariable总是未定义的。

===== '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}. ========

配置密钥external/system call似乎有效:

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

相关内容