tex4ht:使用 tikzexternalize 仍然包含 svg,而不是 png

tex4ht:使用 tikzexternalize 仍然包含 svg,而不是 png

我正在使用make4ht从包含图像的 .tex 文件生成 HTML tikz

我将tikzexternalize命令与代码一起使用Htlatex 和 Tikz 有时会创建错误的 svg确保图像作为.png文件包含在 HTML 中。

最小工作示例

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{external}

\ifdefined\HCode
 % this part is activated with make4ht
 \tikzexternalize[mode=only graphics]
 \tikzset{png export/.style={
    /pgf/images/external info,
    /pgf/images/include external/.code={%
      \includegraphics{##1.png},
     },
   },
  png export,
 }
\else
 % this part is activated with pdflatex
 \tikzexternalize
 \tikzset{pdf export/.style={
    /pgf/images/external info,
    /pgf/images/include external/.code={%
      \includegraphics{##1.pdf},
     },
   },
  pdf export,
 }
\fi

\begin{document}
\begin{tikzpicture}
 \draw (0,0) -- (0,1) -- (1,1) -- (1,0) -- (0,0);
\end{tikzpicture}
\end{document}

汇编

我使用以下命令来生成myfile-figure0.pdf然后图像魔法生产myfile-figure0.png

pdflatex --shell-escape myfile
magick.exe myfile-figure0.pdf myfile-figure0.png

然后我用

make4ht -m draft myfile.tex

生成以下 HTML

<!DOCTYPE html> 
<html lang='en-US' xml:lang='en-US'> 
<head><title></title> 
<meta charset='utf-8' /> 
<meta content='TeX4ht (https://tug.org/tex4ht/)' name='generator' /> 
<meta content='width=device-width,initial-scale=1' name='viewport' /> 
<link href='myfile.css' rel='stylesheet' type='text/css' /> 
<meta content='myfile.tex' name='src' /> 
</head><body>
<!-- l. 29 --><p class='noindent'><img alt=',
' src='myfile0x.svg' />
</p>
    
</body> 
</html>

问题

我如何配置我的构建过程,以便

<img alt=',
' src='myfile0x.svg' />

我收到

<img alt=',
' src='myfile-figure0.png' />

以供参考

我使用的是 texlive 2021 和 make4ht 版本 0.3j。在 texlive 2020 及之前的版本中,我没有遇到此问题,并且我的设置给出了我想要的输出。

也许我的问题与以下代码片段有关,myfile.log我在 texlive 2020 中也没有收到该代码片段。

(\end occurred inside a group at level 1)

### simple group (level 1) entered at line 22 ({)
### bottom level

答案1

这是因为 TeX4ht 现在pgfsys-dvisvgm4ht.def默认使用驱动程序。它不支持该external库。以下是修复此问题的版本:

% Copyright 2021 by Michal Hoftich
% Copyright 2006 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\ProvidesFileRCS{pgfsys-dvisvgm4ht.def}

% Driver commands for tex4ht

%
% Load common pdf commands:
%

% we load the dvips driver by default. it doesn't support patterns and some other stuff,
% but it handles better nested images and some formatting. if you use patterns or if you
% have other issues with the default method, pass the "tikz-dvisvgm" option to make4ht. 
\ifdefined\ifOption
\ifOption{tikz+}{\input pgfsys-dvisvgm.def}{\input pgfsys-dvips.def}
\else
% load the dvips driver by default
\input pgfsys-dvips.def
\fi


\def\texfourht@tikz@begin{%
  \bgroup%
  \def\run@pict@cmd{}% insert the \Picture hooks only in the top nesting level
  \def\end@pict@cmd{}%
  \ifdefined\EndPicture\else% We are already inside command that uses \Picture
  \ifdefined\inside@pict@cmd\else% handle nested uses
  \ifdefined\tikzexternalize\else% Support externalize library
  \def\run@pict@cmd{\Picture*}%
  \def\end@pict@cmd{\EndPicture}%
  \fi\fi\fi%
  % command used to detect nesting
  \def\inside@pict@cmd{}%
  \csname a:tikzpicture\endcsname%
}

\def\texfourht@tikz@end{%
  \csname b:tikzpicture\endcsname%
  \egroup%
}

\AtBeginDocument{%
  \NewConfigure{tikzpicture}{2}%
  \catcode`\:=11%
  \Configure{tikzpicture}{%
    \protect\csname nested:math\endcsname% support display math
    \run@pict@cmd{}%
  }{\end@pict@cmd}
  % configure the output picture format to svg, as it will require dvisvgm
  % post processing. 
  \Configure{Picture}{.svg}%
  % insert tex4ht hooks around TikZ picture box
  \def\pgfsys@typesetpicturebox#1{%
    \texfourht@tikz@begin%
    \orig@pgfsys@typesetpicturebox{#1}%
    \texfourht@tikz@end%
  }
  %
  \ConfigureEnv{tikzpicture}{\texfourht@tikz@begin}{\texfourht@tikz@end}{}{}%
  \ConfigureEnv{pgfpicture}{\texfourht@tikz@begin}{\texfourht@tikz@end}{}{}%
  \catcode`\:=12%
}


% Make the code inserted by tex4ht configurable
% 


\let\orig@pgfsys@typesetpicturebox\pgfsys@typesetpicturebox
%\def\pgf@sys@postscript@header#1{{\special{! #1}}}


\endinput


%%% Local Variables:
%%% mode: latex
%%% End:

我还发现你的 TeX 文件中有一个问题。\includegraphics命令后有一个虚假的逗号。它以 HTML 代码结尾。这是一个修复版本:

\documentclass{article}


\usepackage{tikz}
\usetikzlibrary{external}

\ifdefined\HCode
 % this part is activated with make4ht
 \tikzexternalize[mode=only graphics]
 \tikzset{png export/.style={
    /pgf/images/external info,
    /pgf/images/include external/.code={%
      \includegraphics{##1.png}
     },
   },
  png export,
 }
\else
 % this part is activated with pdflatex
 \tikzexternalize
 \tikzset{pdf export/.style={
    /pgf/images/external info,
    /pgf/images/include external/.code={%
      \includegraphics{##1.pdf},
     },
   },
  pdf export,
 }
\fi

\begin{document}
\begin{tikzpicture}
 \draw (0,0) -- (0,1) -- (1,1) -- (1,0) -- (0,0);
\end{tikzpicture}

\end{document}

顺便说一句,TeX4ht 内置了对外部化库的支持,它始终需要图片模式。当前版本不知何故出现问题,但您可以尝试此修复tikz-hooks.4ht

% tikz-hooks.4ht (2021-10-21-14:55), generated from tex4ht-4ht.tex
% Copyright 2021 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 2021-10-21-14:55}

\ifdefined\pgfsysdriver\else%
  \typeout{*****************************}
  \typeout{TeX4ht info: Using dvisvgm4ht TikZ driver. Put \detokenize{\def\pgfsysdriver{driver-name}} to your
  document before use of TikZ if you want to another driver. Use tikz+ option if your TikZ pictures use patterns.}%
  \def\pgfsysdriver{pgfsys-dvisvgm4ht.def}%
\fi%
\:AtEndOfPackage{%
\let\use:tikzlibrary\usetikzlibrary
\def\find:externalize#1external#2\@nil{%
  \ifdefined\tikzexternalize
    \let\tikz:externalize\tikzexternalize
    \renewcommand\tikzexternalize[1][]{\tikz:externalize[##1,mode=only graphics]}
    \tikzset{%
      tex4ht inc/.style={%
        /pgf/images/include external/.code={%
          \includegraphics[]{####1.pdf}%
        }%
      }%
    }%
    \tikzset{tex4ht inc}%
  \fi%
}
\append:defI\use@@tikzlibrary{\find:externalize##1external\@nil}%
}

然后您可以简化您的文档:

\documentclass{article}


\usepackage{tikz}
\usetikzlibrary{external}

\tikzexternalize
\tikzset{pdf export/.style={
   /pgf/images/external info,
   /pgf/images/include external/.code={%
     \includegraphics{##1.pdf}
    },
  },
 pdf export,
}

\begin{document}
\begin{tikzpicture}
 \draw (0,0) -- (0,1) -- (1,1) -- (1,0) -- (0,0);
\end{tikzpicture}

\end{document}

结果如下:

在此处输入图片描述

相关内容