tex4ebook:.svg 图像可以通过 \includegraphics 正确调整大小,但不能通过 \includesvg 正确调整大小

tex4ebook:.svg 图像可以通过 \includegraphics 正确调整大小,但不能通过 \includesvg 正确调整大小

我正在使用 tex4ebook 生成 .epub。我想要包含的大多数图形都是 svg。如果我使用 \includesvg 包含它们,并使用 \svgpath 指定图形路径,我可以使用 pdflatex 编译文档而不会出现任何问题。当我尝试使用 tex4ebook 进行编译时,tex4ht 找不到 .svg 文件。当我将 \svgpath 更改为 \graphicspath 时,tex4ht 能够包含图形,但 svg 的大小并未根据 \includesvg 给出的尺寸进行调整。

我尝试了这个解决方法,但图像仍然无法正确调整大小:

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

然后我用 \includegraphics 替换了 \includesvg,svg 就以正确的尺寸包含在内了。虽然在很多情况下只提供了一个尺寸,而且没有 .xbb 文件,但 svg 的尺寸是正确的。

我发现的建议是生成一个 .xbb 文件(将 svg 转换为 jpg、pdf 或 png 后)以便为 tex4ht 提供图形的尺寸,但事实上,这对于 includegraphics 来说不是必需的,这表明我可以告诉 tex4ht 如何从 .cfg 文件中的 \includesvg 获取尺寸。如果有人能建议我如何做到这一点,我将不胜感激。

下面我提供了一个 mwe,演示了 tex4ebook 如何处理 \includesvg 和 \includegraphics 提供的 svg。

以下是 .epub 的截图:

麦格

\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}

配置文件

\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>

日志文件很长,所以我决定只包含我认为与图形处理相关的部分。

(/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 ---

由于边界框不可用,我将 \Configure{Gin-dim} 更改为 \Configure{Gin-dim+},尝试编译时出现此错误:

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

答案1

问题在于当前版本的 TeX4ht 配置文件svg.sty没有将尺寸参数传递给\includegraphics,因此图像以其原始大小显示。

这是一个更好的版本,它为构建正确的尺寸,\includegraphics并且还支持\svgpath。将此代码保存为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

它重新定义了\includesvg命令。它通过 获得 SVG 文件名\svg@get@path。大多数代码\includegraphics使用来自 的代码构建维度svg.sty。仅支持widthheight和参数。 TeX4ht 版本支持直接包含 SVG 文件,因此无需执行任何其他操作。angle\includegraphics

以下是修改后的示例:

\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}

这是渲染后的 HTML 文件:

在此处输入图片描述

相关内容