Error using htlatex with tikz forest package, invalid SVG generated

htlatex is supposed to support tikz with \def\pgfsysdriver{pgfsys-tex4ht.def} and it does on some tikz examples, but for some other examples, it gives bad svg images.

UPDATE

I was able to reduce the problem to a simpler example. I left the orginal question as appendix. But this is simpler MWE. The problem shows up when using math inside tikz, and only when math is a little complicated, like fraction. Here is an example that work with htlatex:

\documentclass{article}
\ifdefined\HCode
\def\pgfsysdriver{pgfsys-tex4ht.def}
\fi
\usepackage{tikz,graphicx}
%
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}
\node {root}
child {node {$a=b$}};
\end{tikzpicture}
\end{document}

then htlatex foo.tex and this is the HTML:

So, it works. Now I changed the above, only changed the math from $a=b$ to $\frac{a}{b}$ and now it failed

\documentclass{article}
\ifdefined\HCode
\def\pgfsysdriver{pgfsys-tex4ht.def}
\fi
\usepackage{tikz,graphicx}
%
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}
\node {root}
child {node {$\frac{a}{b}$}};
\end{tikzpicture}
\end{document}

then htlatex foo.tex gives error

This is the HTML file generated by htlatex:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html >
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="generator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)">
<meta name="originator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)">
<!-- html -->
<meta name="src" content="t4.tex">
<meta name="date" content="2013-07-19 08:58:00">
>
<!--l. 12--><p class="noindent" ><object data="t4-1.svg" width="33.99898 " height="77.82672 " type="image/svg+xml"><p>SVG-Viewer needed.</p></object>

</body></html>

And this below is the content of the SVG file generated for the above case where it failed which is referenced in the above HTML

<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
<?xml-stylesheet href="t4.css" type="text/css"?>
<g transform="translate(0, 6.40837 ) scale(1,-1) translate(0,51.2119 )">
<g>
<g stroke="rgb(0.0%,0.0%,0.0%)">
<g fill="rgb(0.0%,0.0%,0.0%)">
<g stroke-width="0.4pt">
<g>
<g>
<g>
<g transform="matrix(1.0,0.0,0.0,1.0,-9.04169,-3.0754)">
<g fill="rgb(0.0%,0.0%,0.0%)">
<text style="stroke:none" transform="scale(1,-1) translate(9.04169,0)" text-anchor="middle"  font-size="10">
root</text>
</g>
</g>
</g>
</g>
<g>
<g>
<g>
<g transform="matrix(1.0,0.0,0.0,1.0,-3.3688,-44.43053)">
<g fill="rgb(0.0%,0.0%,0.0%)">
<text style="stroke:none" transform="scale(1,-1) translate(3.36882,0)" text-anchor="middle"  font-size="10">
<img
src="t40x.png" alt="ab"  class="frac" align="middle"></text>
</g>
</g>
</g>
</g>
<path d=" M  0.0 -6.60837 L  0.00002 -33.94635  " style="fill:none"/>
</g>
</g>
</g>
</g>
</g>
</g>

</g>
</svg>

The strange thing, is that the file t40x.png which is referenced from inside the svg file is actually there and does show the fraction fine. So the problem is with the SVG file?

Here is the t40x.png

So, the math was generated OK, it seems to be an SVG issue or something like this.

I hope the above helps shed light on where the problem is.

Update july 19, 2013 5 pm

The problem boils down to parsing error in the above SVG. When verifying the XML code in the SVG file above, using http://www.w3schools.com/xml/xml_validator.asp, it gives this error

So the question is: What can be done to fix such errors? WHy is this error showing up? Anything one can do to post process these .svg files and fix them using some sort of script (perl, sed, etc..) until a permanent fix is found so that one can use htlatex to generate tikz pictures with math in them?

update july 19, 2013 7 pm It was suggested in the chat to try mathjax with htlatex to see if that will make it display the math with tikz picture. I tried and it did not have an effect.

Instead of posting things here again, I'll just post a link to my Latex cheat sheet where these are documented here

If anyone else has another suggestion for me to try, I'll be happy to do it. I would like to be able to make simple diagrams using tikz with math in the labels but want to convert these to html as well not just pdf.

Appendix (original question)

Here is an example where it works with tikz

