\autoref 会破坏 tex4ht 吗?

\autoref 会破坏 tex4ht 吗?

在开始使用时tex4ht,我注意到了几个问题。以下 MWE 可以很好地编译pdflatex

\documentclass[a4paper, 12pt, ngerman]{scrartcl}

\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenx}

\usepackage{lmodern}

\usepackage[a4paper, left=30mm, right=25mm, top=25mm, bottom=30mm]{geometry}
\usepackage{setspace}
\onehalfspacing
\parindent0mm

\usepackage{amsmath}
\usepackage{commath}
\usepackage{units}
%% Bilder einbinden
\usepackage{graphicx}
%% zum korrekten Export der Vektorgrafiken als SVG
\ifdefined\HCode
\def\pgfsysdriver{pgfsys-tex4ht.def}
\fi
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\usepackage[breaklinks, colorlinks]{hyperref}

\begin{document}

Erstmal etwas inline-Mathe $E = m c^2$, etwas sinnloser Text und dann eine lange, abgesetzte Formel:
\begin{multline}
\iiint_V \dpd{E}{t} \dif V + \iint_S E u_i n_i \dif S = \\
\iint_S \tau_{ij} u_i n_j \dif S - \iiint_V \dpd{p u_i}{x_i} \dif V + 
\iiint_V \rho f_i u_i \dif V - \iint_S q_i n_i \dif S \label{eq:energie}
\end{multline}

\autoref{eq:energie}
ist ein gutes Beispiel für eine lange und komplizierte Gleichung, die nicht mehr auf eine Zeile passt.

\autoref{fig:spz}
zeigt eigentlich nichts Sinnvolles, genauer gesagt einen beliebigen Funktionsverlauf mit $\rho$ = \unitfrac[999]{kg}{m\textsuperscript{3}} und $u_1$ = \unitfrac[2]{m}{s}.

\begin{figure}[htb]
\centering
\begin{tikzpicture}
\begin{axis}[width=0.8\textwidth,
height=0.3\textheight,
scale only axis,
xmin=0,
xmax=1,
xlabel={Dimensionless number $\zeta$ (-)},
xlabel style={fill=white,fill opacity=0.9},
ymin=0,
ymax=2000,
ylabel={Resulting pressure $\Sigma p$ (bar)},
ylabel style={fill=white,fill opacity=0.9},
axis background/.style={fill=white},
legend style={at={(0.71,0.96)},anchor=north west,draw=black,fill=white,legend cell align=left}
]
%% Formula 1
\addplot[blue, domain=0:1, samples=101]
{1998*(1 - x^2)};
\addlegendentry{Formula 1};
\end{axis}
\end{tikzpicture}
\caption{Resulting pressure $\Sigma p$ as a function of $\zeta$ \label{fig:spz}}
\end{figure}

\end{document}

htlatex file.tex "xhtml"在图像部分产生错误,可通过应用解决此修复到马车pgfsys-tex4ht.def,这似乎仍然是TexLive 2016 中未修复!!!

下一个问题出现在\autoref。如果我注释掉包含它的两行(第 37 行和第 40 行),HTML 转换可以正常运行。但是他们htlatexUndefined control sequence ... \autoref。这是一个已知的错误吗?有解决方法吗?或者这与语言设置有关?

此外,还生成了两个 SVG 文件,一个没有图例文本的正确文件(file-1.svg),另一个显然是空的(file-2.svg)。后者是嵌入的,因此图像空间保持空白。如果我手动编辑file.html以嵌入file-1.svg,它会起作用,只是图例不显示(没有文本的空白矩形)。任何帮助都值得感激。

编辑通过 @michal.h21 的惊人详细答案所建议的更改,hyperref问题得到了解决。但是,未生成 SVG。日志文件显示:

LaTeX Warning: File `sample4ht-figure0.svg' not found on input line 80.

l.80 --- TeX4ht warning --- File `sample4ht-figure0.svg' not found ---
l.80 --- TeX4ht warning --- Cannot determine size of graphic in sample4ht-figur
e0.svg (no BoundingBox) ---
l. 80 --- needs --- sample4ht-figure0.svg ---

此外,积分看起来比其他变量更大更粗。常微分算子d完全丢失。 可以以某种方式修复吗?

编辑2我确实使用了该-shell-escape选项并安装了 Inkscape,但从日志文件来看,它似乎inkscape甚至没有被调用!我会在另一台机器上尝试。

数学渲染问题新问题在这里,因为原始hyperref问题已解决。

答案1

我个人会使用Tikz外部化来获取 SVG 图像,pgfsys-tex4ht.def驱动程序有更多缺点。外部化需要对文档进行一些小的更改,请参阅我的这个答案更多细节。

