In an earlier question (Automatic detection of Overleaf for fallback images) I wanted to automatically detect if I am on Overleaf or locally to choose fallback images when on Overleaf, or to re-compile locally.
The initial approach was done using the following code:
\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}
Nevertheless, doing that will lead to quite a lot of code, and make it easy to forget something. Therefore, I decided to redefine includegraphics
with
\renewcommand{\includegraphics}[2]{\ifdefined\TestFigures%
\includegraphics[#1]{example-image-a}
\else
\includegraphics[#1]{#2}
\fi%
}
and use the following code in the main 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}
This fails with ! TeX capacity exceeded, sorry [input stack size=5000].
, presumably due to a definition loop. Therefore, I tried to avoid it by changing the initial definition of includegraphics
to
\LetLtxMacro{\includelateximages}{\includegraphics}
\renewcommand{\includegraphics}[2]{\ifdefined\TestFigures%
\includelateximages[#1]{example-image-a}
\else
\includelateximages[#1]{#2}
\fi%
}
but this fails with
! Package xkeyval Error: `[' undefined in families `Gin'.
What exactly is going wrong here, and how can I simplify my initial approach else?
Now, as suggested in the comments, I included optional arguments (instead of only mandatory arguments) and renamed \includegraphics
to check other error sources, resulting in the following code for the main file:
\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}
For the case of the first sub-figure I do not get any error messages, but neither does it change when defining or un-defining the key TestFigures
. For the case of the second sub-figure it works fine if I enable TestFigures
, but fails with LaTeX Error: Unknown graphics extension: .tikz.
if I disable it, indicating that I might be messing with the changes done in tikzscale
(which redefines includegraphics
, too). Are there solutions to that?
\let\oldincludegraphics\includegraphics
to save the original version of the command. Then redefine it like\renewcommand\includegraphics{...\oldincludegraphics...}
because otherwise you will have an "endless" recursion (well it ends when you run out of memory).\let
would not work here that is why the OP used\LetLtxMacro
or in a current latex format you could use\CopyCommand
tikzscale
which are giving me some headache