For a quite large document I'm using the external
library from tikz
. However, to annotate an image I tried to include it in a tikzpicture node and then draw on it.
My problem is, that I have defined a new command \includegraphicscoloured
, which replaces the default \includegraphics
, such that it supports an additional file suffix (for me "-monochrome" or "-rgb", defined before in the preamble). Now, when I compile the document, my image does not get included, but I only get the text
[includegraphics optimized away because it does not contribute to exported PDF]
When I use the default \includegraphics
everything is fine. I can also get it to work, when I globally use tikzset{external/optimize=false}
, but that is not really an option, since then every single externalisation takes ages.
I tried putting the external/optimize
key directly at the tikzpicture
environment or simply inside my newly defined macro, but none of this worked out.
Is there a way that I can switch optimisation off for only that single picture? Or are there things I can modify in the tikzexternalize settings that help? I read the docs, but could not find any details on the process.
MWE:
\documentclass{article}
\usepackage{mwe}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[optimize=true]
\def\imageSuffix{-monochrome}
% This line is only here to stop TeXnicCenter from complaining
% that the image #2\tikzExternalSuffix does not exist
\let\latexincludegraphics\includegraphics
\newcommand{\includegraphicscoloured}[2][]{%
% we have to define the filename to test for in a macro,
% otherwise it just fails
\def\testfile{./#2\imageSuffix}%
%
% now test if the picture exists as
% pdf, png, or jpg
\IfFileExists{\testfile.pdf}{%
% pdf version of file exists
\latexincludegraphics[#1]{#2\imageSuffix}%
}{%
\IfFileExists{\testfile.jpg}{%
% jpg version of file exists
\latexincludegraphics[#1]{#2\imageSuffix}%
}{%
\IfFileExists{\testfile.png}{%
% png version of file exists
\latexincludegraphics[#1]{#2\imageSuffix}%
}{%
% no known version of the suffixed image exists
% thus fallback to normal include, which will
% print it's own error when it fails
\latexincludegraphics[#1]{#2}%
}
}
}
}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\node at (0, 0) {\includegraphics[width=0.5\columnwidth]{example-image-a}};
\end{tikzpicture}
\caption{This works as expected.}
\end{figure}
\begin{figure}
\begin{tikzpicture}
\node at (0, 0) {\includegraphicscoloured[width=0.5\columnwidth]{example-image-b}};
\end{tikzpicture}
\caption{This doesn't: ``includegraphics optimized away because it does not contribute to exported PDF''.}
\end{figure}
\end{document}
\let
line to\def\latexincludegraphics{\includegraphics}
will work. – Symbol 1 Dec 6 '16 at 20:00\let
and\def
, which probably helps here, too. Short beginner question, should there be an answer made for this, or would I leave the question open? – Timm Dec 8 '16 at 9:52