1

I need to externalize my TikZ pictures to .png files and include those into my final document. In fact, I'll generate two documents, one with vector graphics and one with bitmaps. When searching the web for a method to automate the generation of bitmaps, I found this solution that is even documented in the PGF/TikZ documentation. (Page 618 in the documentation of version 3.0.1a)

The requirement for this method is a running installation of ImageMagick.

\tikzset{
    % Defines a custom style which generates BOTH, .pdf and .png export
    % but prefers the .png on inclusion.
    %
    % This style is not pre-defined, you may need to copy-paste and
    % adjust it.
    png export/.style={
        external/system call/.add=
            {}
            {; convert -density 300 -transparent white "\image.pdf" "\image.png"},
        %
        /pgf/images/external info,
        /pgf/images/include external/.code={%
            \includegraphics
                [width=\pgfexternalwidth,height=\pgfexternalheight]
                {##1.png}%
        },
    }
}

To activate the script automatically to apply this to the whole document, one can add png export to the \tikzset command.

I did recompile my document, having this code in my preamble and waited for some time, then I was flooded with error messages. This error message occurs for each graph.

[8] (./img/graphs/foobar.tex ===== 'mode=convert with system call': Invoking 'xelatex -shell-escape -halt-on-error -interaction=batchmode -jobname "img/build/foobar" "\def\tikzexternalrealjob{main}\input{main}"; convert -density 300 "img/build/foobar.pdf" "img/build/foobar.png"' ======== \openout5 = `main.auxlock'.

runsystem(xelatex -shell-escape -halt-on-error -interaction=batchmode -jobname "img/build/foobar" "\def\tikzexternalrealjob{main}\input{main}"; convert -density 300 "img/build/foobar.pdf" "img/build/foobar.png")...executed.

\openout5 = `main.auxlock'.

LaTeX Warning: File `.png' not found on input line 66.

./img/graphs/foobar.tex:66: Unable to load picture or PDF file '.png'.

<to be read again>

          }

l.66 \end{tikzpicture}

The requested image couldn't be read because it was not a recognized image format.

./img/graphs/foobar.tex:66: Package graphics Error: Division by 0.

Note that both, the .png and the .pdf, are generated correctly and are in the ./img/graphs directory. I also checked the .dpth files. These do all include the width and height of the respective image.

Why is LaTeX, or graphics complaining about not finding a .png image if it's definitely there?

Any ideas are greatly appreciated.


M(N)WE

\documentclass[11pt]{article}

% %%%%%%%%
% Preamble
% %%%%%%%%

% Drawing Packages
\usepackage{graphicx, tikz, pgfplots}

% pgfplots
\pgfplotsset{compat=1.15}

% TikZ
\usetikzlibrary{external}
\tikzexternalize

\newcommand\usepng{
    \tikzset{external/force remake} % otherwise will use external pdf if it exists
    \tikzset{
        png export/.style = {
            % First we call ImageMagick
            external/system call/.add = {}{; convert -density 300 "\image.pdf" "\image.png"},
            % Now we force the PNG figure to be used instead of the PDF
            /pgf/images/external info,
            /pgf/images/include external/.code = {%
                \includegraphics
                [width=\pgfexternalwidth,height=\pgfexternalheight]
                {##1.png}%
            },
        },
        % Activate the script
        png export,
    }
}

% Use bitmap images in this document
\usepng



% %%%%%%%%%%%%%%%%%%
% Begin the document
% %%%%%%%%%%%%%%%%%%

\begin{document}

    \tikzsetnextfilename{foobar}
    \begin{tikzpicture}[baseline]
        \begin{axis}[
            xlabel = {x},
            ylabel = {y},
        ]

            \addplot+[mark=none] coordinates {
                (2,-2.8559703)
                (3,-3.5301677)
                (4,-4.3050655)
                (5,-5.1413136)
                (6,-6.0322865)
                (7,-6.9675052)
                (8,-7.9377747)
            };

        \end{axis}
    \end{tikzpicture}

\end{document}
  • Is there a reason not to use the standalone package? – Alan Sep 20 '18 at 1:21
  • Yes! I don't want to export a graph alone. I'm exporting a document containing a number of graphs and plots. And I need a version of that document, where all the graphs and plots are bitmap images and one version containing the original vector files. – Sam Sep 20 '18 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.