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:


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

The minimalistic configuration myconfig.cfg file is:




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

   <div class="par-math-display"><!--l. 5--><math 
display="block" >
<mstyle class="mbox">
<!--error-->   <mtext>&#x00A0;is&#x00A0;
<!--error-->   <em>dense&#x00A0;in&#x00A0;
<!--error-->   </mtext>
  <mstyle class="math">
<!--error-->   </em>
<!--l. 7--><p class="nopar" >  

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).


1 Answer 1



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

  \HCode{</mtext>}\ht:special{t4ht@,}\HCode{<mtext \a:@mtext>}\ht:special{t4ht@,&\#x00A0;}%

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

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:

enter image description here

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>

Original answer:

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:

{\ifmathml \Configure{@mtext}{ class="emph"
mathvariant="italic" }%
\else \HCode{<em>}\NoFonts\fi}
{\ifmathml\else\EndNoFonts \HCode{</em>}\fi}

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>

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, 2018 at 16:48
  • @ThomasColcombet you are right, I see it now. I need to investigate that.
    – michal.h21
    Feb 22, 2018 at 20:05
  • @ThomasColcombet I provided another solution
    – michal.h21
    Feb 22, 2018 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, 2018 at 9:27
  • 1
    @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
    – michal.h21
    Feb 23, 2018 at 9:37

You must log in to answer this question.

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