的问题是由Hyperref 包支持\autoref中的错误引起的。它错误地检查了类型,如果未设置标签类型(这是 Amsmath 环境的情况),则会导致编译错误。我已经修复了这个问题并为 Amsmath 环境添加了标签类型,因此您的示例现在应该可以正常工作了。请将以下代码保存为:tex4ht\labelnameref.4ht

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% nameref.4ht                           2013-06-28-15:01 %
% Copyright (C) 2005--2009       Eitan M. Gurari         %
%               2009--2013       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".%
%                                                        %
% This Current Maintainer of this work                   %
% is the TeX4ht Project <[email protected]>.                %
%                                                        %
% If you modify this program your changing its signature %
% with a directive of the following form will be         %
% appreciated.                                           %
%            \message{signature}                         %
%                                                        %
%                                        [email protected]  %
%                             http://www.tug.org/tex4ht  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\immediate\write-1{version 2013-06-28-15:01}

   \let\NR:Type\relax
\let\ltx@label\label
\def\prf:label{{\ifx \NR:Title\:UnDef \else \NR:Title\fi}%
               {\ifx \NR:Type\relax\else \NR:Type .1\fi}{}}%
\let\NR:StartSec\:StartSec
\let\NR:no@sect\no@sect
\def\no@sect#1#2#3#4#5#6[#7]#8{\gdef\NR:Title{\a:newlabel{#7}}%
    \gdef\NR:Type{#1}%
    \NR:no@sect{#1}{#2}{#3}{#4}{#5}{#6}[{#7}]{#8}}
\def\:StartSec#1#2#3{%
   \gdef\NR:Title{\a:newlabel{#3}}%
   \gdef\NR:Type{#1}%
   \NR:StartSec{#1}{#2}{#3}%
}

\def\:tempc#1#2#3#4#5#6[#7]#8{%
   \gdef\NR:Title{\a:newlabel{#7}}%
   \gdef\NR:Type{#1}%
   \o:NR@sect:{#1}{#2}{#3}{#4}{#5}{#6}[{#7}]{#8}}
\HLet\NR@sect\:tempc

\def\:tempc#1#2#3#4#5{%
   \gdef\NR:Title{\a:newlabel\ssect:ttl}%
   \gdef\NR:Type{#1}%
   \o:NR@ssect:{#1}{#2}{#3}{#4}{#5}%
}
\HLet\NR@ssect\:tempc

\def\:tempc[#1]#2{%
   \gdef\NR:Title{\a:newlabel{#1}}%
   \gdef\NR:Type{part}%
   \o:no@part:[{#1}]{#2}}
\HLet\no@part\:tempc

\def\:tempc#1{%
   \gdef\NR:Title{\a:newlabel{#1}}%
   \gdef\NR:Type{part}%
   \o:no@spart:{#1}}
\HLet\no@spart\:tempc

\def\:tempc[#1]#2{%
   \gdef\NR:Title{\a:newlabel{#1}}%
   \gdef\NR:Type{chapter}%
   \o:NR@chapter:[{#1}]{#2}}
\HLet\NR@chapter\:tempc

\def\:tempc#1{%
   \gdef\NR:Title{\a:newlabel\sch:ttl}%
   \o:NR@schapter:{#1}%
   \gdef\NR:Type{chapter}%
}
\HLet\NR@schapter\:tempc

\let\o:NR@@caption\@caption
%
%
% Keith Andrews <[email protected]> reported that \@captype as
% \NR:Type threw an undefined control sequence error. I think
% \@currenvir is safe, there is nothing special about \@captype.
%
\long\def\@caption#1[#2]{%
    \gdef\NR:Type{\@currenvir}%
    \gdef\NR:Title{\a:newlabel{#2}}%
   \o:NR@@caption{#1}[{#2}]%
}

\let\o:NRorg@opargbegintheorem\@opargbegintheorem
  \def\@opargbegintheorem#1#2#3{%
    \gdef\NR:Title{\a:newlabel{#3}}%
    \gdef\NR:Type{\@currenvir}%
    \NR@gettitle{#3}%
    \defineautorefname{\@currenvir}{#1}%
    \o:NRorg@opargbegintheorem{#1}{#2}{#3}%
  }%

\let\o:NRorg@begintheorem\@begintheorem
  \def\@begintheorem#1#2{%
    \gdef\NR:Title{\a:newlabel{#1 #2}}%
    \gdef\NR:Type{\@currenvir}%
    \defineautorefname{\@currenvir}{#1}%
    \NR@gettitle{}%
    \o:NRorg@begintheorem{#1}{#2}%
  }%

\AtBeginDocument{%
  \@ifpackageloaded{listings}{%
      \def\:tempc#1{%
         \gdef\NR:Title{\a:newlabel{listing}}%
         \gdef\NR:Type{lstlisting}%
         \o:NROrg@lst@MakeCaption:{#1}%
         \gdef\@currentlabelname{listing}}
      \HLet\NROrg@lst@MakeCaption\:tempc
  }{}%
  \@ifpackageloaded{amsmath}{%
     \def\:tempc{%
       \gdef\NR:Title{\a:newlabel{equation}}%
       \gdef\NR:Type{equation}%
       \gdef\@currentlabelname{equation}%
     }
     %\pend:def\a:multline{\:tempc}
     \Configure{@begin}{multline}{\:tempc}
     \Configure{@begin}{equation}{\:tempc}
     \Configure{@begin}{boxed}{\:tempc}
     \Configure{@begin}{equations}{\:tempc}
     \Configure{@begin}{equation}{\:tempc}
     \Configure{@begin}{gather*}{\:tempc}
     \Configure{@begin}{gather}{\:tempc}
     \Configure{@begin}{genfrac}{\:tempc}
     \Configure{@begin}{measure@}{\:tempc}
     \Configure{@begin}{multline*}{\:tempc}
     \Configure{@begin}{multline}{\:tempc}
     \Configure{@begin}{overset}{\:tempc}
     \Configure{@begin}{smallmatrix}{\:tempc}
     \Configure{@begin}{split}{\:tempc}
     \Configure{@begin}{subarray}{\:tempc}
     \Configure{@begin}{substack}{\:tempc}
     \Configure{@begin}{underset}{\:tempc}
     \Configure{@begin}{xleftarrow}{\:tempc}
     \Configure{@begin}{xrightarrow}{\:tempc}

    %\:maketemp{equation}
    %\:maketemp{equation*}
  }{}
}

\let\T:ref=\::ref
\def\::ref{\protect\T@ref}
\def\T@ref#1{%
  \@safe@activestrue
  \let\::ref \T:ref
  \expandafter\@setref\csname r@#1\endcsname\@firstoffive{#1}%
  \def\::ref{\protect\T@ref}%
  \@safe@activesfalse
}

\gdef\defineautorefname#1#2{%
    \expandafter\gdef\csname #1autorefname\endcsname{#2}}
\defineautorefname{theorem}{Theorem}

\Configure{newlabel}
   {\csname cur:th\endcsname \csname :currentlabel\endcsname}
   {\string\csname\space :autoref\string\endcsname
     {\NR:Type}#1}

\ifx \@currentlabelname\:UnDef
   \let\@currentlabelname\empty
\fi

\pend:defIII\@setref{\edef\RefArg{##3}}
\append:defIII\@setref{\let\:autoref\:gobble}
\let\:autoref\:gobble


\Hinput{nameref}
\endinput

您还需要更新您的 TeX 文件:

\documentclass[a4paper, 12pt, ngerman]{scrartcl}

\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenx}

\usepackage{lmodern}

\usepackage[a4paper, left=30mm, right=25mm, top=25mm, bottom=30mm]{geometry}
\usepackage{setspace}
\onehalfspacing
\parindent0mm

\usepackage{amsmath}
\usepackage{commath}
\usepackage{units}
%% Bilder einbinden
\usepackage{graphicx}
%% zum korrekten Export der Vektorgrafiken als SVG
\ifdefined\HCode
\def\pgfsysdriver{pgfsys-tex4ht.def}
\fi
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\renewcommand{\dpd}[3][]{\ensuremath{\mathinner{%
      \dfrac{\partial{{}^{#1}}#2}{\partial{{#3}^{#1}}}
}}}
\renewcommand\dif{\mathop{d}\!}

\usepackage[breaklinks, colorlinks]{hyperref}
\usetikzlibrary{external}    
\tikzset{
 external/system call/.add={}                                                
      {; inkscape -z -f "\image.pdf" -l "\image.svg" }    
}
\makeatletter
\@ifpackageloaded{tex4ht}{
}{
    \tikzexternalize
}
\makeatother

\begin{document}

Erstmal etwas inline-Mathe $E = m c^2$, etwas sinnloser Text und dann eine lange, abgesetzte Formel:
\begin{multline}
\iiint_V \dpd{E}{t} \dif V + \iint_S E u_i n_i \dif S = \\
\iint_S \tau_{ij} u_i n_j \dif S - \iiint_V \dpd{p u_i}{x_i} \dif V + 
\iiint_V \rho f_i u_i \dif V - \iint_S q_i n_i \dif S \label{eq:energie}
\end{multline}

\autoref{eq:energie}
ist ein gutes Beispiel für eine lange und komplizierte Gleichung, die nicht mehr auf eine Zeile passt.

\autoref{fig:spz}
zeigt eigentlich nichts Sinnvolles, genauer gesagt einen beliebigen Funktionsverlauf mit $\rho$ = \unitfrac[999]{kg}{m\textsuperscript{3}} und $u_1$ = \unitfrac[2]{m}{s}.

\begin{figure}[htb]
\centering
\begin{tikzpicture}
\begin{axis}[width=0.8\textwidth,
height=0.3\textheight,
scale only axis,
xmin=0,
xmax=1,
xlabel={Dimensionless number $\zeta$ (-)},
xlabel style={fill=white,fill opacity=0.9},
ymin=0,
ymax=2000,
ylabel={Resulting pressure $\Sigma p$ (bar)},
ylabel style={fill=white,fill opacity=0.9},
axis background/.style={fill=white},
legend style={at={(0.71,0.96)},anchor=north west,draw=black,fill=white,legend cell align=left}
]
%% Formula 1
\addplot[blue, domain=0:1, samples=101]
{1998*(1 - x^2)};
\addlegendentry{Formula 1};
\end{axis}
\end{tikzpicture}
\caption{Resulting pressure $\Sigma p$ as a function of $\zeta$ \label{fig:spz}}
\end{figure}

\end{document}

这些变化主要是 Tikz 外部化所需的代码,但也修复了 Commath 包提供的命令:

\renewcommand{\dpd}[3][]{\ensuremath{\mathinner{%
      \dfrac{\partial{{}^{#1}}#2}{\partial{{#3}^{#1}}}
}}}
\renewcommand\dif[1]{\mathop{#1}\!}

原始版本生成了错误的 Mathml 代码,因为数学运算符中的上标或间距命令缺少或不正确。这个包已经很长时间没有更新了,我猜它提供的其他命令也会有问题。

您需要做的最后一件事是为 tex4ht 创建一个配置文件mycfg.cfg

\tikzset{
    tex4ht inc/.style={
        /pgf/images/include external/.code={%
            \includegraphics[]{##1.svg}%
        }

    }
}
\tikzexternalize[mode=only graphics]  
\tikzset{tex4ht inc}
\Preamble{xhtml,mathml}
\Configure{@HEAD}{\HCode{<script type="text/javascript"
   src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=MML_HTMLorMML">
</script>
}}
\makeatletter
\Configure{graphics*}  
         {svg}{  
          {\Configure{Needs}{File: \[email protected]}\Needs{}}
          \Picture[\csname a:GraphicsAlt\endcsname]{\csname Gin@base\endcsname.svg \csname a:Gin-dim\endcsname}%  
         }  
\makeatother
\begin{document}
\EndPreamble

我认为我应该描述一下这里发生的事情:

\tikzset{
    tex4ht inc/.style={
        /pgf/images/include external/.code={%
            \includegraphics[]{##1.svg}%
        }

    }
}
\tikzexternalize[mode=only graphics]  
\tikzset{tex4ht inc}

声明只应包含 SVG 图像,无需进行任何转换。使用 pdflatex 编译文档时应创建 SVG 文件。

\Preamble{xhtml,mathml}
\Configure{@HEAD}{\HCode{<script type="text/javascript"
   src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=MML_HTMLorMML">
</script>
}}

这将生成 MathML 格式的数学运算,并包含 Mathjax 库来修复 Web 浏览器中的 Mathml 问题。

  \Configure{graphics*}  
         {svg}{  
          {\Configure{Needs}{File: \[email protected]}\Needs{}}
          \Picture[\csname a:GraphicsAlt\endcsname]{\csname Gin@base\endcsname.svg \csname a:Gin-dim\endcsname}%  
         }  

这个配置tex4ht支持包含 SVG 图像,我还将此代码包含到tex4ht核心中,因此如果您保持 TeX 发行版更新,很快就不需要使用此配置了。

现在,为了获取 SVG 图像,您需要pdflatex首先使用以下命令编译您的文档,并启用调用外部命令:

 pdflatex -shell-escape filename.tex

外部化为每个 Tikz 图片创建 PDF 图像,然后使用 Inkscape 将其转换为 SVG,因此您需要先安装它。

然后您可以使用以下命令编译您的文档tex4ht

 make4ht -uc mycfg.cfg filename.tex

结果如下:

在此处输入图片描述

相关内容