-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use MathJax in TypeScript project? #2385
Comments
I hope for a quick answer this is making me mad :( |
I don't know if my problem is clear but what I basically want to do is just use and initialize MathJax in TypeScript (and include it) rather than initializing it via plain JS.
I don't think this is possible at the moment, since I think (as far as I could see) the whole interface with the component system is set out to be used in JS only (correct me if I'm wrong). But I would really love to use MathJax like that. How much work do you think would this be? Do you think re-writing the interface to be TS compatible would be hard? I would love doing that myself but I would really like to hear your expert opinion on this before starting something that's too big. |
Is this not possible...? |
The MathJax components are designed around the need to run MathJax from a CDN with potential for dynamic loading of components and custom configurations without requiring authors to package their own copies of MathJax. The source files for the components are small wrapper files that load the needed (compiled) typescript files, and register themselves with the component startup system. These seemed minor enough not to need the power of Typescript. While it is possible to bundle these comports in different ways, and to call on the files in |
Sorry for bumping this 2 years later, but this is first in the Google search results when you look for MathJax and TypeScript. The MathJax documentation for TypeScript is absolutely horrendous, especially the 'direct' examples that aren't written in TypeScript, containing an endless amount of useless boilerplate. The following code block contains the most minimal TypeScript code to convert a TeX string into an SVG string. Please be warned to set your module to import { mathjax } from 'mathjax-full/js/mathjax'
import { TeX } from 'mathjax-full/js/input/tex'
import { SVG } from 'mathjax-full/js/output/svg'
import { AllPackages } from 'mathjax-full/js/input/tex/AllPackages'
import { liteAdaptor } from 'mathjax-full/js/adaptors/liteAdaptor'
import { RegisterHTMLHandler } from 'mathjax-full/js/handlers/html'
const adaptor = liteAdaptor()
RegisterHTMLHandler(adaptor)
const mathjax_document = mathjax.document('', {
InputJax: new TeX({ packages: AllPackages }),
OutputJax: new SVG({ fontCache: 'local' })
})
const mathjax_options = {
em: 16,
ex: 8,
containerWidth: 1280
}
export function get_mathjax_svg(math: string): string {
const node = mathjax_document.convert(math, mathjax_options)
return adaptor.innerHTML(node)
} |
@winstxnhdw Would you please give one more example when output is to be MathML? |
Hey, @mustafasalahuldin. I believe it is as simple as switching import { mathjax } from 'mathjax-full/js/mathjax'
import { MathML} from 'mathjax-full/js/input/mathml'
import { SVG } from 'mathjax-full/js/output/svg'
import { liteAdaptor } from 'mathjax-full/js/adaptors/liteAdaptor'
import { RegisterHTMLHandler } from 'mathjax-full/js/handlers/html'
const adaptor = liteAdaptor()
RegisterHTMLHandler(adaptor)
const mathjax_document = mathjax.document('', {
InputJax: new MathML(),
OutputJax: new SVG({ fontCache: 'local' })
})
const mathjax_options = {
em: 16,
ex: 8,
containerWidth: 1280
}
export function get_mathjax_svg(math: string): string {
const node = mathjax_document.convert(math, mathjax_options)
return adaptor.innerHTML(node)
} |
@winstxnhdw I adjusted your code with whatever I could found in the website, found here.
The tags are generated but it has no values, any advice? |
I need to render from Lax to MML3 |
@winstxnhdw And I need to enable the accessibility aria tags feature too, please |
Any idea how to get around this?
because if I change module to CommonJS from ES2015 I get this error:
...and I'd love to avoid having to rework my rollup.config.js |
I managed to get the code working with module "es2015".
|
I managed to get it to work with ESNext but I am no longer using |
I built a custom combined component file with a modified subclass of the output jax
CHTML
and incorporated it into my project. My combined component filecustom-component.js
is written in plain JavaScript, just like the examples in the MathJax-demos-web repo. I included my webpackedcustom-component.min.js
with a<script>
tag in myindex.html
and everything worked fine. I used JavaScript in my other scripts inindex.html
as well.Now, however, I am looking to move to TypeScript, since now that I have made the connection to MathJax by being able to configure and access the output jax in the ways I want, I am ready to write my own code, and that, I want to do in TypeScript.
That's why I am looking to get rid of all the plain JavaScript in my
index.html
and move all the logic (and MathJax loading) to anindex.ts
file.That's where I am running into problems though. Since I would like to have just a single
<script>
tag that I need to include in myindex.html
, I want even the MathJax loading to be done in myindex.ts
. And although I can import my own combined component file, I can't setup the global MathJax object because TypeScript does not recognize it.I looked into the code a little bit about how the global is set up, but I don't understand how to solve my problem anyway.
My question now is, what I am doing wrong here. Am I taking a wrong approach? Should I rewrite my custom component file to be a TypeScript file and not a plain JavaScript one? And also, why were all the components written in JavaScript and not in TypeScript like the rest of the project? Is there some easy way to connect to the MathJax source with a TypeScript project like mine?
The text was updated successfully, but these errors were encountered: