# \tikzexternalize together with drawing on an image with TikZ

I am following this very good answer that explains how to draw on an image with TikZ. Now I tried to implement this new tikz picture I got with Tikz library external but I get the error

Package tikz Error: Sorry, the system call 'pdflatex -shell-escape -halt-on-e rror -interaction=batchmode -jobname "tikz/myTikzFileName" did NOT result in a usable output file 'tikz/myTikzFileName' (expected one of .pdf:.jpg:.jpeg:.png:).

I have been working for the last time on a project with ~20 figures, all made with tikz and pgfplots, and the externalization commands worked perfect, up until now when I inserted a png file into tikz.

The graphics I want to insert into my document is the file myGraphics.tikz,

% myGraphics.tikz
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.9\textwidth]{some-image.png}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\draw[red,ultra thick,rounded corners] (0.62,0.65) rectangle (0.78,0.75);
\end{scope}
\end{tikzpicture}


implemented with the tikzscale package:

% main.tex
\documentclass[dvipsnames]{beamer}
\usepackage{tikz,tikzscale}
%% To reduce compilation time
\usetikzlibrary{external}
\tikzexternalize[prefix=tikz]

\begin{document}

\begin{frame}{Lorem Ipsum}{dolor sit amet}
Some text
\begin{figure}
\tikzsetnextfilename{myExternalTikzFile}
\includegraphics[height=100pt]{myGraphics}
\end{figure}
\end{frame}

\end{document}


I do get a non empty md5 file, but nothing more.

Do anyone know how to fix this?

(some-image.png is this image)

• works fine for me on a current texlive. Are you sure that you tried with this example? The error message indicates a subfolder, but your prefix has no slash. Jun 8 '21 at 16:46
• @UlrikeFischer You are right, I changed the question.
– tush
Jun 8 '21 at 16:59

I see two possible problems in creating your output file:

• the use of \includegraphics to invoke the .tikz file; it should rather be something like \input (see @sheß answer)
• maybe you don't use --shell-escape option when running the document; you need something like pdflatex -synctex=1 -interaction=nonstopmode --shell-escape main.tex.

I insert the code below, but the important parts of it are borrowed from sheß's answer.

The code

\documentclass[11pt]{article}
\usepackage{tikz, tikzscale}

%% To reduce compilation time
\usetikzlibrary{external}
\tikzexternalize[prefix=tikz]
\newcommand{\inputtikz}[1]{\tikzsetnextfilename{#1}\input{#1.tikz}}

\usepackage{lipsum}
\begin{document}

\lipsum[1]

\begin{figure}[htb!]
\centering
\inputtikz{myGraphics}
\caption{Boletus nubes}
\label{fig:1}
\end{figure}

\lipsum[2]
\end{document}

• Thanks a lot! What about image size specification? can I define it using \input{___.tikz} ?
– tush
Jun 14 '21 at 16:19
• You can introduce a variable, something like \newcommand{myWidth}{0.5} in the main.tex and use it in myGraphics.tikz in \includegraphics[width=\myWidth\textwidth]{mushrooms.png}. But it is not very clear for me how it really works: if the value is changed, there is no effect unless I delete manually the file tikzmyGraphics.pdf that was previously created. At least this is how it works for me. Jun 15 '21 at 5:34