I found another problem with math when using tex4ht and tikz. This is similar/followup to earlier question no math is generated when using tikz/forest with tex4ht in mathjax mode but since this affects now tikzpicture and not forest I thought it will be better to make new question instead of cluttering the old one?

Here is the problem. With the fix given in the above question now math shows up when using forest but when using tikzpicture all the math is missing.

In addition, when using \left or \right even in forest it does not render correctly in HTML.

So there are big problems when it comes to using math with tex4ht and tikz. May be it is due to using mathjax mode. But that is really the only mode I use tex4ht with now.

Here is MWE


for tree={
    draw, rounded corners, fill=blue!20,
    minimum height=1.5cm, minimum width=2cm,
    align=center, base=b,
    s sep=1cm, l sep=.5cm,
    if level<=2{edge=-latex}{edge=red},
first order
       y'&=\left(a + b x + y\right)^{\frac{1}{n}}

\node [rectangle, minimum width=14cm]  (A) 
first order
y'&=\left(a + b x + y\right)^{\frac{1}{n}}


When compiling using lualatex to pdf, this is the output

enter image description here

When compiled with make4ht (need to use .cfg below)

make4ht  -ulm default -a debug -c mycfg.cfg foo.tex "mathjax,htm" "-cunihtf -utf8"

enter image description here

Here is the .cfg file thanks to michal.h21 from the above link

  {\start@align \@ne \st@rredtrue \m@ne}
  {\math@cr \black@ \totwidth@ \egroup \ifingather@ \restorealignstate@ \egroup \nonumber \ifnum 0=`{\fi \iffalse }\fi \else $$\fi \ignorespacesafterend}

Using TL 2022 on Linux

>which tex4ht
>make4ht -v
make4ht version v0.3l
  • michael.h21's cfg file only patches forest, doesn't it? Either \AddToHook{env/tikzpicture/begin} this too or try \tikzset{every picture/.append code={\renewenvironment{align*}{\start@align \@ne \st@rredtrue \m@ne}{\math@cr \black@ \totwidth@ \egroup \ifingather@ \restorealignstate@ \egroup \nonumber \ifnum 0=`{\fi \iffalse }\fi \else $$\fi \ignorespacesafterend}} (TikZ's own hook that gets executed at the start of the picture.) Commented Nov 28, 2022 at 9:28

1 Answer 1


The problem is the same as in the previous question. We need to restore the original version of the environment in TikZ picture. I've made a macro that saves environments before we redefine them for MathJax. You can then restore the original meaning using the \RestoreMathJaxEnvironments command. You need this version of mathjax-latex-4ht.4ht:

% mathjax-latex-4ht.4ht (2022-10-17-13:57), generated from tex4ht-mathjax.tex
% Copyright 2018-2022 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".
% The Current Maintainer of this work
% is the TeX4ht Project <http://tug.org/tex4ht>.
% If you modify this program, changing the
% version identification would be appreciated.
\immediate\write-1{version 2022-10-17-13:57}

\cs_new_protected:Npn \alteqtoks #1
  \tl_set:Nx \l_tmpa_tl {\detokenize{#1}}
  % % replace < > and & with xml entities
  \regex_replace_all:nnN { \x{26} } { &amp; } \l_tmpa_tl
  \regex_replace_all:nnN { \x{3C} } { &lt; } \l_tmpa_tl
  \regex_replace_all:nnN { \x{3E} } { &gt; } \l_tmpa_tl
  % replace \par command with blank lines
  \regex_replace_all:nnN { \x{5C}par\b } {\x{A}\x{A}} \l_tmpa_tl
  \tl_set:Nx \l_tmpb_tl{ \l_tmpa_tl }


% this seems a bit hacky -- we need to skip some code inserted at the
% beginning of each display math



  \expandafter\let\csname mathjax-#1\expandafter\endcsname\csname #1\endcsname
  \expandafter\let\csname mathjax-end#1\expandafter\endcsname\csname end#1\endcsname

  \expandafter\let\csname #1\expandafter\endcsname\csname mathjax-#1\endcsname%
  \expandafter\let\csname end#1\expandafter\endcsname\csname mathjax-end#1\endcsname%


\cs_generate_variant:Nn \regex_extract_once:nnNTF {nV}
      \regex_extract_once:nVNTF { label\s* \x{7B}([^\x{7D}]*)\x{7D}} {\l_tmpb_tl} \l_tmp_seq {\label{\seq_item:Nn\l_tmp_seq{2}}} {}%




It can be required for all TikZ environments using this configuration (thanks to @Qrrbrbirlbel for the inspiration):

\tikzset{every picture/.append code={\RestoreMathJaxEnvironments}}

And this is the result:

enter image description here

  • There are a lot of empty lines in your first code block (and two \endinputs). Did something go wrong or is it just a paste error? Commented Nov 28, 2022 at 18:19
  • @Qrrbrbirlbel this is just an artifact created by the literate programming system that is used to generate the .4ht files. In this case there are too many of them, so I've updated my answer and removed unnecessary lines
    – michal.h21
    Commented Nov 28, 2022 at 18:48
  • Thanks Michal. I'll test this more as soon as I can. Will next update of TL has the above newer version of mathjax-latex-4ht.4ht and I just need to add \tikzset{every picture/.append code={\RestoreMathJaxEnvironments}} to my .cfg?
    – Nasser
    Commented Nov 29, 2022 at 5:04
  • 1
    @Nasser the next update of TL should have this change, and in addition, the code for TikZ environments is executed too, when you load TikZ in your preamble.
    – michal.h21
    Commented Nov 29, 2022 at 9:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .