Using MathJax: Difference between revisions
A WASSERMANN (talk | contribs) No edit summary  | 
				A WASSERMANN (talk | contribs) No edit summary  | 
				||
| (6 intermediate revisions by the same user not shown) | |||
| Line 11: | Line 11: | ||
<jsxgraph>  | <jsxgraph>  | ||
var   | var board, k;  | ||
JXG.Options.text.useMathJax = true;  | JXG.Options.text.useMathJax = true;  | ||
board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,20,5,-5], axis:true, showNavigation:true, showCopyright:true});  | |||
k =   | k = board.create('slider',[[-4,-2],[3,-2],[-5,1,5]],{name:'n', snapWidth:1});  | ||
board.create('functiongraph', [function(t) {return JXG.Math.pow(Math.E,t*k.Value());}],{strokeColor:'#ff0000'});  | |||
board.create('text',[-4,7,  | |||
   () => '\\[f(x) = e^{' + k.Value() + 'x}\\]'  |    () => '\\[f(x) = e^{' + k.Value() + 'x}\\]'  | ||
], {fontSize:24});  | ], {fontSize:24});  | ||
| Line 31: | Line 31: | ||
<source lang="php">  | <source lang="php">  | ||
$  | $wgSmjDisplayMath = [ [ "$$", "$$"], ["\[", "\]"] ];  | ||
$  | $wgSmjExtraInlineMath = [[ "\(", "\)"] ];  | ||
</source>  | </source>  | ||
| Line 42: | Line 42: | ||
To display MathJax inside of JSXGraph, the function that is linked to (dynamic) text has to return valid MathJax syntax.  | To display MathJax inside of JSXGraph, the function that is linked to (dynamic) text has to return valid MathJax syntax.  | ||
<source lang="javascript">  | <source lang="javascript">  | ||
board.create('text',[-4,15,  | |||
   () => '\\[f(x) = e^{' + k.Value() + 'x}\\]'  |    () => '\\[f(x) = e^{' + k.Value() + 'x}\\]'  | ||
], {fontSize:24});  | ], {fontSize:24});  | ||
| Line 49: | Line 49: | ||
The complete program code looks like this:  | The complete program code looks like this:  | ||
<source lang="javascript">  | <source lang="javascript">  | ||
var   | var board, k;  | ||
JXG.Options.text.useMathJax = true;  | JXG.Options.text.useMathJax = true;  | ||
board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,20,5,-5], axis:true, showNavigation:true, showCopyright:true});  | |||
k =   | k = board.create('slider',[[-4,-2],[3,-2],[-5,1,5]],{name:'n', snapWidth:1});  | ||
board.create('functiongraph', [function(t) {return JXG.Math.pow(Math.E,t*k.Value());}],{strokeColor:'#ff0000'});  | |||
board.create('text',[-4,15,  | |||
   () => '\\[f(x) = e^{' + k.Value() + 'x}\\]'  |    () => '\\[f(x) = e^{' + k.Value() + 'x}\\]'  | ||
]);  | ]);  | ||
Latest revision as of 11:54, 3 November 2025
This example uses MathJax to render mathematical text like \(\int x^2 dx\).
MathJax is an open-source JavaScript display engine for LaTeX and MathML that works in all modern browsers. It was designed with the goal of consolidating the recent advances in web technologies into a single, definitive, math-on-the-web platform supporting the major browsers and operating systems.
The first solutions how to use MathJax in texts inside of a JSXGraph construction have been worked out by Carol Fisher and Agentyikes. Thank you very much for doing this great work.
Meanwhile, JSXGraph contains full support of MathJax. Here is an example how to enable dynamic MathJax syntax inside of JSXGraph.
The underlying JavaScript code
The first step is to include the freely available MathJax JavaScript library (in addition to the JSXGraph library):
  <script defer src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-svg.js"></script>
In mediawiki, it is sufficient to load the extension "SimpleMathJax" and configure it by setting e.g.
$wgSmjDisplayMath = [ [ "$$", "$$"], ["\[", "\]"] ];
$wgSmjExtraInlineMath = [[ "\(", "\)"] ];
The second step is to enable MathJax rendering in the JSXGraph options:
JXG.Options.text.useMathJax = true;
To display MathJax inside of JSXGraph, the function that is linked to (dynamic) text has to return valid MathJax syntax.
board.create('text',[-4,15,
  () => '\\[f(x) = e^{' + k.Value() + 'x}\\]'
], {fontSize:24});
The complete program code looks like this:
var board, k;
JXG.Options.text.useMathJax = true;
board = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-5,20,5,-5], axis:true, showNavigation:true, showCopyright:true});
k = board.create('slider',[[-4,-2],[3,-2],[-5,1,5]],{name:'n', snapWidth:1});
board.create('functiongraph', [function(t) {return JXG.Math.pow(Math.E,t*k.Value());}],{strokeColor:'#ff0000'});
board.create('text',[-4,15,
  () => '\\[f(x) = e^{' + k.Value() + 'x}\\]'
]);