2

While getting started with tex4ht, I noticed a couple of issues. The following MWE compiles fine with 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" produces an error in the image part, which is resolved applying this fix to the buggy pgfsys-tex4ht.def, which still seems to be unfixed in TexLive 2016!!!

The next issue arises with \autoref. If I comment out the two lines (lines 37 and 40) containing it, the HTML-conversion runs fine. But with them, htlatex says Undefined control sequence ... \autoref. Is this a known bug? Is there a workaround? Or is this related to the language settings?

Also, two SVG files are produced, a correct one without legend text (file-1.svg) and an apparently empty one (file-2.svg). The latter is embedded, so the image space stays blank. If I manually edit file.html to embed file-1.svg, it works except the legend is not shown (empty white rectangle without text). Any help is appreciated.

EDIT With the changes suggested by @michal.h21's amazingly detailed answer, the hyperref issue is solved. However, the SVG is not generated. The log file says:

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

Also, the integrals look much bigger and bolder than the other variables. And the ordinary differential operator d is missing completely. Can this be fixed somehow?

EDIT2 I did use the -shell-escape option and have Inkscape installed, but from the log file, it seems inkscape is not even called! I will try on another machine.

The math rendering issue as a new question here, since the original hyperref issue is solved.

  • you need to compile the file with pdflatex -shell-escape filename.tex to get the svg file. The missing operator could be an issue with commath package, it seems that it generates invalid mathml code. – michal.h21 Jan 11 '17 at 13:56
  • 1
    I've found that the d operator was missing due to wrong definition of \dif macro. Please see the updated answer. – michal.h21 Jan 11 '17 at 22:56
  • Good, I've made another edit with the new issues. Btw, should I move this to a new question, since this is more related to math rendering than to hyperref? – rotton Jan 12 '17 at 14:30
  • I think it would be better to post it as a new question, it would get lost if it was just an edit – michal.h21 Jan 12 '17 at 16:25
  • Done!. – rotton Jan 13 '17 at 15:28
4

I would personally use Tikz externalization to get the SVG images, pgfsys-tex4ht.def driver has more drawbacks. Externalization required some small changes to your document, see this answer of mine for more details.

The issue with \autoref is caused by a bug in tex4ht support for Hyperref package. It checked the \label type incorrectly, which caused compilation error if the label type isn't set, which is the case of Amsmath environments. I've fixed this issue and added label types for Amsmath environments, so your example should work correctly now. Please save the following code as nameref.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 <tex4ht@tug.org>.                %
%                                                        %
% If you modify this program your changing its signature %
% with a directive of the following form will be         %
% appreciated.                                           %
%            \message{signature}                         %
%                                                        %
%                                        tex4ht@tug.org  %
%                             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 <kandrews@iicm.edu> 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

You need also to update your TeX file:

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

The changes are mainly the code needed for Tikz externalization, but there are also fixes for commands provided by Commath package:

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

The original ones produced wrong Mathml code, because there were missing or incorrect bases for superscripts or spacing commands inside the math operator. This package is quite a long time without updates, I guess that there will be issues also with other commands it provides.

The last thing which you need is to create a configuration file for 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: \Gin@base.svg}\Needs{}}
          \Picture[\csname a:GraphicsAlt\endcsname]{\csname Gin@base\endcsname.svg \csname a:Gin-dim\endcsname}%  
         }  
\makeatother
\begin{document}
\EndPreamble

I think that I should describe what is going on here:

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

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

declares that only SVG images should be included, without any conversion. The SVG files should be created when you compile the document with pdflatex.

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

This will produce math in MathML format and include Mathjax library to fix the Mathml issues in web browsers.

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

this configures tex4ht to support inclusion of SVG images, I included this code also to tex4ht core, so it will be soon unnecessary to use this configuration, if you keep your TeX distribution updated.

Now, in order to get the SVG images, you need to compile your document using pdflatex first, with calling to external commands enabled:

 pdflatex -shell-escape filename.tex

Externalization created PDF image for each Tikz picture, shich is then converted to SVG using Inkscape, so you need to install it first.

Then you can compile your document using tex4ht:

 make4ht -uc mycfg.cfg filename.tex

And this is the result:

enter image description here

| improve this answer | |

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.