2

I'm using XeLatex and dvisvgm to produce a .svg file. I want to include a .png image with \includegraphics but it doesn't work: there is a replacement image instead of the one I want.

Here is what I use to compile (overleaf):

    END {
system("xelatex -no-pdf svgbeamer.tex && xelatex -no-pdf svgbeamer.tex && dvisvgm --font-format=ttf --exact --zoom=-1 -p1,- svgbeamer.xdv")
}

And here is my minimal example:

\documentclass[dvisvgm,aspectratio=169]{beamer}

\setbeamersize{text margin left=0cm} %to get real fullscreen

\begin{document}

\includegraphics[height=8cm]{banane.png}

\end{document}

The same code compiled with only XeLatex works alright.

Any clue?

PS: the image is

enter image description here

5
  • does dvisvgm make any warnings? Aug 23, 2022 at 17:21
  • I'm not sure where to look
    – FunkiePhil
    Aug 23, 2022 at 22:36
  • Works for me. Note that unlike PDF, PNG is not physically embedded into the output SVG, only linked. Therefore, it should be present in the same directory as the final SVG.
    – AlexG
    Aug 24, 2022 at 6:59
  • Ah I get It, is it the case for any image file type?
    – FunkiePhil
    Aug 24, 2022 at 8:55
  • Except PDF, yes. But it can be worked around. See answer.
    – AlexG
    Aug 24, 2022 at 9:26

1 Answer 1

3

PNG (and other web compatible bitmap formats like JPEG and GIF) is not physically embedded into the SVG output by dvisvgm, only referenced via external link. Only PostScript and PDF (into which most static bitmap formats may be converted or wrapped using an external converter, such as ImageMagick) are. They remain separate and must be found by the Web browser during run-time.

To make SVG self-sufficient, bitmap files must be base64 encoded and embedded as data:image/png;base64,... data blob. This can be done as follows, patching the graphicx backend driver for dvisvgm.

This requires the media4svg package at a recent version (>= v0.9 2022-08-12).

Typeset with

dvilualatex svgbeamer
dvisvgm --font-format=woff2 --bbox=papersize --zoom=-1 -p1,- svgbeamer

or

latex --shell-escape svgbeamer
dvisvgm --font-format=woff2 --bbox=papersize --zoom=-1 -p1,- svgbeamer

or

xelatex --no-pdf --shell-escape svgbeamer
dvisvgm --font-format=woff2 --bbox=papersize --zoom=-1 -p1,- svgbeamer.xdv
\documentclass[dvisvgm,aspectratio=169]{beamer}

%\graphicspath{{myimages}} % add search dirs as needed

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% patch graphics backend driver `dvisvgm.def' to physically embed
% bitmaps into DVI
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\let\Ginclude@bitmapOrig\Ginclude@bitmap
\def\Ginclude@bitmap#1{%
  \baseSixtyFour{#1}{72}{{?nl}}\bitmap@stream%
  \Ginclude@bitmapOrig{%
    data:image/\expandafter\remove@dot\Gin@ext;;base64,{?nl}%
    \bitmap@stream}%
}
\def\remove@dot.#1;{#1}
\makeatother

\RequirePackage{media4svg} % provides base64-encode utility
\ExplSyntaxOn
\cs_new:Npn\baseSixtyFour#1#2#3#4{
  \sys_if_engine_luatex:TF{
    \xdef#4{\directlua{media4svg.base64("#1",#2,"#3")}}
  }{
    \msvg_convert_file_to_blob:nnnN{#1}{#2}{#3}#4
  }
}
\ExplSyntaxOff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
\begin{document}

\begin{frame}{Embedded PNG}
\includegraphics[height=6cm]{banane.png}
\end{frame}

\end{document}
2
  • Would it be useful to add something for base64 encoding to l3kernel?
    – Joseph Wright
    Aug 24, 2022 at 9:46
  • Yes, I think such a tool could come in handy from time to time. Is it possible to read arbitrary binary files bytewise without Lua?
    – AlexG
    Aug 24, 2022 at 11:34

You must log in to answer this question.

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