2

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

\documentclass[11pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{amsmath}
\usepackage{tikz,tikzpagenodes}
\usepackage{etoolbox}
\usepackage{hyperref}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{positioning,fit}    
\usepackage[edges]{forest}
\begin{document}

\begin{forest}
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},
}
[\begin{minipage}{2cm}     
first order
{\begin{align*} 
       f(x,y,(y')^n)&=0\\
       y'&=\left(a + b x + y\right)^{\frac{1}{n}}
\end{align*} 
}
\end{minipage}
,calign=last
]
\end{forest}


\begin{tikzpicture}
\node [rectangle, minimum width=14cm]  (A) 
{
\begin{minipage}{2cm}     
first order
\begin{align*} 
f(x,y,(y')^n)&=0\\
y'&=\left(a + b x + y\right)^{\frac{1}{n}}
\end{align*} 
\end{minipage}
};
\end{tikzpicture}

\end{document}

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

\Preamble{xhtml}
\makeatletter
\AddToHook{env/forest/begin}{%
  \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}
}
\makeatother
\begin{document}
\EndPreamble

Using TL 2022 on Linux

>which tex4ht
/usr/local/texlive/2022/bin/x86_64-linux/tex4ht
>make4ht -v
make4ht version v0.3l
>
1
  • 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

2

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}


\ExplSyntaxOn
\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 }
  \HCode{\l_tmpb_tl}
}
\ExplSyntaxOff

\NewConfigure{MathJaxConfig}{1}
\NewConfigure{MathJaxMacros}[1]{%
\Configure{@BODY}{\bgroup\NoFonts\ttfamily\detokenize{\(}%
  \special{t4ht*<#1}%
\detokenize{\)}\EndNoFonts\egroup}%
}

\long\def\AltlMath#1\){\expandafter\alteqtoks{\(#1\)}\)}
\long\def\AltlDisplay#1\]{\alteqtoks{\[#1\]}\]}
\long\def\AltMathOne#1${\alteqtoks{\(#1\)}$}
% this seems a bit hacky -- we need to skip some code inserted at the
% beginning of each display math
\long\def\AltlDisplayDollars#1$${\alteqtoks{\[#1\]}$$}

\newcommand\VerbMathToks[2]{%
  \alteqtoks{\begin{#2}
    #1
  \end{#2}}%
}
\ExplSyntaxOn

\seq_new:N\:savedmathjaxenvs

\newcommand\:savemathjaxenv[1]{%
  \seq_gput_right:Nn\:savedmathjaxenvs{#1}
  \expandafter\let\csname mathjax-#1\expandafter\endcsname\csname #1\endcsname
  \expandafter\let\csname mathjax-end#1\expandafter\endcsname\csname end#1\endcsname
}

\newcommand\RestoreMathJaxEnvironment[1]{%
  \expandafter\let\csname #1\expandafter\endcsname\csname mathjax-#1\endcsname%
  \expandafter\let\csname end#1\expandafter\endcsname\csname mathjax-end#1\endcsname%
}

\newcommand\RestoreMathJaxEnvironments{%
  \seq_map_function:NN\:savedmathjaxenvs\RestoreMathJaxEnvironment%
}



\cs_generate_variant:Nn \regex_extract_once:nnNTF {nV}
\newcommand\VerbMath[2][]{%
  \cs_if_exist:cTF{#2}{
    \:savemathjaxenv{#2}%
    \RenewDocumentEnvironment{#2}{+!b}{%
      \NoFonts\expandafter\VerbMathToks\expandafter{\detokenize{##1}}{#2}\EndNoFonts%
      \ifx\relax#1\relax\else%
      \refstepcounter{#1}%
      \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}}} {}%
      \fi
    }{}
  }{}%
}
\ExplSyntaxOff
\def\fixmathjaxtoc#1{\Configure{writetoc}{\def#1{\detokenize{#1}}}}

\AtBeginDocument{%
\VerbMath{subarray}
\VerbMath{smallmatrix}
\VerbMath{matrix}
\VerbMath{pmatrix}
\VerbMath{bmatrix}
\VerbMath{Bmatrix}
\VerbMath{vmatrix}
\VerbMath{Vmatrix}
\VerbMath{cases}
\VerbMath{subequations}
\VerbMath{aligned}
\VerbMath{alignedat}
\VerbMath{gathered}
\VerbMath{gather}
\VerbMath{gather*}
\VerbMath{alignat}
\VerbMath{alignat*}
\VerbMath{xalignat}
\VerbMath{xalignat*}
\VerbMath{xxalignat}
\VerbMath{align}
\VerbMath{align*}
\VerbMath{flalign}
\VerbMath{flalign*}
\VerbMath{split}
\VerbMath{multline}
\VerbMath{multline*}
\VerbMath[equation]{equation}
\VerbMath{equation*}
\VerbMath{math}
\VerbMath{displaymath}
\VerbMath{eqnarray}
\VerbMath{eqnarray*}
\ConfigureEnv{gather}{}{}{}{}
\ConfigureEnv{gather*}{}{}{}{}
\ConfigureEnv{multline}{}{}{}{}
\ConfigureEnv{multline*}{}{}{}{}
\fixmathjaxtoc\int
\fixmathjaxtoc\,
\fixmathjaxtoc\sin
\fixmathjaxtoc\cos
\fixmathjaxtoc\tan
\fixmathjaxtoc\arcsin
\fixmathjaxtoc\arccos
\fixmathjaxtoc\arctan
\fixmathjaxtoc\csc
\fixmathjaxtoc\sec
\fixmathjaxtoc\cot
\fixmathjaxtoc\sinh
\fixmathjaxtoc\cosh
\fixmathjaxtoc\tanh
\fixmathjaxtoc\coth
\fixmathjaxtoc\log
\fixmathjaxtoc\ln
\fixmathjaxtoc\sum
\fixmathjaxtoc\(
\fixmathjaxtoc\)
\fixmathjaxtoc\begin
\fixmathjaxtoc\end
\fixmathjaxtoc\\
\fixmathjaxtoc\exp

\@ifpackageloaded{mhchem}{%
\def\ce#1{\texttt{\detokenize{\(\ce{#1}\)}}}
}{}

}
\endinput

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

\Preamble{xhtml}
\tikzset{every picture/.append code={\RestoreMathJaxEnvironments}}
\begin{document}
\EndPreamble

And this is the result:

enter image description here

4
  • 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 .