I would like to make \inclugraphics{file.ext} perform on-the-fly conversion of graphics files with unsupported formats, e.g. .ext is .svg or .gif.
This use case happens at least with the package moodle.sty, where users might want to include local graphic files adapted to the web (SVG, GIF), and have them passed as-is to the XML output.
This XML output contains a question bank to be imported in the Moodle LMS for building quizzes.
In order to keep the process as simple as possible for the user, I would like to avoid requiring that two versions of the graphic file are manually kept synchronized locally (one for the XML, one for the standard LaTeX output file).
Here is a MWE that compiles without error and does what I expect with: Linux, ImageMagick for conversion, TeX Live 2020 and both compilation calls pdflatex -shell-escape MWE.tex and lualatex -shell-escape MWE.tex.
\documentclass{minimal}
\usepackage{graphicx}
\makeatletter
\edef\Gin@extensions{\Gin@extensions,.svg}%
\DeclareGraphicsRule{.svg}{png}{.png}{%
`convert '#1' \noexpand\Gin@base-svg-converted-to.png
}%
\makeatother
\begin{document}
\begin{filecontents*}{pict.svg}
<?xml version="1.0" encoding="UTF-8"?>
<svg width="10" height="10" version="1.1" viewBox="0 0 10 10"
xmlns="http://www.w3.org/2000/svg">
<circle cx="5" cy="5" r="4"/>
</svg>
\end{filecontents*}
This is a black disk: \includegraphics{pict}
\end{document}
Unfortunately, I am failing to make this work with XeTeX.
Here are parts of the log file when using \tracingmacros1
This is XeTeX, Version 3.14159265-2.6-0.999992 (TeX Live 2020/Debian) (preloaded format=xelatex 2020.10.2) 11 DEC 2020 11:20
entering extended mode
\write18 enabled.
%&-line parsing enabled.
**MWE.tex
(./MWE.tex
LaTeX2e <2020-02-02> patch level 5
L3 programming layer <2020-07-17>
...
\Gin@ext ->.svg
\Gin@rule@.svg #1->{png}{.png}{`convert '#1' \Gin@base -svg-converted-to.png }
#1<-\Gin@base \Gin@ext
\Gin@setfile #1#2#3->\ifx \#2\\Gread@false \fi \ifGin@bbox \else \ifGread@ \c
sname Gread@\expandafter \ifx \csname Gread@#1\endcsname \relax eps\else #1\fi
\endcsname {\Gin@base #2}\else \Gin@nosize {#3}\fi \fi \Gin@viewport@code \Gin@
nat@height \Gin@ury bp\advance \Gin@nat@height -\Gin@lly bp\Gin@nat@width \Gin@
urx bp\advance \Gin@nat@width -\Gin@llx bp\Gin@req@sizes \expandafter \ifx \csn
ame Ginclude@#1\endcsname \relax \Gin@drafttrue \expandafter \ifx \csname Gread
@#1\endcsname \relax @latex@error {Can not include graphics of type: #1}@ehc
\global \expandafter \let \csname Gread@#1\endcsname @empty \fi \fi \leavevmod
e \ifGin@draft \hb@xt@ \Gin@req@width {\vrule \hss \vbox to \Gin@req@height {\h
rule @width \Gin@req@width \vss \edef @tempa {#3}\rlap { \ttfamily \expandaft
er \strip@prefix \meaning @tempa }\vss \hrule }\hss \vrule }\else @addtofilel
ist {#3}\ProvidesFile {#3}[Graphic file (type #1)]\setbox \z@ \hbox {\csname Gi
nclude@#1\endcsname {#3}}\dp \z@ \z@ \ht \z@ \Gin@req@height \wd \z@ \Gin@req@w
idth \box \z@ \fi
#1<-png
#2<-.png
#3<-`convert '\Gin@base \Gin@ext ' \Gin@base -svg-converted-to.png
\Gread@eps #1->\Gread@generic {#1}\Gread@eps@aux
#1<-\Gin@base .png
...
->\errmessage LaTeX Error: File `\Gin@base .png' not found.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help@err@
\Gin@base ->pict
@err@ ->
! LaTeX Error: File `pict.png' not found.
Since the file pict.png is not created, I guess that the call to convert is not made.
Here is what I tried unsuccessfully:
- removing the part
-svg-converted-toin the\DeclareGraphicsRuledoes not help (still the conversion call is not made I guess) epstopdfdoes not seem to help- solutions involving the QuickTime library seem to be macOS-only, see here and there
My questions are:
- Am I missing something obvious?
- Why is the conversion call not made when compiling with XeTeX?
- Is there something we can do specifically for XeTeX to achieve what the MWE does with pdfTeX and LuaTeX?
--shell-escapeis required. That is a security risk. Beside this: on windows the name of the command is notconvert(at least not always). – Ulrike Fischer Dec 11 '20 at 11:21moodlepackage actually requires-shell-escapealready for conversion to base64. – mgk Dec 11 '20 at 11:32gswin64c.exeor any other conversion tool. – mgk Dec 11 '20 at 12:12-svg-converted-tois certainly specific to pdflatex/lualatex. You would probably have to hook into the image reading commands. Something like this (that is for dvips, so needs adaption): https://chat.stackexchange.com/transcript/41?m=52778507#52778507 – Ulrike Fischer Dec 11 '20 at 13:08shellesc,\DeclareGraphicsRule{.svg}{svg}{.svg}{}\def\Gread@svg#1{ \IfFileExists{#1}\ShellEscape{convert '#1' '#1'.png}}{} \edef\Gin@base{\Gin@base.svg}\edef\Gin@ext{.png}\let\Ginclude@svg=\Ginclude@bmp\Gread@bmp{#1.xbb}}. Probably not the most elegant solution, though. – mgk Dec 11 '20 at 14:44xetex.defcontrains lines like@namedef{Gin@rule@.eps.gz}#1{{eps}{.eps.xbb}{`gunzip -c #1}}? – mgk Dec 12 '20 at 09:11