1

I'm generating an .epub with tex4ebook. Most of the graphics I want to include are svg. If I include them with \includesvg, and specify the graphics path with \svgpath, I can compile the document with pdflatex without any issues. When I tried to compile with tex4ebook, tex4ht couldn't find the .svg files. When I changed \svgpath to \graphicspath, tex4ht was able to include the graphics, but the svgs were not resized according to the dimensions given to \includesvg.

I tried this workaround, but the images were still not resized correctly:

\ifdefined\HCode
    \graphicspath{{./images}}    
    \renewcommand\includesvg[2][]{\includegraphics{#2}}
\else
    \svgpath{{./images}}
\fi 

Then I replaced \includesvg with \includegraphics, and the svg was included with the correct dimensions. The dimensions for the svgs were correct even though only one dimension was provided in many cases, and there was no .xbb file.

The recommendations that I've found suggest generating an .xbb file (after converting svg to jpg, pdf, or png) to give tex4ht the dimensions for the graphic, but the fact that this isn't necessary for includegraphics suggests that I could tell tex4ht how to get the dimensions from \includesvg in a .cfg file. If someone could suggest how I could do that, I would really appreciate it.

Below I've included an mwe demonstrating how tex4ebook handles svgs provided with \includesvg and \includegraphics.

Here's a screenshot of the .epub:

mwe.tex

\documentclass{book}

\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{svg}
\graphicspath{{./images}}
\begin{document}

\begin{figure}[ht]
\centering
\caption{Image generated with includegraphics}
\label{fig:frogone}
\includegraphics[width=0.50\textwidth]{images/simplefrog.svg}
\end{figure}

\begin{figure}[ht]
\centering
\caption{Image generated with includesvg}
\label{fig:frogtwo}
\includesvg[width=0.50\textwidth]{images/simplefrog.svg}
\end{figure}

\end{document}

mwe.cfg

\Preamble{xhtml}

\ConfigureEnv{figure}
   {\ifvmode\IgnorePar\fi\EndP\HCode{<div class="figure">}%
    \bgroup \Configure{float}{\ShowPar}{}{}%
   }
   {\egroup
    \ifvmode\IgnorePar\fi\EndP\HCode{</div>}\ShowPar
\par}
{}{}

\Css{div.caption {text-align:center;font-size:83\%;text-indent:0em; margin-left:2em; margin-right:2em; }}
\Css{div.figure{text-align:center;clear:both;overflow:auto;width:100\%;margin-bottom:1em;}}

\makeatletter 
\DeclareGraphicsExtensions{.svg}
\ExplSyntaxOn
\Configure{Gin-dim}
    {style="width:\fp_eval:n{round(\Gin@req@width/\textwidth*100,2)}\%;max-height:\fp_eval:n{round(\Gin@req@height/\textwidth*100,2)}\%;"}
\ExplSyntaxOff
\makeatother 

\begin{document}

\EndPreamble`

mwe.html

<?xml version='1.0' encoding='utf-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns='http://www.w3.org/1999/xhtml'> 
<head>
   <title></title> 
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> 
<meta content='TeX4ht (https://tug.org/tex4ht/)' name='generator' /> 
<meta content='TeX4ht (https://tug.org/tex4ht/)' name='originator' /> 
<!--  charset=utf-8,epub,uni-html4,html,xhtml  --> 
<meta content='mwe.tex' name='src' /> 
<link href='mwe.css' type='text/css' rel='stylesheet' /> 
</head><body>
   <div class='figure'>                                                                  
<a id='x1-21'></a>                                                                 
<br /> <div class='caption'><span class='id'>Figure 1: </span><span class='content'>Image generated with includegraphics</span></div><!-- tex4ht:label?: x1-21  -->
<img style='width:50%;max-height:50%;' src='./images/simplefrog.svg' alt='PIC' />                                                                  
   </div>
   <div class='figure'>                                                                  
<a id='x1-32'></a>                                                                  
<br /> <div class='caption'><span class='id'>Figure 2: </span><span class='content'>Image generated with includesvg</span></div><!-- tex4ht:label?: x1-32  -->
<img style='width:20.95%;max-height:20.95%;' src='./images/simplefrog.svg' alt='PIC' />                                                                  
   </div>    
</body></html>

The log file is very long, so I decided to only include the parts that I thought were relevant for graphics processing.

(/usr/share/texmf-dist/tex/latex/graphics/graphicx.sty
Package: graphicx 2020/09/09 v1.2b Enhanced LaTeX Graphics (DPC,SPQR)
(/usr/share/texmf-dist/tex/generic/tex4ht/graphics-hooks.4ht
version 2020-10-17-13:53
) (/usr/share/texmf-dist/tex/latex/graphics/graphics.sty
Package: graphics 2020/08/30 v1.4c Standard LaTeX Graphics (DPC,SPQR)
(/usr/share/texmf-dist/tex/latex/graphics/trig.sty
Package: trig 2016/01/03 v1.10 sin cos tan (DPC)
) (/usr/share/texmf-dist/tex/latex/graphics-cfg/graphics.cfg
File: graphics.cfg 2016/06/04 v1.11 sample graphics configuration
)
Package graphics Info: Driver file: dvips.def on input line 105.
(/usr/share/texmf-dist/tex/latex/graphics-def/dvips.def
File: dvips.def 2017/06/20 v3.1d Graphics/color driver for dvips
))
\Gin@req@height=\dimen139
\Gin@req@width=\dimen140
)) (/usr/share/texmf-dist/tex/latex/base/inputenc.sty
Package: inputenc 2020/08/01 v1.3d Input encoding file
\inpenc@prehook=\toks17
\inpenc@posthook=\toks18
) (/usr/share/texmf-dist/tex/latex/svg/svg.sty
Package: svg 2020/11/26 v2.02k (include SVG pictures)

) (/usr/share/texmf-dist/tex/latex/pgf/systemlayer/pgfsys.sty (/usr/share/texmf
-dist/tex/latex/pgf/utilities/pgfrcs.sty (/usr/share/texmf-dist/tex/generic/pgf
/utilities/pgfutil-common.tex
\pgfutil@everybye=\toks19
\pgfutil@tempdima=\dimen141
\pgfutil@tempdimb=\dimen142
(/usr/share/texmf-dist/tex/generic/pgf/utilities/pgfutil-common-lists.tex)) (/u
sr/share/texmf-dist/tex/generic/pgf/utilities/pgfutil-latex.def
\pgfutil@abb=\box48
) (/usr/share/texmf-dist/tex/generic/pgf/utilities/pgfrcs.code.tex (/usr/share/
texmf-dist/tex/generic/pgf/pgf.revision.tex)
Package: pgfrcs 2020/12/01 v3.1.7a (3.1.7a)
)) (/usr/share/texmf-dist/tex/generic/pgf/systemlayer/pgfsys.code.tex
Package: pgfsys 2020/12/01 v3.1.7a (3.1.7a)
(/usr/share/texmf-dist/tex/generic/pgf/utilities/pgfkeys.code.tex
\pgfkeys@pathtoks=\toks20
\pgfkeys@temptoks=\toks21
(/usr/share/texmf-dist/tex/generic/pgf/utilities/pgfkeysfiltered.code.tex
\pgfkeys@tmptoks=\toks22
))
\pgf@x=\dimen143
\pgf@y=\dimen144
\pgf@xa=\dimen145
\pgf@ya=\dimen146
\pgf@xb=\dimen147
\pgf@yb=\dimen148
\pgf@xc=\dimen149
\pgf@yc=\dimen150
\pgf@xd=\dimen151
\pgf@yd=\dimen152
\w@pgf@writea=\write3
\r@pgf@reada=\read2
\c@pgf@counta=\count189
\c@pgf@countb=\count190
\c@pgf@countc=\count191
\c@pgf@countd=\count192
\t@pgf@toka=\toks23
\t@pgf@tokb=\toks24
\t@pgf@tokc=\toks25
\pgf@sys@id@count=\count193
(/usr/share/texmf-dist/tex/generic/pgf/systemlayer/pgf.cfg
File: pgf.cfg 2020/12/01 v3.1.7a (3.1.7a)
)
Driver file for pgf: pgfsys-tex4ht.def
(/usr/share/texmf-dist/tex/generic/pgf/systemlayer/pgfsys-tex4ht.def
File: pgfsys-tex4ht.def 2020/12/01 v3.1.7a (3.1.7a)
(/usr/share/texmf-dist/tex/generic/pgf/systemlayer/pgfsys-common-svg.def
File: pgfsys-common-svg.def 2020/12/01 v3.1.7a (3.1.7a)
\pgf@sys@svg@objectcount=\count194
\pgf@sys@svg@scopecount=\count195
\pgf@sys@svg@type@count=\count196
\pgf@sys@svg@canvascount=\count197
)
\pgf@sys@svg@picnum=\count198
\pgf@sys@svg@nodenum=\count199
\pgfsys@foreignobject@Box=\box49
\pgf@s=\dimen153
\pgf@t=\dimen154
(/usr/share/texmf-dist/tex/generic/tex4ht/xcolor-hooks.4ht
version 2020-10-17-13:53
))) (/usr/share/texmf-dist/tex/generic/pgf/systemlayer/pgfsyssoftpath.code.tex
File: pgfsyssoftpath.code.tex 2020/12/01 v3.1.7a (3.1.7a)
\pgfsyssoftpath@smallbuffer@items=\count266
\pgfsyssoftpath@bigbuffer@items=\count267
) (/usr/share/texmf-dist/tex/generic/pgf/systemlayer/pgfsysprotocol.code.tex
File: pgfsysprotocol.code.tex 2020/12/01 v3.1.7a (3.1.7a)
)) (./mwe.cfg (/usr/share/texmf-dist/tex/generic/tex4ht/tex4ht.4ht
version 2020-03-12-14:03
l.13 --- TeX4ht warning --- File `"./images/simplefrog.xbb"' not found ---
l.13 --- TeX4ht warning --- Cannot determine size of graphic in "./images/simpl
efrog.xbb" (no BoundingBox) ---
l. 13 --- needs --- ./images/simplefrog.svg ---
[3

] [4

] [5

]
l.20 --- TeX4ht warning --- File `"./images/simplefrog.xbb"' not found ---
l.20 --- TeX4ht warning --- Cannot determine size of graphic in "./images/simpl
efrog.xbb" (no BoundingBox) ---
l. 20 --- needs --- ./images/simplefrog.svg ---

Since the bounding box is not available, I changed \Configure{Gin-dim} to \Configure{Gin-dim+}, and I got this error when I tried to compile:

[ERROR]   htlatex: ./mwe.cfg    19   LaTeX Error: Missing \begin{document} in `mwe.cfg'.
1

The problem is that the current version of TeX4ht configuration file for svg.sty doesn't pass dimension parameters to \includegraphics, so images are displayed at their original size.

Here is a better version, which constructs correct dimensions for \includegraphics and it also support \svgpath. Save this code as svg.4ht:

% svg.4ht (2018-12-30-09:31), generated from tex4ht-4ht.tex
% Copyright 2018 TeX Users Group
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.3c of this license or (at your option) any
% later version. The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions
% of LaTeX version 2005/12/01 or later.
%
% This work has the LPPL maintenance status "maintained".
%
% The Current Maintainer of this work
% is the TeX4ht Project <http://tug.org/tex4ht>.
%
% If you modify this program, changing the
% version identification would be appreciated.
\immediate\write-1{version 2018-12-30-09:31}

\renewcommand\includesvg[2][]{%
  % get file name with \svgpath support
  \svg@get@path{#2}{}%
  % set keys
  \svg@local@param@set{#1}%
  \if@svg@file@found%
    % convert supported parameters from \includesvg for use with \includegraphics
    \edef\svg@tempb{}
    \ifdim\svg@param@height>\z@\relax%
      \edef\svg@tempb{\svg@tempb,height=\svg@param@height}%
    \fi%
    \ifdim\svg@param@width>\z@\relax%
      \edef\svg@tempb{\svg@tempb,width=\svg@param@width}%
    \fi%
    \ifdim\dimexpr\svg@param@angle\p@\relax=\z@\relax\else%
      \edef\svg@tempb{%
        \svg@tempb,origin=\svg@param@origin,angle=\svg@param@angle%
      }%
    \fi%
    \expandafter\includegraphics\expandafter[\svg@tempb]{\svg@file@base.\svg@file@ext}
  \else
    \typeout{SVG file #1 cannot be found}
  \fi
}

\Hinput{svg}
\endinput

It redefines the \includesvg command. It gets the SVG filename thanks to \svg@get@path. Most of the code constructs dimensions for \includegraphics using code that comes from svg.sty. Only width, height and angle parameters are supported. TeX4ht version of \includegraphics supports direct inclusion of SVG files, so nothing more is needed.

Here is a modified example:

\documentclass{book}

\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{svg}
\graphicspath{{./images}}
\svgpath{{./images}}
\begin{document}

\begin{figure}[ht]
\centering
\caption{Image generated with includegraphics}
\label{fig:frogone}
\includegraphics[width=0.50\textwidth]{simplefrog.svg}
\end{figure}

\begin{figure}[ht]
\centering
\caption{Image generated with includesvg}
\label{fig:frogtwo}
\includesvg[width=0.50\textwidth]{simplefrog.svg}
\end{figure}

\end{document}

And this is the rendered HTML file:

enter image description here

2
  • That works, thanks! Also, the dancing frogs are great :D – avren Feb 10 at 22:38
  • 1
    @avren I've updated TeX4ht sources, so it should be available in TL soon :) – michal.h21 Feb 10 at 22:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.