2

I am trying to compile some minimal test.tex using htlatex test.tex "xhtml,svg", but the SVG generated from tikz pictures does not seem to work.

\documentclass[11pt]{article}

\usepackage{tikz}

\title{Testing}
\begin{document}

\maketitle

Inline equation \(X_1 + X_2 = Y\), equation
\[\int^{X \in \mathbf{C}} \mathbf{C}(X,X).
\]

Tikz picture.

\begin{tikzpicture}
  \draw [blue,fill] (0,0) rectangle (3,3);
  \draw [red,fill] (0,0) rectangle (3,2);
\end{tikzpicture}

\end{document}

The svg file for the tikz picture seems to be generated in a different way from the other SVGs (it has a different name scheme from the others, for instance), and it does not display due to the following error.

XML Parsing Error: mismatched tag. Expected: </g>.
Location: file:///home/mario/testing-htlatex/test-1.svg
Line Number 5, Column 3:
</p><g stroke="#000" fill="#000">

Indeed, removing that conflicting <p> makes the SVG display again. Is this a bug or am I missing something? Did I do something wrong that added that <p> to the tikz picture? Why is the tikz picture generated differently?

What puzzles me the most is that, if I decide to try without svg, using just htlatex test.tex, then a correct SVG for the tikz picture gets generated (!).

1
  • Oh, so it works if I enclose the tikzpicture with \[...\]. Was this intended behaviour? May 18 '20 at 18:18
2

By default, TikZ uses special driver for TeX4ht to create the SVG file. It maps the low-level PGF command to SVG, so it doesn't use the mechanism TeX4ht uses for the conversion of LaTeX code to SVG. Sometimes this method fails. You can use the alternative TikZ driver for TeX4ht. It uses dvisvgm for the conversion to SVG. It results in better results in most cases (but there are cases where it fails completely). It seems to work nice for your example:

\documentclass[11pt]{article}

\ifdefined\HCode
   \def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi

\usepackage{tikz}

\title{Testing}
\begin{document}

\maketitle

Inline equation \(X_1 + X_2 = Y\), equation
\[\int^{X \in \mathbf{C}} \mathbf{C}(X,X).
\]

Tikz picture.

\begin{tikzpicture}
  \draw [blue,fill] (0,0) rectangle (3,3);
  \draw [red,fill] (0,0) rectangle (3,2);
\end{tikzpicture}

\end{document}

You may also want to use make4ht instead of htlatex. It fixes lot of issues that htlatex has and provides additional features.

This is the result when I compile your document using

make4ht filename.tex "svg"

enter image description here

2
  • I see. I must be doing something wrong because your exact code (having downloaded the alternative driver, also using make4ht) generates a blank svg in place of the tikzpicture (maybe this is a problem of dvisvgm?). In any case, putting every tikz inside an equation works perfectly. Thank you, @michal.h21 May 19 '20 at 6:58
  • 1
    @MarioRomán you should see some info from dvisvgm in the terminal output. maybe there are some error messages?
    – michal.h21
    May 19 '20 at 8:14

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.