# What are the limitations of using tikz with tex4ht?

As I was trying tikz with tex4ht, I found some small pics are not generated correctly, which means they do not show the same as when compiled using lualatex. Here is one such example

## Example 1

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{quotes,angles,calc}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\Tree [.A
[.\node(B){B}; ]
[.\node(C){C}; ]
]
\begin{scope}[xshift=0in,yshift=-2cm]
\Tree [.\node(Dx){D};
[.E ]
[.F ]
]
\end{scope}

\draw[-] (B) -- (Dx);
\draw[-] (C) -- (Dx);
\end{tikzpicture}
\end{document}


When compiled using lualatex foo3.tex it gives this PDF

But when compiled using

   make4ht -ulm default -a debug foo3.tex "htm,mathjax"


The HTML page shows this

## Example 2

Thanks to code posted in comment below by Schrödinger's cat, I tried that example and it works in lualatex, but in tex4ht, the letters are missing even though the tree branches show up.

\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document} \begin{tikzpicture}[level distance=1cm,sibling distance=1cm]
\node {A} child {node (B){B}} child {node (C){C}};
\node[yshift=-2cm] (D) {D} child {node {E}} child {node {F}};
\draw (B) -- (D) (C) -- (D);
\end{tikzpicture}
\end{document}


Using the same code to compile as above, here it is in PDF

And this is in HTML

I know there is supposed to be some limitations to using tikz with tex4ht (and it is great that it even works on some examples).

But was wondering if it is possible to get a more clear idea what these limitations are and what to expect, to avoid having to do lots of trial and errors to see if something will work or not in tex4ht.

Using TL 2020

• One guess might be that it does see the plain TikZ commands but not the tikz-qtree stuff. You do not need tikz-qtree to produce the above tree, so maybe you want to try without. Actually, this is what I was about to do but on my installation  make4ht -ulm default -a debug foo3.tex "htm,mathjax" only lead to some messages that are cryptic even by TeX standards. Yet it may make sense to go in smaller steps, i.e. use "just" TikZ before trying to use arguably more hacky packages like tikz-qtree.
– user194703
May 13 '20 at 3:08
• @Schrödinger'scat it works with simple tikz pics. yes. if this is what you mean. May 13 '20 at 3:12
• Sure, you cannot use tikz-qtree syntax if you are not loading it. What I wanted to say is that you can produce the tree in TikZ, but of course with the appropriate syntax.
– user194703
May 13 '20 at 3:13
• This code produces your tree \documentclass[12pt]{article} \usepackage{tikz} \begin{document} \begin{tikzpicture}[level distance=1cm,sibling distance=1cm] \node {A} child {node (B){B}} child {node (C){C}}; \node[yshift=-2cm] (D) {D} child {node {E}} child {node {F}}; \draw (B) -- (D) (C) -- (D); \end{tikzpicture} \end{document}. Without tikz-qtree.
– user194703
May 13 '20 at 3:20
• I understand your question but you are waiting for users who know TikZ and make4ht very well, I do not know how many such users exist and are active on this site. All I was trying to do is to test the idea that TikZ works largely but not special variations like tikz-qtree which do some interesting tricks. In your output the stuff with plain TikZ seems to appear but not what's generated by tikz-qtree.
– user194703
May 13 '20 at 3:23

There are often issues with text nodes in the default tex4ht driver for TikZ. There are two options to fix this. Either you can try to set the pgf/tex4ht node/escape option for TikZ, or use the alternative driver that uses dvisvgm for the conversion.

The first option is described as follows in the PGF manual:

Selects the rendering method for a text node with the tex4ht driver. When this key is set to false, text is translated P R into svg text, which is somewhat limited: simple characters (letters, numerals, punctuation, , , ...), subscripts and superscripts (but not subsub- scripts) will display but everything else will be filtered out, ignored or will produce invalid html code (in the worst case). This means that two kind of texts render reasonably well:

1. First, plain text without math mode, special characters or anything else special.
2. Second, very simple mathematical text that contains subscripts or superscripts. Even then, variables are not correctly set in italics and, in general, text simple does not look very nice.

If you use text that contains anything special, even something as simple as $\alpha$, this may corrupt the graphic.

\tikz \node[draw,/pgf/tex4ht node/escape=false] {Example : $(a+b)^2=a^2+2ab+b^2$};

When you write node[/pgf/tex4ht node/escape=true] {htexti}, pgf escapes back to html to render the htexti. This method produces valid html code in most cases and the support for complicated text nodes is much better since code that renders well outside a {pgfpicture}, should also render well inside a text node. Another advantage is that inside text nodes with fixed width, html will produce line breaks for long lines. On the other hand, you need a browser with good svg support to display the picture. Also, the text will display differently, depending on your browsers, the fonts you have on your system and your settings. Finally, pgf has to guess the size of the text rendered by the browser to scale it and prevent it from sticking from the node. When it fails, the text will be either cropped or too small.

You can set the option in the Tex4ht config file:

\Preamble{xhtml}
\tikzset{every node/.style={/pgf/tex4ht node/escape=true}}
\begin{document}
\EndPreamble


This method doesn't work with your first example, but the second example works:

The second method is to use the alternative TikZ driver. Add the following lines before \usepackage{tikz}:

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


It works for both examples and produces better results in general, so I would prefer it.

The first example rendered:

• Thanks Michal for the additional info, Do you know why when I have tex4ht.env in same folder, then it does not work any more. i.e. the webpage do not show the tikz images. This was another problem I had. I had old tex4ht.env in same folder I was testing tex4ht with tikz and things were not working ok. When I noticed this file and deleted it, now all images show OK when I compile. I had no idea that make4ht reads this file if it is in local folder. This was causing it to not generate .svg images and so the HTML page could not find them. May 13 '20 at 13:16
• @Nasser the tex4ht.env can specify commands used for the image conversion, so your old version probably contained something that doesn't work anymore. I think there used to be quite complicated command to get SVG images, it often failed. so you definitely don't want any old copies of this file anywhere. May 13 '20 at 13:22
• Sure. I removed this file and now it works. But I am not able to make your \tikzset{every node/.style={/pgf/tex4ht node/escape=true}} to work. I get error ! Package pgfkeys Error: I do not know the key '/pgf/tex4ht node/escape', to wh ich you passed 'true', and I am going to ignore it. Perhaps you misspelled it. I compiled using make4ht -ulm default -a debug -c ./nma_mathjax.cfg html_and_latex.tex "htm,mathjax" and I put the latex file and .cfg file in this folder for you 12000.org/tmp/051320 why it works for you and not for me? I am using TL 2020 May 13 '20 at 13:32
• It looks like you have different version of files or something. I did this from clean folder and only have the latex and the .cfg files in there as you can see. I also noticed if I remove the lines \ifdefined\HCode \def\pgfsysdriver{pgfsys-dvisvgm4ht.def} \fi  then the error goes away, but it does not display correct any more. May 13 '20 at 13:34
• @Nasser tex4ht node/escape doesn't work with dvisgm4ht.def driver. May 13 '20 at 13:38

I am sorry. All these problems where due to user error. My wrong use of \def\pgfsysdriver{pgfsys-dvisvgm4ht.def}

One year ago, I downloaded copy of pgfsys-dvisvgm4ht.def and put it in local texmf under \$HOME/texmf/tex/latex/dvisvgm4ht/

ls -lrt
total 28
-rw-rw-rw- 1 me me 17762 May  2  2019 pgfsys-tex4ht-updated.def
-rw-rw-rw- 1 me me  1734 May  2  2019 pgfsys-dvisvgm4ht.def
-rw-rw-rw- 1 me me  1623 May  2  2019 README.md


Now with TL 2020 out, it seems a better pgfsys-dvisvgm4ht.def was included in it, but my old pgfsys-dvisvgm4ht.def was still being used and I all forget that I had old one there from a year ago.

Now I deleted the one in my local texmf completely, and now tex4ht works. However, I had to put back the following in my latex file for it to work

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


without it, it will not work right.

So here is the updated results for example 1 and 2 again. Example 2 now works OK. Example 1 works better than before, but still has some issues, but this is OK, since example 1 uses external tikz-qtree package which looks like tex4ht does not yet supports well. But at least the basic Tikz seems to work now.

## Example 1

\documentclass[12pt]{article}
\ifdefined\HCode
\def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi

\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{quotes,angles,calc}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\Tree [.A
[.\node(B){B}; ]
[.\node(C){C}; ]
]
\begin{scope}[xshift=0in,yshift=-2cm]
\Tree [.\node(Dx){D};
[.E ]
[.F ]
]
\end{scope}

\draw[-] (B) -- (Dx);
\draw[-] (C) -- (Dx);
\end{tikzpicture}
\end{document}


Compile with

  make4ht -ulm default -a debug foo3.tex "htm,mathjax"


## Example 2

\documentclass[12pt]{article}
\ifdefined\HCode
\def\pgfsysdriver{pgfsys-dvisvgm4ht.def}
\fi

\usepackage{tikz}
\begin{document} \begin{tikzpicture}[level distance=1cm,sibling distance=1cm]
\node {A} child {node (B){B}} child {node (C){C}};
\node[yshift=-2cm] (D) {D} child {node {E}} child {node {F}};
\draw (B) -- (D) (C) -- (D);
\end{tikzpicture}
\end{document}


Compile with

  make4ht -ulm default -a debug foo3.tex "htm,mathjax"


gives