# Wrong tag ordering in mathml produced using htlatex

I am converting large mathematical documents from latex to html + mathml. I have wrongly shaped file as output in some situations. An example is as follows.

The main file test.tex is:

\documentclass{article}

\begin{document}
$X \mbox{ is \emph{dense in Y}}$
\end{document}


The minimalistic configuration myconfig.cfg file is:

\Preamble{mathml}

\Configure{emph}{\ifvmode\ShowPar\fi\HCode{<em>}}{\HCode{</em>}}

\begin{document}
\EndPreamble


Now, running the command htlatex test.tex myconfig.cfg I get (after removing the preamble, and reorganising the code):

<body>
<div class="par-math-display"><!--l. 5--><math
xmlns="http://www.w3.org/1998/Math/MathML"
display="block" >
<mrow>
<mi>X</mi>
<mstyle class="mbox">
<!--error-->   <mtext>&#x00A0;is&#x00A0;
<!--error-->   <em>dense&#x00A0;in&#x00A0;
<!--error-->   </mtext>
<mstyle class="math">
<mi>Y</mi>
</mstyle>
<mtext>
<!--error-->   </em>
</mtext></mstyle>
</mrow>[/itex]</div>
<!--l. 7--><p class="nopar" >
</body>


In this output, the opening and closing tags of mtext and em that I have highlighted are interleaved rather than nested.

Let me add that modifying the latex code is not an option (because of large pieces of code). What is nevertheless possible would be to redefine \emph (however the various redefinitions I tried did not change the output).

• There is a bug tracker for tex4ht (puszcza.gnu.org.ua/bugs/?group=tex4ht). But asking here is ok too. @michal.h21 is doing a good job trying to resolve this sort of things. Feb 22 '18 at 9:25

Edit:

Here is updated configuration that actually produces <mtext> element when formatting command is used:

\Preamble{xhtml,mathml,fonts}
\catcode\:=11
\makeatletter
\newcommand\providemtextclass[1]{%
\Configure{@mtext}{#1}%
\HCode{</mtext>}\ht:special{t4ht@,}\HCode{<mtext \a:@mtext>}\ht:special{t4ht@,&\#x00A0;}%
}
\catcode\:=12
\makeatother

\begin{document}
\Configure{emph} {\ifmathml\providemtextclass{class="emph" mathvariant="italic" }\else \HCode{<em>}\NoFonts\fi}{\ifmathml\else\EndNoFonts \HCode{</em>}\fi}
\EndPreamble


The \providemtextclass command is a little bit complicated. It defines attributes which should be used in following <mtext> elements. It needs to close the actually opened <mtext>. The \ht:special commands provide special instructions to tex4ht. The \ht:special{t4ht@,&\#x00A0;} is used in to replace spaces with unbreakable spaces. It is necessary in MathML text, because words would collapse together otherwise. But it must be disabled when tags are printed, because it would produce invalid XML structure. It can be disabled using \ht:special{t4ht@,}.

The rendered result:

and MathML:

<math display='block' xmlns='http://www.w3.org/1998/Math/MathML'><mrow>
<mi>X</mi><mstyle class='mbox'><mtext> is </mtext><mtext class='emph' mathvariant='italic'>dense in </mtext><mstyle class='math'><mi>Y</mi> </mstyle><mtext class='emph' mathvariant='italic'></mtext></mstyle>
</mrow>[/itex]


The configuration for \emph command needs to take into account the situation when it is used inside math, you will get invalid MathML otherwise.

Here is the modified version of configuration provided in mathml.4ht:

\Preamble{xhtml,mathml,fonts}
\Configure{emph}
{\ifmathml \Configure{@mtext}{ class="emph"
mathvariant="italic" }%
\else \HCode{<em>}\NoFonts\fi}
{\ifmathml\else\EndNoFonts \HCode{</em>}\fi}
\begin{document}
\EndPreamble


The basic text commands produce <mtext> element in MathML, it is possible to set it's attributes using \Configure{@mtext}. This configuration is used inside math environments thanks to \ifmathml swich. In normal text, <em> is used.

This produces the following result:

<div class='par-math-display'><!-- l. 4 --><math xmlns='http://www.w3.org/1998/Math/MathML' display='block'><mrow>
<mi>X</mi><mstyle class='mbox'><mtext> is dense in </mtext><mstyle class='math'><mi>Y</mi> </mstyle><mtext class='emph' mathvariant='italic'></mtext></mstyle>
</mrow>[/itex]</div>


You can see that there is a spurious <mtext class='emph' mathvariant='italic'></mtext>. It is caused by including $Y$ in \emph command. This version works better:

 X \mbox{ is \emph{dense in }$Y$}


Regarding your last question. We have a bug tracker and source code repository at Puszcza. The error are fixed directly in the sources and the updates go directly to the TeX distributions.

Big part of the questions on TeX.sx are really not bugs, but more questions about modifying output to particular user needs, so it doesn't really make sense to provide them as a package. It would be nice if we provided some list of common issues and configurations, the fact is that we also need to update the documentation. I started to work on it, but the progress is slow. It is much easier to write code than prose to me, especially because I am not native English speaker. I am also busy on new make4ht and tex4ebooks versions, so I can't really find a time for the documentation.

• Thanks a lot for this answer. It provides a well structured html document. However, it does not fulfil the expected semantics: the part dense in of the example text does not get to be emphasized. Furthermore my goal is to insert a link at this place, not to emphasize. Feb 22 '18 at 16:48
• @ThomasColcombet you are right, I see it now. I need to investigate that. Feb 22 '18 at 20:05
• @ThomasColcombet I provided another solution Feb 22 '18 at 22:17
• Wonderful. Now the result is fine. So if I understand properly, it is not possible inside a mathml block to revert to the normal text behaviour of LaTeX as would do an \mbox command... Also, these \ht:special commands are extremely weird. What would be a good source for understanding what it does and how to use it ? Feb 23 '18 at 9:27
• @ThomasColcombet the problem with text inside math is that it would produce invalid MathML if normal configurations for HTML were used. So it is necessary to use MathML elements like <mtext> or <mi>. I've found these \ht:special commands in the tex4ht source code. There are various codes, they are documented here: kodymirus.cz/src4ht/tex4ht-info69.html Feb 23 '18 at 9:37