在先前的一个问题中(自动检测 Overleaf 以获取后备图像)我想自动检测我是在 Overleaf 上还是在本地,以便在 Overleaf 上选择后备图像,或者在本地重新编译。
最初的方法是使用以下代码完成的:
\begin{figure}[htpb]
\centering
\begin{subfigure}[t]{.5\linewidth}
\ifdefined\TestFigures
\includegraphics[width=0.9\linewidth]{example-image-a}
\else
\includegraphics[width=0.9\linewidth]{example-image-b}
\fi
\caption{Image I}
\end{subfigure}\hfill%
\begin{subfigure}[t]{.5\linewidth}
\centering
\ifdefined\TestFigures
\includegraphics[width=0.5\linewidth]{example-image-a}
\else
\includegraphics[width=0.5\linewidth]{example-image-b}
\fi
\caption{Image II}
\end{subfigure}%
\end{figure}
然而,这样做会导致相当多的代码,并且很容易忘记一些东西。因此,我决定includegraphics
用
\renewcommand{\includegraphics}[2]{\ifdefined\TestFigures%
\includegraphics[#1]{example-image-a}
\else
\includegraphics[#1]{#2}
\fi%
}
并在主文档中使用以下代码:
\begin{figure}[htpb]
\centering
\begin{subfigure}[t]{.5\linewidth}
\includegraphics[width=0.9\linewidth]{sub.tikz}
\caption{Image I}
\end{subfigure}\hfill%
\begin{subfigure}[t]{.5\linewidth}
\centering
\includegraphics[width=0.5\linewidth]{sub.tikz}
\caption{Image II}
\end{subfigure}%
\end{figure}
失败,可能是由于定义循环。因此,我尝试通过将! TeX capacity exceeded, sorry [input stack size=5000].
的初始定义更改为来避免这种情况includegraphics
\LetLtxMacro{\includelateximages}{\includegraphics}
\renewcommand{\includegraphics}[2]{\ifdefined\TestFigures%
\includelateximages[#1]{example-image-a}
\else
\includelateximages[#1]{#2}
\fi%
}
但这失败了
! Package xkeyval Error: `[' undefined in families `Gin'.
这里到底出了什么问题?我该如何简化我最初的方法?
现在,正如评论中所建议的,我包含了可选参数(而不仅仅是强制参数)并重命名\includegraphics
以检查其他错误源,从而导致主文件的代码如下:
\documentclass{article}
\usepackage{standalone}
\usepackage{tikz}
\usetikzlibrary{external}
\usepackage{letltxmacro}
\usepackage{xparse}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\usepackage{tikzscale}
\usepackage{siunitx}
\usepackage{subcaption}
\tikzexternalize[prefix=tikz-cache/]
\tikzset{external/force remake}
\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{compat=1.17}
\pgfplotsset{every axis/.append style={
label style={font=\Large\bfseries},
tick label style={font=\Large},
legend style={font=\Large}
},
y axis/.append style={align=center}}
\tikzset{Line Label/.style={font=\Large,scale=2}}
\newcommand{\figurefontsize}{\Large}
\newcommand{\TestFigures}{1}
\LetLtxMacro{\includegraphicsorig}{\includegraphics}
\RenewDocumentCommand{\includegraphics}{O{} m }{%
\ifdefined\TestFigures%
\includegraphicsorig[#1]{example-image-a}%
\else%
\includegraphicsorig[#1]{example-image-b}%
\fi%
}
\NewDocumentCommand{\includeOLgraphics}{O{} m }{%
\ifdefined\TestFigures%
\includegraphicsorig[#1]{example-image-a}%
\else%
\includegraphicsorig[#1]{#2}%
\fi%
}
\begin{filecontents}{file1.dat}
x y
0 0
1 1
2 2
3 3
4 4
5 5
\end{filecontents}
\begin{document}
\begin{figure}[htpb]
\centering
\begin{subfigure}[t]{.5\linewidth}
\includegraphics[width=0.9\linewidth]{sub.tikz}
\caption{Image I}
\end{subfigure}\hfill%
\begin{subfigure}[t]{.5\linewidth}
\centering
\includegraphics[width=0.5\linewidth]{sub.tikz}
\caption{Image II}
\end{subfigure}%
\end{figure}
\begin{figure}[htpb]
\centering
\begin{subfigure}[t]{.5\linewidth}
\includeOLgraphics[width=0.9\linewidth]{sub.tikz}
\caption{Image I}
\end{subfigure}\hfill%
\begin{subfigure}[t]{.5\linewidth}
\centering
\includeOLgraphics[width=0.5\linewidth]{sub.tikz}
\caption{Image II}
\end{subfigure}%
\end{figure}
\end{document}
对于第一个子图的情况,我没有收到任何错误消息,但在定义或取消定义键时它也没有改变TestFigures
。对于第二个子图的情况,如果我启用它,它会正常工作TestFigures
,但如果我禁用它,它会失败LaTeX Error: Unknown graphics extension: .tikz.
,这表明我可能弄乱了所做的更改tikzscale
(它includegraphics
也重新定义了)。有解决方案吗?
答案1
经过进一步的研究,我找到了一个解决方案,通过将重新定义延迟到文档的开头:
\AtBeginDocument{%
\LetLtxMacro{\includegraphicsorig}{\includegraphics}
\RenewDocumentCommand{\includegraphics}{O{} m }{%
\ifdefined\TestFigures%
\includegraphicsorig[#1]{example-image-a}%
\else%
\includegraphicsorig[#1]{#2}%
\fi%
}%
}
现在它可以按预期工作,如果\TestFigures
定义了,则使用示例图形,如果没有,则加载包含的图形。