7

I want to use tikz images but include them in my document via PNG with includegraphics. This works nicely both for PDF and HTML, but the original tikz image is displayed, too. The reason why I need this is because I want to use it in conjunction with tex4ht and have the resulting HTML file display the PNG files and not the SVG files.

Currently, the following code just displays three pictures. I want the first one hidden and only be used to export into PNG.

\begin{tikzpicture}
    \node[fill=yellow!80,ellipse] (origin) {Origin};
    \node[fill=blue!30,ellipse] (destination) at (15em,0) {Destination};
    \path (origin) edge[->] node[above,font=\footnotesize] {the journey} (destination);
\end{tikzpicture}

\includegraphics{figures/myfigure.png}
\includegraphics{figures/myfigure-600.png}

To convert and copy the PNG files, I use the following code:

\tikzset{external/force remake}
\tikzset{png export/.style={
    external/system call={
        pdflatex \tikzexternalcheckshellescape
            -halt-on-error -interaction=batchmode -jobname "\image" "\texsource";
        convert -units pixelsperinch -density 150 "\image.pdf" "\image.png";
        convert -units pixelsperinch -density 600 "\image.pdf" "\image-600.png";
}}}
\tikzset{png export}
\tikzsetexternalprefix{figures/}
\tikzsetnextfilename{myfigure}

Alternatively, I could live with the tikzpicture command directly use the PNG files it generates for the output (instead of relying on the SVG file), so I don't need the includegraphics command (might even be the better solution :) ).

Here is the minimal example: https://www.overleaf.com/17313462rcpxbvktsgvd

Thank you in advance!

main.tex:

\documentclass{scrbook}
\title{TEST TIKZ}

\usepackage{tikz,pgfplots}
\usetikzlibrary{matrix}
\usepgfplotslibrary{external}
\usepackage{siunitx}
\DeclareSIUnit{\sr}{sr}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
%\tikzexternalize % comment out to debug if latex errors: generates the external pdf
\tikzset{external/force remake}
\tikzset{png export/.style={
    external/system call={
        pdflatex \tikzexternalcheckshellescape
            -halt-on-error -interaction=batchmode -jobname "\image" "\texsource";
        convert -units pixelsperinch -density 150 "\image.pdf" "\image.png";
        convert -units pixelsperinch -density 600 "\image.pdf" "\image-600.png";
}}}
\tikzset{png export}
\tikzsetexternalprefix{figures/}
\tikzsetnextfilename{myfigure}


\usetikzlibrary{calc}
\usetikzlibrary{positioning,shapes.arrows,shapes.symbols,decorations.pathreplacing,}
\usetikzlibrary{patterns,shapes,backgrounds,lindenmayersystems,shadings,intersections}


\begin{document}

\begin{tikzpicture}
    \node[fill=yellow!80,ellipse] (origin) {Origin};
    \node[fill=blue!30,ellipse] (destination) at (15em,0) {Destination};
    \path (origin) edge[->] node[above,font=\footnotesize] {the journey} (destination);
\end{tikzpicture}

\includegraphics{figures/myfigure.png}
\includegraphics{figures/myfigure-600.png}

\end{document}

latexmkrc:

# html generation
$pdflatex = "htlatex %S \"htlatex/htlatex.cfg,MyFonts,NoFonts\" \"\" \"\" -shell-escape > output.txt; pdflatex -shell-escape -synctex=1 %O %S";

$clean_ext .= ' 4ct 4tc idv lg tmp xref';
$clean_full_ext .= ' css html';

htlatex/htlatex.cfg:

\Preamble{xhtml}