\documentclass{article}
\ifdefined\HCode
\def\pgfsysdriver{pgfsys-tex4ht.def}
\fi
\usepackage{tikz,graphicx}

\begin{document}

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

compiling with htlatex foo.tex gives a web page which shows the picture OK.

But with this example, using forest package, the web page gives an error

\documentclass{article}
\ifdefined\HCode
\def\pgfsysdriver{pgfsys-tex4ht.def}
\fi
\usepackage{tikz,graphicx}
\usepackage{forest}
\begin{document}

\begin{forest} for tree={draw,align=center}
[second order partial differential equation
[elliptic
[Laplace]
[Poisson]
]
[parabolic]
[hyperbolic]
]
\end{forest}

\end{document}

compiling with htlatex foo.tex then loading the html file gives the error

Why does one get an error from some tikz examples and not from others?

I tried BOTH solutions given in this answer Htlatex and Tikz creates sometimes incorrect svgs (the accepted one and the one before it). But could not get an image using either one. Here I show the first solution given

\documentclass{article}
\usepackage{tikz,graphicx}
\usetikzlibrary{external}
\makeatletter
\tikzexternalize[mode=only graphics]
}{
\tikzexternalize
}
\makeatother

\tikzset{
png export/.style={
/pgf/images/external info,
/pgf/images/include external/.code={%
\includegraphics
[width=\pgfexternalwidth,height=\pgfexternalheight]
{##1.png}%
}
}
}

\tikzset{png export}
\usepackage{forest}

\begin{document}

\begin{forest} for tree={draw,align=center}
[second order partial differential equation
[elliptic
[Laplace]
[Poisson]
]
[parabolic]
[hyperbolic]
]
\end{forest}

\end{document}

Then run the same sequence of commands shown:

pdflatex t3 -shell-escape
for i in *.pdf; do convert -density 300 $i echo$i | sed -e 's/\.pdf/\.png/g'; done
htlatex t3.tex

but the HTML now just showed the text, no lines and no graphics:

It should look like this (in pdf)

How can I get the package forest to work with htlatex?

I am using TL 2013, and updated all packages. On Linux mint 15.

-
I think this is the same issue as: tex.stackexchange.com/q/121944/2891 –  michal.h21 Jul 19 '13 at 14:39
@michal.h21 I see. I did try the command you showed there, which is htlatex t4.tex "xhtml, mathml" and opened the html file in firefox. But the fraction did not display at all. Only the text and the vertical line. So no math showed. Does this mean one can't use tikz with complicated math and have it work with htlatex? thanks –  Nasser Jul 19 '13 at 14:49
@Nasser: The error in your SVG XML is that <col ...> is not properly closed. I have manually corrected the svg and viewed it in browser. Only the rules appeared, but no text. Can we convert this to image (i.e. the content inside \begin{forest} ... \end{forest})? –  Jagath AR Jul 19 '13 at 14:53

Thanks to everyone's input. Here is the work around that was suggested to me to use and I thought to document it here.

Even though this is a workaround, it is a very good work around. The idea is to use a separate latex file to make the diagram(s) using tikz+math using standalone class.

Hence, there will be one latex file for each one diagram.

Then generate the pdf file using pdflatex.

Next these pdf file(s) are converted to png. Using a separate latex file, this png is included where it needs to be. This way htlatex and pdflatex will be able to process the final document ok.

Here are the steps.

1) make separate latex file for each diagram. For example diagram.tex

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}
\node {root}
child {node {$\frac{a}{b}$}};
\end{tikzpicture}
\end{document}

2) compile the above file to pdf

pdflatex diagram.tex

3) convert the pdf file to png

pdftoppm -png diagram.pdf > diagram.png

4) create the latex file which will use the above diagram, say main.tex

\documentclass{article}
\usepackage{graphicx}

\begin{document}

The following diagram was generated using tikz+latex in
a separate file

\includegraphics{diagram.png}

and it is now in html
\end{document}

5) now make pdf and html files from the above

pdflatex main.tex
htlatex main.tex

6) now the files main.pdf and main.html can be used.

Thanks again for everyone's help. I will use this method from now one.

-
you can also try to process your diagrams with latex and resulting dvi file with dvisgm –  michal.h21 Jul 20 '13 at 9:06