TokenMap not working as expected

13 views
Skip to first unread message

Andrew Murdza

unread,
Aug 7, 2025, 6:10:28 PMAug 7
to MathJax Users
I have this code that was working in MathJax version 3 with SymbolMap instead of TokenMap.
startup: {
ready: () => {
const MacroMap } = MathJax._.input.tex.TokenMap;//.SymbolMap
const Configuration } = MathJax._.input.tex.Configuration;
const BaseMethods MathJax._.input.tex.base.BaseMethods.default;
new MacroMap('bold-chars', {'a':`\\p{\\unicode{${'a'.charCodeAt(0)}}}}`}, BaseMethods);
Configuration.create('bold-chars', { handler: { character: ['bold-chars'] } });
MathJax.startup.defaultReady();
}
},

This is my full code after removing everything unnecessary. The `\p` makes text invisible until revealed by JavaScript. However, when I run my website, the "a"'s in MathJax is not invisible. Could you please tell me how I can fix this? Also "npm install mathjax-full" seems to install a previous version of MathJax, not MathJax4.0.

window.MathJax = Object.assign({
loader: {
load: [
'[tex]/color',
'[tex]/cancel',
'[tex]/unicode',
'[tex]/mathtools',
'[tex]/textcomp',
],
},
output:{
font:'mathjax-tex'
},
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
preFilters: [
({math}) => {
if (math.start.delim !== '\begin{align}') {
math.math = math.math.replace(/(\\begin|\\end){align}/g, '$1{aligned}');
}
}
],
packages: {
'[+]': ['bold-chars', 'color', 'cancel', 'unicode', 'mathtools', 'textcomp'],//
},
maxBuffer: 10 * 1024,
tags: 'ams',
macros: Object.assign({
//pause
p: ['\\class{invisible}{#1}',1],
})
},
startup: {
ready: () => {
const { MacroMap } = MathJax._.input.tex.TokenMap;//.SymbolMap
const { Configuration } = MathJax._.input.tex.Configuration;
const BaseMethods = MathJax._.input.tex.base.BaseMethods.default;

let map= {};

new MacroMap('bold-chars', {'a':`\\p{\\unicode{${'a'.charCodeAt(0)}}}}`}, BaseMethods);
Configuration.create('bold-chars', { handler: { character: ['bold-chars'] } });
MathJax.startup.defaultReady();
}
}
});
delete window.ExtraMacros;

Davide Cervone

unread,
Aug 7, 2025, 7:58:15 PMAug 7
to mathja...@googlegroups.com
Andrew:

The TokenMap is a direct replacement for SymbolMap, so it works fine.  But your MacroMap() call is incorrect, which is the source of the problem for you.  The object that follows the map name should be key-value pairs where the key is the character to define and the value is either a function that implements it or a string that is the name of a function in the third object that you pass it.  You seem to be passing the sting that you want the letter to be replaced by, but that is not how MacroMap() works, and that isn't how it worked in v3, so I don't see how this code could have ever done what you wanted.  I have tested it in v3 and it does not work there, either.

In v4, there is an easier way to define active characters like this, as they can be configured directly.  E.g.,

MathJax = {
  tex: {
    active: {'a': `\\p{\\unicode{${'a'.charCodeAt(0)}}}`}
  },
};

will do what I think you are trying to do.  See the What's New in v4.0 section on updates to the configmacros package for details.

As for "mathjax-full", in version 4 of MathJax we have moved to scoped packages for everything but the mathjax package itself.  So mathjax-full has been replaced by @mathjax/src instead.  See the What's New in v4.0 section on scoped packages for details.

Finally, you should add another backslash to the \begin in the line

if (math.start.delim !== '\begin{align}') {

since that was missing in the code I provided to Lisa (but corrected in a later message).

Davide


--
You received this message because you are subscribed to the Google Groups "MathJax Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathjax-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/mathjax-users/8c23b7ce-e5f8-450e-90f5-d4265a5a5082n%40googlegroups.com.

Message has been deleted
Message has been deleted

Andrew Murdza

unread,
Aug 10, 2025, 5:47:33 PMAug 10
to MathJax Users
You're right, I actually had map[a]=['Macro',b] not map[a]=b. I messed up when making it a minimal example.
Using active: worked thanks

Andrew Murdza

unread,
Aug 10, 2025, 7:22:28 PMAug 10
to MathJax Users
You're right my code was actually
let char=String.fromCharCode(i);
map[char]=['Macro', (operator?'\\mathrel{{}':'')+
(number?'\\p{\\unicode{'+char.charCodeAt(0)+'}}':'\\p{\\mmlToken{mi}{'+char+'}}')+(operator?'{}}':'')]
However, when I reduced it to a minimal example, I omitted the first part.
Your suggestion worked!
Reply all
Reply to author
Forward
0 new messages