How to use MathJax v4 in a Node.js+TypeScript project?

21 views
Skip to first unread message

Leandro Facchinetti

unread,
Oct 7, 2025, 7:26:08 AMOct 7
to MathJax Users
Hi MathJax folks,

I’m trying to use MathJax v4 in a Node.js+TypeScript project and unfortunately I haven’t succeeded so far.

The ‘mathjax’ and ‘@mathjax/src’ packages don’t seem to include types (no ‘types‘ field in ‘package.json’).

The ‘@types/mathjax’ package is for an older version of MathJax.

I looked at the examples in the documentation, but they don’t use TypeScript.

I suppose that it should be possible to do what I’m trying to do, given that MathJax is itself written in TypeScript, but perhaps there’s a reason why the types weren’t published as part of the packages?

For the time being I’m working around this by importing MathJax without types:

// @ts-expect-error
import MathJax from "mathjax";

Thanks in advance for any help.

Rob Beezer

unread,
Oct 14, 2025, 1:37:10 PMOct 14
to mathja...@googlegroups.com
The following LaTeX renders just fine with v3:

```tex
\begin{align}
c^2 \amp = a^2+b^2\tag{✶✶}\\
c^2 \amp = a^2+b^2\tag{††}\\
c^2 \amp = a^2+b^2\tag{‡‡}\\
c^2 \amp = a^2+b^2\tag{##}\\
c^2 \amp = a^2+b^2\tag{✠✠}
\end{align}
```

But now in v4 the whole display says "Misplaced '#'". You can see v3 here:

Subsection 7.5: Local Tags on Equations
https://pretextbook.org/examples/sample-article/html/section-mathematics.html#section-mathematics-7

Using \# instead seems to be the solution for v4. (Though with v3, the "\"
renders also.)

Wanted to check that the v3 behavior was perhaps accidental and escaping the
octothorpe is now correct use. Or maybe vice-versa?

Will v4 support the version PreTeXt creates for use with "regular" LaTeX? (I
removed the \label{} from these.) It might be nice if these were identical for
creating both PDF and HTML output (and maybe especially for SRE), and did not
use Unicode characters.

```tex
\begin{align}
c^2 \amp = a^2+b^2\tag{\textasteriskcentered\textasteriskcentered}\\
c^2 \amp = a^2+b^2\tag{\textdagger\textdagger}\\
c^2 \amp = a^2+b^2\tag{\textdaggerdbl\textdaggerdbl}\\
c^2 \amp = a^2+b^2\tag{\#\#}\\
c^2 \amp = a^2+b^2\tag{\maltese\maltese}
\end{align}

Thanks,
Rob

Davide Cervone

unread,
Oct 14, 2025, 3:31:49 PMOct 14
to mathja...@googlegroups.com
Leandro:

I suppose that it should be possible to do what I’m trying to do, given that MathJax is itself written in TypeScript, but perhaps there’s a reason why the types weren’t published as part of the packages?

If you use the @mathjax/src package and use the direct access method (rather than the components framework), you will get the typing from the .d.ts files in the mjs directory.  See the Linking to MathJax Directly in Node section of the documentation for examples.

Because the MathJax object is used as both the configuration object in the browser and is then modified when MathJax is loaded, and because its contents depends on the components that you have loaded, and those can come from third-party sources as well as MathJax's core components, and because the components are not known at compile time, it is difficult to make reasonable typings for the MathJax object.

Davide

Davide Cervone

unread,
Oct 14, 2025, 4:16:47 PMOct 14
to mathja...@googlegroups.com
Rob:

Not sure how your message got linked into this thread (did you reply to a previous message and just change the subject?).

In any case, in MathJax v4, the textmacros package is now included in the combined components like tex-chtml.js, so the contents of tex-mode material are now processed for macros and special characters.  Since the \tag argument is in text mode, it will now be processed as well, and since # is a TeX special character (only allowed in macro definitions), that is now being flagged inside the \tag.

Using  \#  instead seems to be the solution for v4.  (Though with v3, the "\" renders also.)

Right, \# is the easiest way to get the literal # in text mode.  

If you want v4 to act like v3, you can configure MathJax to not include the textmacros package using

MathJax = {
  tex: {
    packages: {'[-]': ['textmacros']}
  }
};

as described in the Textmacros Enabled by Default section of the documentation.

If you want to have v3 act like v3, use

MathJax = {
  loader: {
    load: ['[tex]/textmacros'],
  },
  tex: {
    packages: {'[+]': ['textmacros']}
  }
};

instead.

Wanted to check that the v3 behavior was perhaps accidental and escaping the octothorpe is now correct use.  Or maybe vice-versa?

It is the expected behavior for both versions, since v4 includes textmacros in the combined components while v3 does not.

Will v4 support the version PreTeXt creates for use with "regular" LaTeX? (I removed the \label{} from these.)  It might be nice if these were identical for creating both PDF and HTML output (and maybe especially for SRE), and did not use Unicode characters.

\begin{align}
c^2 \amp = a^2+b^2\tag{\textasteriskcentered\textasteriskcentered}\\
c^2 \amp = a^2+b^2\tag{\textdagger\textdagger}\\
c^2 \amp = a^2+b^2\tag{\textdaggerdbl\textdaggerdbl}\\
c^2 \amp = a^2+b^2\tag{\#\#}\\
c^2 \amp = a^2+b^2\tag{\maltese\maltese}
\end{align}


The \textasterskcentered, \textdagger, and \textdaggerdbl macros are part of the textcomp package, which you can load and add to the packages array (in both v3 and v4).  The \maltese macro is defined in the ams package, which is included in all the combined components.  Unfortunately, it is not marked as a text-mode macro, so using it will cause an error about \maltese only being allowed in math mode.  You can get around this by redefining \maltese via

MathJax = {
  tex: {
    macros: {
      maltese: '\\char"2720',
    }
  }
};

So if you load textcomp, and use the definition above, you should be able to use these tags.  You will need to define \amp yourself, however.  One way is to add

      amp: '&',

into the macro list above.   Then you can use the same expression in both MathJax and LaTeX.

Davide

Rob Beezer

unread,
Oct 14, 2025, 5:24:04 PMOct 14
to mathja...@googlegroups.com
On 10/14/25 13:16, Davide Cervone wrote:
> Not sure how your message got linked into this thread (did you reply to a
> previous message and just change the subject?).

Yes, that's what I did. Sorry - I didn't expect my email client to do that.

Thanks very much for all the details. I think I'll start simple with "\#" as we
move to v4, and then see about harmonizing with the "textmacros" package after
that. There are likely other places I can take advantage of that new feature.

Thanks very much.

Rob

Davide Cervone

unread,
Oct 14, 2025, 5:27:48 PMOct 14
to mathja...@googlegroups.com
No problem. I just reread my message and saw an important typo. Where I say "If you want v3 to act like v3", it should be "if you want v3 to act like v4". OOPS!

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/MTAwMDAyNS5iZWV6ZXI.1760477039%40pnsh.

Reply all
Reply to author
Forward
0 new messages