michal-h21 / make4ht

Build system for tex4ht
131 stars 15 forks source link

Problem with Tikz: matrix #108

Closed danielezambelli closed 1 year ago

danielezambelli commented 1 year ago

I get different behavior in graphics compiling with mathml or with mathjax.

The following example describes the situation.

%<-------------------------------------------------------------------
\documentclass[10pt,a4paper]{memoir}
\usepackage[T1]{fontenc} 
\usepackage{textcomp}   
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel} 

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{
  matrix,%
  positioning,%
}
\usepackage{lipsum}

\def \valposc{green!60!black}
\def \quozc{blue!60!black}
\def \restc{red!60!black}

\newenvironment{alternativetext}[1]{}{}

\newcommand{\divisintb}{
\begin{tikzpicture}[node distance=-25ex, ampersand replacement=\&]
  \begin{scope}[font=\ttfamily]
    \matrix (divisioneb) [matrix of nodes]
    {%
  \& 3 \& 2 \& 7 \& 2 \& 3 \&[8mm] \\
- \& 2 \& 3 \&   \& |[\quozc]|1 \& |[\quozc]|4 \& \\
  \&   \& 9 \& 7 \&   \&   \& \\
  \& - \& 9 \& 2 \&  \&  \& \\
  \&   \&   \& |[\restc]|5 \&  \&  \& \\
    };
  \end{scope}
%   Prima divisioneb
  \draw(divisioneb-1-5.north west)--(divisioneb-2-5.south west);
  \draw(divisioneb-1-5.south west)--(divisioneb-1-6.south east);
  \draw(divisioneb-2-2.south west)--(divisioneb-2-3.south east);
  \draw(divisioneb-4-3.south west)--(divisioneb-4-4.south east);
  \draw[densely dotted,->] (divisioneb-1-4) -- (divisioneb-3-4);
  \node (a) [above=of divisioneb-1-4.north west] {$Q=14 \quad R=5$};
\end{tikzpicture}
}

\begin{document}

\chapter{Problem with Tikz: matrix}

\divisintb

Compiling in mathml:

\begin{verbatim}
make4ht tikz_matrix.tex -f html5-common_domfilters -c ml_make4ht -l -u -s -d 
./html/mathml "mathml, 3, sec-filename, fn-in, blind"
\end{verbatim}

I get the graph despite the error:

\begin{verbatim}
[ERROR]   htlatex: ./ml_make4ht.        29       LaTeX Error: Missing 
\begin{document} in `ml_make4ht.cfg'.
\end{verbatim}

Compiling in mathjax:

\begin{verbatim}
make4ht tikz_matrix.tex -f html5-common_domfilters -c ml_make4ht -l -u -s -d 
./html/mathjax "mathjax, 3, sec-filename, fn-in, blind"
\end{verbatim}

I get the following errors:

\begin{verbatim}
[ERROR]   htlatex: ./tikz_matrix.tex    48       Package amsmath Error: Old 
form 
`\matrix' should be \begin{matrix}.
[ERROR]   htlatex: ./tikz_matrix.tex    48       Package pgf Error: No shape 
named `divisioneb-1-5' is known.
...

[ERROR]   htlatex: ./ml_make4ht.        29       LaTeX Error: Missing 
\begin{document} in `ml_make4ht.cfg'.
\end{verbatim}

and instead of the graph I get a little symbol.
\end{document}
%<-------------------------------------------------------------------

I attach the configuration file which is quite long.

Thanks for your attention and your work.

Daniele

ml_make4ht.zip

michal-h21 commented 1 year ago

There are two problems. First is that the configuration file produces errors if you don't use the ulem package. It can be fixed by replacing all ulem configurations with the following code, which checks it the package is loaded first:

\makeatletter
\@ifpackageloaded{ulem}{%
\Configure{uline}
   {\HCode{<span class="uline">}\NoFonts} {\EndNoFonts\HCode{</span>}}
\Configure{uline-math}
   {\HCode{<span class="uline">}\NoFonts} {\EndNoFonts\HCode{</span>}}
\Css{.uline,.uline-math{ text-decoration:underline; }}
\Configure{uuline}
   {\HCode{<span class="uuline">}\NoFonts} {\EndNoFonts\HCode{</span>}}
\Configure{uuline-math}
   {\HCode{<span class="uuline">}\NoFonts} {\EndNoFonts\HCode{</span>}}
\Css{.uuline,.uuline-math{ text-decoration:underline double; }}
\Configure{sout}
   {\HCode{<span class="sout">}\NoFonts} {\EndNoFonts\HCode{</span>}}
\Configure{sout-math}
   {\HCode{<span class="sout">}\NoFonts} {\EndNoFonts\HCode{</span>}}
\Css{span.sout, .sout-math {text-decoration: line-through }}
\Configure{xout}
   {\HCode{<span class="xout">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Configure{xout-math}
   {\HCode{<span class="xout-math">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Css{span.xout, .xout-math {text-decoration: line-through underline }}
\Configure{uwave}
   {\HCode{<span class="uwave">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Configure{uwave-math}
   {\HCode{<span class="uwave-math">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Css{span.uwave, .uwave-math {text-decoration:underline wavy; }}
\Configure{dashuline}
   {\HCode{<span class="dashuline">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Configure{dashuline-math}
   {\HCode{<span class="dashuline-math">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Css{span.dashuline, .dashuline-math {text-decoration:underline dashed;}}
\Configure{dotuline}
   {\HCode{<span class="dotuline">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Configure{dotuline-math}
   {\HCode{<span class="dotuline-math">}\NoFonts} 
   {\EndNoFonts\HCode{</span>}}
\Css{span.dotuline, .dotuline-math {text-decoration:underline dotted;}}
}{}
\makeatother

The other issue is that in the MathJax mode, we redefine math environments at the beginning of each TikZ environment, in order to produce images. The problem is that we replace TikZ's \matrix command with the matrix math environment. I've updated TeX4ht source code, so this error should go away soon.

danielezambelli commented 1 year ago

Thank you Michal!

I will update the configuration file.

If the update for matrix is for a single file, can you tell me where I can download it to put it in my system without waiting for the TexLive distribution update?

Thank you, a good day!

Daniele

michal-h21 commented 1 year ago

It is this file, mathjax-latex-4ht.4ht:

% mathjax-latex-4ht.4ht (2023-02-22-14:58), 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 2023-02-22-14:58}

\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%
}

% we must not reintroduce the matrix environmeint in TikZ, because it interferes with the \matrix command
\newcommand\:ignoretikzmatrix{\seq_remove_all:Nn\:savedmathjaxenvs{matrix}}

\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%
}
\ExplSyntaxOff
\ExplSyntaxOn
\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
\fixmathjaxtoc\left
\fixmathjaxtoc\right

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

\@ifpackageloaded{tikz}{%
\tikzset{every picture/.append code={\:ignoretikzmatrix\RestoreMathJaxEnvironments}}
}{}

}
\endinput

\endinput

But it should be included in TeX Live soon, probably in few days.

danielezambelli commented 1 year ago

Thank you for your hints.

I put the file in the same directory where the file being compiled is located and it works. When texlive is updated I will remove the file from there.

I don't really understand the mechanism by which the .4ht files are loaded, where can I find information about that?

michal-h21 commented 1 year ago

It seems that the update is already included in TeX Live.

.4ht files work like other files in TeX distribution, so you can find them using the kpsewhich command. So for example the mathjax file can be found using:

 $ kpsewhich mathjax-latex-4ht.4ht

which is /usr/local/texlive/2022/texmf-dist/tex/generic/tex4ht/mathjax-latex-4ht.4ht on my system.

danielezambelli commented 1 year ago

On my system (Debian Testing), the latest update of texlive (version: 2022.20230122-3) has the old version of the mathjax-latex-4ht.4ht file. I replaced it with the version reported here and the compilation worked fine again.

michal-h21 commented 1 year ago

I think TeX Live 2022 will not get more updates, I hope Debian will switch to TL 2023 soon.