MathJax icon indicating copy to clipboard operation
MathJax copied to clipboard

Incorrect Spacing Between `\textcolor` inside `\texttt`

Open RarityBrown opened this issue 10 months ago • 1 comments

Steps to Reproduce:

Code:

\texttt{R~B}
\texttt{R ~ B}
\texttt{R~~~B}
\texttt{R   B}
\texttt{\textcolor{red}{R}~\textcolor{blue}{B}}
\texttt{\textcolor{red}{R} ~ \textcolor{blue}{B}}
\texttt{\textcolor{red}{R}~~~\textcolor{blue}{B}}
\texttt{\textcolor{red}{R}   \textcolor{blue}{B}}

Github:

$$ \texttt{R~B} \texttt{R ~ B} \texttt{R~~~B} \texttt{R B} \texttt{\textcolor{red}{R}~\textcolor{blue}{B}} \texttt{\textcolor{red}{R} ~ \textcolor{blue}{B}} \texttt{\textcolor{red}{R}~~~\textcolor{blue}{B}} \texttt{\textcolor{red}{R} \textcolor{blue}{B}} $$

Standard Mathjax:

Image

XeLaTeX:

This may be related to #1953?

Technical details:

  • MathJax Version: 3.2.2
  • Client OS: Windows 11
  • Browser: Chrome

RarityBrown avatar Apr 06 '25 08:04 RarityBrown

Thanks for the report. The issue is caused by MathJax removing spaces at the beginning and ending of the <mtext> elements produces within a \text macro (or one of its relatives like \texttt). These occur whenever MathJax has to break up the content of \text into separate pieces, as is the case for when it contains color commands, but there are many other places that also occurs. The space removal used a regular expression based on \s which matches any space, including the non-breaking space produced by ~.

I will make a PR to correct this, but for now, you can use a configuration like

MathJax = {
  loader: {load: ['[tex]/textmacros']},
  tex: {
    packages: {'[+]': ['textmacros']}
  },
  startup: {
    ready() {
      const ParseUtil = MathJax._.input.tex.ParseUtil.default;
      ParseUtil.internalText = function (parser, text, def) {
        text = text.replace(/\n+/g, ' ').replace(/^ +/, '\u00A0').replace(/ +$/, '\u00A0');
        return parser.create('node', 'mtext', [], def, parser.create('text', text));
      };
      MathJax.startup.defaultReady();
    }
  }
}

to resolve the problem.

dpvc avatar Apr 06 '25 23:04 dpvc

Fixed in v4.0

dpvc avatar Aug 13 '25 14:08 dpvc