Unfortunately, very little information on this library. It is not completely clear to me after installation what I need to import into the app.module.ts and whether there is something to import there? I have prescribed the following code in the index.html:
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$','$'], ['\\(','\\)']]
}
});
</script>
<script type="text/javascript" async
src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?
config=TeX-MML-AM_CHTML'>
</script>
And how can I apply the MathJax, if I have not simple text, but a table in which text with formulas appears in some columns? Perhaps you can somehow transfer the entire table to MathJax.Hub.Queue?
I was looking at the same problem like two weeks ago and today i finally manage to make it work. I'm no angular expert so it may need some optimization, but core funcionality is working.
Step one
Add
@types/mathjax
dependency to yourpackage.json
file.Step two
Add newly added type to
tsconfig.app.json
like this:This will allow you to use constructs such as
MathJax.Callback.Queue
and your IDE won't complain about unkown type etc.Step three create wrapper object for your math content (optional)
I had some issues with Mathml so i have created wrapper for math which looks like this :
Step four
Now we need to define module which whill inject MathJax script tag with configuration. Because it will be loaded dynamically
async
we need to make sure typesetting doesn't start before MathJax is fully loaded. Easiest way is to storeObserver<any>
intowindow
object.Observer
and render functionality can be wrapped into service.Step five
Now we will create directive which will trigger rendering once MathJax is loaded. The directive may look like this:
Step six (almost there)
In step four i mentioned
async
loading. According to docs onMathJax this is done usingdocument.createElement
. Angular Module is perfect place for this logic. To trigger.ready()
method on ourMathService
we will useMathJax.Hub.Register.StartupHook
and pass observable fromMathService
So our module will look like this:Step seven: render math
Now everything is ready simply import
MathModule.forRoot()
in module where you want to render math. The component will look like this:and template
Which should render to this
Step eight (bonus)
Here is working stackblitz example https://stackblitz.com/edit/mathjax-example so you can check your progress against implementation
You can also look here for nice implementation of Mathjax with Angular 6:
https://github.com/shubhvjain/angular-mathjax