This is a follow-up question to the one answered here: Issue with mhchem and fractions in make4ht and mathml.

Using that same MWE, but adding in hyperref, cleverref, and a label in the equation does not break the reaction (if using mhchem to build reaction equations-not shown here), but it does break the equation reference and destroys the equation rendering.

\usepackage[noabbrev]{cleveref} % This makes references better


Text with formula \ce{CuCl2} and \ce{Cu^{2+}} referring to \ref{newLabel}.

\frac{\ce{mass CuCl2}}{1}\times\frac{\ce{1 mol CuCl2}}{\ce{MM of CuCl2}}= \mathord{?}\mathord{?}~\ce{ mol CuCl2}

Text before a reaction.

\ce{Al^0 (s) -> Al^3+ (aq) + 3e-}\\
\ce{Cu^2+ (aq) + 2e- -> Cu^0 (s)}


This uses the preamble config file from the other post: config.cfg


and it is run with the command:

make4ht -c config.cfg mwe.tex

Result: Screenshot showing a red snippet of LaTeX code with the begin equation and end equation visible with yellow highlight

The HTML of the snippet using @michal.21 answer below:

<!DOCTYPE html> 
<html lang='en' xml:lang='en'> 
<head> <title></title> 
<meta charset='utf-8' /> 
<meta content='TeX4ht (https://tug.org/tex4ht/)' name='generator' /> 
<meta content='width=device-width,initial-scale=1' name='viewport' /> 
<link href='mwe7.css' rel='stylesheet' type='text/css' /> 
<meta content='mwe7.tex' name='src' /> 
<script>window.MathJax = { tex: { tags: "ams", }, }; </script> 
 <script async='async' id='MathJax-script' src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js' type='text/javascript'></script>  
<!-- l. 11 --><p class='noindent'>Text with formula <span class='cmtt-10'>\(\ce {CuCl2}\) </span>and <span class='cmtt-10'>\(\ce {Cu^{2+}}\) </span>referring to
<span class='cmbx-10'>??</span>.
</p><!-- l. 15 --><p class='indent'>   \begin {equation} \@ifnextchar [\label@optarg \label@noarg {newlabel} \frac {\ce {mass CuCl2}}{1}\times \frac {\ce {1 mol CuCl2}}{\ce {MM of CuCl2}}= \mathord {?}\mathord {?}~\ce { mol CuCl2}  \end {equation} <a id='x1-2r1'></a>
</p><!-- l. 17 --><p class='indent'>   Text before a reaction.
</p><!-- l. 22 --><p class='indent'>   \begin {align}  \ce {Al^0 (s) -&gt; Al^3+ (aq) + 3e-}\\ \ce {Cu^2+ (aq) + 2e- -&gt; Cu^0 (s)}  \end {align}
  • Can you post a screenshot of the error that you get? Equation works for me, but the referencing doesn't as it needs to be handled by MathJax too. You can replace \ref{newLabel} by $\eqref{newlabel}$, and it should work.
    – michal.h21
    Commented Oct 4, 2022 at 14:44
  • @michal.h21 I have added what I get in my Chrome browser. there isn't an error in the terminal, but I can go find a more specific error in the log if you let me know where to look. I have a whole set of documents that all use \Cref{} for all references as I have multiple styles of tables, appendix tables, and the same for figures and equations. It would be very hard to retroactively try to force \ref to use all the custom names, I think. Commented Oct 4, 2022 at 18:08
  • Can you post your full config file? I don't get the MathJax error that you get. It shouldn't happen, as \label should be passed verbatim to the HTML file. Regarding \cref etc., it is quite complicated, as labels inside math are invisible on the LaTeX side. But I think that I found a solution some time ago using some Lua scripting.
    – michal.h21
    Commented Oct 4, 2022 at 20:29

1 Answer 1


The problem is that the \label is inside the environment that is output verbatim to the HTML code, so LaTeX referencing system doesn't see it at all.

Fortunatelly, TeX4ht saves contents of the environment to a token list, and using LaTeX 3 regular expressions, we can search for \label and set it outside the verbatim block. Try this configuration file:

\cs_generate_variant:Nn \regex_extract_once:nnNTF {nV}
  \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}}} {}

We execute the code that should searches for \label at the end of the equation environment, thanks to \AddToHook{env/equation/end}. We update manually the equation counter, because it is not done automatically in the MathJax mode. The \label is extracted using \regex_extract_once:nVNTF function. The \l_tmpb_tl is variable used by the MathJax node for some manipulations of the MathJax environment, so we can reuse it for our purposes. When it is found, we output \label in the true branch of the regex command.

This is the result:

enter image description here

  • I use the same mwe.tex file at the top (edited because the document class was missing - maybe you did article?) and your config file and I get the same yellow box at the top, but now all of the text after the first reaction is missing. Your screenshot is also missing the second equation in the reaction scheme. Commented Oct 5, 2022 at 17:58
  • ah, I see the difference, now. You did have the second equation, but it's inline instead of a new line as the \\ would have expected. Commented Oct 5, 2022 at 18:14
  • @JustinShorb the second equation shows as one line even in the PDF mode, so I think you would need a different markup for two lines.
    – michal.h21
    Commented Oct 5, 2022 at 18:47
  • @JustinShorb there is also one issue with your sample, you need to use \ref{newlabel}, instead of \ref{newLabel}
    – michal.h21
    Commented Oct 5, 2022 at 18:48
  • Thanks, @michal.h21 That was definitely a typo. Still, using the mwe above (fixed to use newlabel properly), I still get the yellow rendering box without the equation. Within the yellow box is: \begin {equation} \@ifnextchar [\label@optarg \label@noarg {newlabel} \frac {\ce {mass CuCl2}}{1}\times \frac {\ce {1 mol CuCl2}}{\ce {MM of CuCl2}}= \mathord {?}\mathord {?}~\ce { mol CuCl2} \end {equation} Commented Oct 6, 2022 at 19:29

You must log in to answer this question.

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