Support for comma as decimal separator in math mode
Problem description
In many countries, the comma is used as the decimal separator (e.g., 3,14).
In MathJax, when using , in math mode, a thin space is automatically inserted after the comma because it is treated as punctuation (\mathpunct) in TeX.
This makes it impossible to display numbers with commas as decimals correctly, without modifying thousands of formulas to use 3{,}14 or similar unadvisable workarounds.
Expected behavior
It should be possible to treat commas between digits as decimal separators, without adding extra space, ideally via a configuration option or macro, so that formulas like 3,14 render naturally.
Current behavior
- The thin space is always inserted after
,in math mode. - Workarounds require modifying the formulas themselves (
3{,}14) or using CSS hacks, which are not robust.
Environment
- MathJax 3 latest versions
- Browser rendering
Proposal
- Provide a configuration option to treat commas between digits as “mathord” automatically.
- Or provide a built-in macro to render decimal commas correctly without extra spacing.
This makes it impossible to display numbers with commas as decimals correctly, without modifying thousands of formulas to use
3{,}14or similar unadvisable workarounds.
That's because that's the way TeX works.
Provide a configuration option to treat commas between digits as “mathord” automatically.
There already exists such an option: numberPattern in the tex block of the MathJax configuration.
For example
MathJax = {
tex: {
numberPattern: /^(?:[0-9]+(?:\.[0-9]{3})*(?:,[0-9]*)?|,[0-9]+)/,
},
};
would make . be the thousands separator and , be the decimal point, without the need for braces.
I tried to implement such solution in the following MWE HTML, but I couldn't make it work.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.26">
<title>Tabella 4.2.VII</title>
<script>
window.MathJax = {
tex: {
numberPattern: /^(?:[0-9]+(?:\.[0-9]{3})*(?:,[0-9]*)?|,[0-9]+)/,
},
};
</script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
</head>
<body class="article">
<div id="header">
<p>Resistenza delle Sezioni di Classe 1-2-3-4</p><p>\(\gamma_{M0} = 1,05\)</p>
</div>
</body>
</html>
Sorry, I missed the fact that you are using v3, and I gave you a v4 configuration. In v3 the portion is the poorly named digits, so change numberPattern to digits and you should be in business. Note that the developer's console should have given you message about numberPattern not being defined, which might have been clue as to what was going on.
Thanks, it works. Is there a way to do the same with MathJax 2.7.9 too?
Is there a way to do the same with MathJax 2.7.9 too?
Yes, but it is a bit more difficult. Issue #169 discusses this, and suggests something like
<script type="text/x-mathjax-config">
MathJax.Hub.Register.StartupHook("TeX Jax Ready", function () {
MathJax.InputJax.TeX.Definitions.number = /^(?:[0-9]+(?:\.[0-9]{3})*(?:,[0-9]*)*|,[0-9]+)/;
MathJax.InputJax.TeX.Definitions.digit = /[0-9.,]/;
});
</script>
as the way to do this. (In v2, these weren't available as configuration options directly).