michal-h21 / make4ht

Build system for tex4ht
131 stars 15 forks source link

\intertext not working as intended #102

Open yalguzaq opened 1 year ago

yalguzaq commented 1 year ago

\intertext is a very popular command in LaTeX. Suppose that I have the following LaTeX code:

We cancel out 2
\begin{align*}
  2x + 2y -2 + 2 &= 2x + 2y
      \intertext{and factor out 2}
  &=2(x+y)
\end{align*}

Then pdflatex produces the following output: image

make4ht, however, puts the text inside the equation (which is the worst possible outcome): image

Obviously, the \intertext part must become a part of the usual HTML. Wouldn't it be possible to replace the opening \intertext{ with \end{align*} and the ending } with \begin{align*}? I once did something like this in JavaScript as a temporary solution (before I switched to make4ht):

latexml.parser.intertext = function(text) {
    for (const align of ["align", "align*"]) {
    var begin_align = text.indexOf("\\begin{"+align+"}");
    while (begin_align != -1) {
        var end_align = text.indexOf("\\end{"+align+"}", begin_align);
        var start = text.indexOf("\\intertext", begin_align, end_align);
        while (start != -1) {
        var end = latexml.parser.bracketClosing(text, "{", start+9+1);
        text = text.modify("}", "\\begin{"+align+"}", end, end+1);
        text = text.replace("\\intertext{", "\\end{"+align+"}");
        start = text.indexOf("\\intertext", begin_align, end_align);
        }
        begin_align = text.indexOf("\\begin{"+align+"}", end_align);        
    }
    }
    return text;
}

The main trick is, of course, preserving the alignment of &. But even if that is not possible, I think that this would be a better solution than what we have right now.

Thanks, Michal!