\usepackage{graphicx}
\DeclareGraphicsExtensions{.svg,.png,.jpg,.jpeg,.gif,.pdf,.eps}
  \Configure{graphics*}
         {pdf}
         {\Needs{"convert -density 150 \csname Gin@base\endcsname.pdf
                               \csname Gin@base\endcsname.png"}%
          \Picture[pict]{\csname Gin@base\endcsname.png}%
          \special{t4ht+@File: \csname Gin@base\endcsname.png}
         }
  \Configure{graphics*}
         {eps}
         {\Needs{"convert -density 150 \csname Gin@base\endcsname.eps
                               \csname Gin@base\endcsname.png"}%
          \Picture[pict]{\csname Gin@base\endcsname.png}%
          \special{t4ht+@File: \csname Gin@base\endcsname.png}
         }

\Configure{VERSION}{}
\Configure{DOCTYPE}{\HCode{<!DOCTYPE html>\Hnewline}}
\Configure{HTML}{\HCode{<html>\Hnewline}}{\HCode{\Hnewline</html>}}


\begin{document}
\EndPreamble

The resulting HTML shows an SVG object + 2 pictures

9
  • Welcome! Please post your minimal code here in your question. This will encourage people to answer (one less click, one less site to deal with) and ensures that your question remains useful even if the content of the external link changes. Questions should be self-contained if at all possible.
    – cfr
    Jun 24, 2018 at 23:10
  • Where does an SVG file come into it? Note that PNG is not good for use in PDFs. If you have no choice, it will sometimes do, but it is definitely not something to contemplate where a vector format is available (here that format is probably PDF).
    – cfr
    Jun 24, 2018 at 23:13
  • However, if you really want to reduce the quality of your PDFs, you can change the inclusion command used to include externalised pictures, I think. Or you could use standalone, if the externalisation library can't do it.
    – cfr
    Jun 24, 2018 at 23:14
  • (I have added all the files, parts of the code come from here: users.cecs.anu.edu.au/~rod/resources/p-tikz-external-png.html ). Note that my goal is to create a HTML file that uses PNG files instead of SVG files. The goal is not to create a PDF file. As it is, it works (it creates an HTML file, shows the two generated PNG files, but also shows the SVG file in the HTML file---that I want to omit or be replaced by the PNG command). It's probably something trivial where I tell tikz to omit the output into the document... Jun 25, 2018 at 0:20
  • mmmh... what do you mean with "you can change the inclusion command used to include externalised pictures"? Jun 25, 2018 at 0:24

1 Answer 1

1

Ok, I got now a fully working example. The missing part was

/pgf/images/external info, /pgf/images/include external/.code=\includegraphics{##1.png}

to instruct the compiler to include the PNG, not the SVG (or whatever).

The full main.tex of a MWE looks like this:

\documentclass{scrbook}
\title{TEST TIKZ}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzset{png export/.style={external/system call={pdflatex 
\tikzexternalcheckshellescape -interaction=batchmode -jobname "\image" "\texsource"; 
convert -density 300 -transparent white "\image.pdf" "\image.png"},
/pgf/images/external info,
/pgf/images/include external/.code=\includegraphics{##1.png}}}
\tikzset{png export}
\usetikzlibrary{shapes}
\ifx\HCode\undefined 
    \tikzexternalize
\else
    \tikzexternalize[mode=only graphics]
\fi
\begin{document}
\begin{tikzpicture}
    \node[fill=yellow!80,ellipse] (origin) {Origin};
    \node[fill=blue!30,ellipse] (destination) at (15em,0) {Destination};
    \path (origin) edge[->] node[above,font=\footnotesize] {the journey} (destination);
\end{tikzpicture}
\begin{tikzpicture}
    \node[fill=yellow!80,ellipse] (origin) {Origin};
    \node[fill=blue!30,ellipse] (destination) at (15em,0) {Destination};
    \path (origin) edge[->] node[above,font=\footnotesize] {the journey} (destination);
\end{tikzpicture}
\begin{tikzpicture}
    \node[fill=yellow!80,ellipse] (origin) {Origin};
    \node[fill=blue!30,ellipse] (destination) at (15em,0) {Destination};
    \path (origin) edge[->] node[above,font=\footnotesize] {the journey} (destination);
\end{tikzpicture}
\end{document}

It correctly produces HTML output that looks like this:

<img src="main-figure0.png" alt="PIC">
<img src="main-figure1.png" alt="PIC">
<img src="main-figure2.png" alt="PIC">

I also had to add this:

\ifx\HCode\undefined 
    \tikzexternalize
\else
    \tikzexternalize[mode=only graphics]
\fi

otherwise the compiler would have a problem as the .tex file is read twice (once by pdflatex to generate the images, then by htlatex):

$pdflatex = "pdflatex -shell-escape -synctex=1 %O %S; htlatex %S \"htlatex/htlatex.cfg,MyFonts,NoFonts\" \"\" \"\" -shell-escape > output.txt";

Phew. :)

Thanks cfr, the includegraphics hint brought me on the right track :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .