Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions js/mathjax_startup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/***************************************************************
* Implements startup of MathJax v4
***************************************************************/

// Let's Runestone know that MathJax is ready
const runestoneMathReady = new Promise((resolve) => window.rsMathReady = resolve);

window.MathJax = {
"tex": {
"inlineMath": [
[
"\\(",
"\\)"
]
],
"tags": "none",
"tagSide": "right",
"tagIndent": ".8em",
"packages": {
"[+]": [
"base",
"ams",
"amscd",
"color",
"newcommand",
"knowl"
]
}
},
"options": {
"ignoreHtmlClass": "tex2jax_ignore|ignore-math",
"processHtmlClass": "process-math",
"renderActions": {
"findScript": [
10,
function (doc) {
document.querySelectorAll('script[type^="math/tex"]').forEach(function (node) {
var display = !!node.type.match(/; *mode=display/);
var math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
var text = document.createTextNode('');
node.parentNode.replaceChild(text, node);
math.start = { node: text, delim: '', n: 0 };
math.end = { node: text, delim: '', n: 0 };
doc.math.push(math);
});
},
""
]
}
},
"chtml": {
"scale": 0.98,
"mtextInheritFont": true
},
"loader": {
"load": [
"input/asciimath",
"[tex]/extpfeil",
"[tex]/amscd",
"[tex]/color",
"[tex]/newcommand",
],
"paths": {
"pretext": "_static/pretext/js/lib"
}
},
"startup": {
ready() {
const { Configuration } = MathJax._.input.tex.Configuration;
const configuration = Configuration.create("knowl", {
handler: {
macro: ["knowl"]
}
});

function GetArgumentMML(parser, name) {
const NodeUtil = MathJax._.input.tex.NodeUtil.default;
const arg = parser.ParseArg(name);
if (!NodeUtil.isInferred(arg)) {
return arg;
}
const children = NodeUtil.getChildren(arg);
if (children.length === 1) {
return children[0];
}
const mrow = parser.create("node", "mrow");
NodeUtil.copyChildren(arg, mrow);
NodeUtil.copyAttributes(arg, mrow);
return mrow;
};

let mathjaxKnowl = {};
/**
* Implements \knowl{url}{math}
* @param {TexParser} parser The calling parser.
* @param {string} name The TeX string
*/
mathjaxKnowl.Knowl = function (parser, name) {
const url = parser.GetArgument(name);
const arg = GetArgumentMML(parser, name);
const mrow = parser.create("node", "mrow", [arg], { tabindex: '0', "data-knowl": url });
parser.Push(mrow);
};

const CommandMap = MathJax._.input.tex.TokenMap.CommandMap;
new CommandMap(
"knowl",
{
knowl: ["Knowl"]
},
mathjaxKnowl
);

MathJax.startup.defaultReady();
},
pageReady() {
return MathJax.startup.defaultPageReady().then(function () {
rsMathReady();
}
)
},
}
};
228 changes: 114 additions & 114 deletions xsl/pretext-html.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -12232,127 +12232,127 @@ TODO:
<!-- have mathematics in them (suggested by P. Krautzberger) -->
<xsl:template name="mathjax">
<!-- mathjax configuration -->
<xsl:element name="script">
<xsl:text>&#xa;</xsl:text>
<xsl:text>var runestoneMathReady = new Promise((resolve) => window.rsMathReady = resolve);&#xa;</xsl:text>
<xsl:text>window.MathJax = </xsl:text>
<xsl:call-template name="json">
<xsl:with-param name="content">
<map xmlns="http://www.w3.org/2005/xpath-functions">
<map key="tex">
<array key="inlineMath">
<array>
<string>\(</string>
<string>\)</string>
</array>
</array>
<string key="tags">none</string>
<string key="tagSide">right</string>
<string key="tagIndent">.8em</string>
<map key="packages">
<array key="[+]">
<string>base</string>
<!-- 2023-10-19: this provides backward-compatible behavior -->
<!-- and could be removed at the first sign of trouble -->
<xsl:if test="not(contains($latex-packages-mathjax, '\require{extpfeil}'))">
<string>extpfeil</string>
</xsl:if>
<string>ams</string>
<string>amscd</string>
<string>color</string>
<string>newcommand</string>
<string>knowl</string>
</array>
</map>
</map>
<map key="options">
<string key="ignoreHtmlClass">tex2jax_ignore|ignore-math</string>
<string key="processHtmlClass">process-math</string>
<xsl:if test="$b-has-webwork-reps or $b-has-sage">
<map key="renderActions">
<array key="findScript">
<number>10</number>
<raw>
<xsl:text>function (doc) {&#xa;</xsl:text>
<xsl:text> document.querySelectorAll('script[type^="math/tex"]').forEach(function(node) {&#xa;</xsl:text>
<xsl:text> var display = !!node.type.match(/; *mode=display/);&#xa;</xsl:text>
<xsl:text> var math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);&#xa;</xsl:text>
<xsl:text> var text = document.createTextNode('');&#xa;</xsl:text>
<xsl:text> node.parentNode.replaceChild(text, node);&#xa;</xsl:text>
<xsl:text> math.start = {node: text, delim: '', n: 0};&#xa;</xsl:text>
<xsl:text> math.end = {node: text, delim: '', n: 0};&#xa;</xsl:text>
<xsl:text> doc.math.push(math);&#xa;</xsl:text>
<xsl:text> });&#xa;</xsl:text>
<xsl:text> }</xsl:text>
</raw>
<string></string>
<xsl:choose>
<xsl:when test="$mathjax4-testing">
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the inclusion spot

<script src="_static/pretext/js/mathjax_startup.js"></script>
</xsl:when>
<xsl:otherwise>
<xsl:element name="script">
<xsl:text>&#xa;</xsl:text>
<xsl:text>var runestoneMathReady = new Promise((resolve) => window.rsMathReady = resolve);&#xa;</xsl:text>
<xsl:text>window.MathJax = </xsl:text>
<xsl:call-template name="json">
<xsl:with-param name="content">
<map xmlns="http://www.w3.org/2005/xpath-functions">
<map key="tex">
<array key="inlineMath">
<array>
<string>\(</string>
<string>\)</string>
</array>
</array>
<string key="tags">none</string>
<string key="tagSide">right</string>
<string key="tagIndent">.8em</string>
<map key="packages">
<array key="[+]">
<string>base</string>
<!-- 2023-10-19: this provides backward-compatible behavior -->
<!-- and could be removed at the first sign of trouble -->
<xsl:if test="not(contains($latex-packages-mathjax, '\require{extpfeil}'))">
<string>extpfeil</string>
</xsl:if>
<string>ams</string>
<string>amscd</string>
<string>color</string>
<string>newcommand</string>
<string>knowl</string>
</array>
</map>
</map>
</xsl:if>
</map>
<map key="chtml">
<number key="scale">0.98</number>
<boolean key="mtextInheritFont">true</boolean>
</map>
<map key="loader">
<array key="load">
<string>input/asciimath</string>
<string>[tex]/extpfeil</string>
<string>[tex]/amscd</string>
<string>[tex]/color</string>
<string>[tex]/newcommand</string>
<string>[pretext]/mathjaxknowl3.js</string>
</array>
<map key="paths">
<string key="pretext">
<xsl:value-of select="$html.jslib.dir"/>
</string>
</map>
</map>
<map key="startup">
<xsl:choose>
<xsl:when test="$b-debug-react">
<boolean key="typeset">false</boolean>
</xsl:when>
<xsl:otherwise>
<!-- tell Runestone components that MathJax is all loaded -->
<raw>
<xsl:text>pageReady() {&#xa;</xsl:text>
<xsl:text> return MathJax.startup.defaultPageReady().then(function () {&#xa;</xsl:text>
<xsl:text> console.log("in ready function");&#xa;</xsl:text>
<xsl:text> rsMathReady();&#xa;</xsl:text>
<xsl:text> }&#xa;</xsl:text>
<xsl:text> )}</xsl:text>
</raw>
</xsl:otherwise>
</xsl:choose>
</map>
<!-- optional presentation mode gets clickable, large math -->
<xsl:if test="$b-html-presentation">
<map key="options">
<map key="menuOptions">
<map key="settings">
<string key="zoom">Click</string>
<string key="zscale">300%</string>
<map key="options">
<string key="ignoreHtmlClass">tex2jax_ignore|ignore-math</string>
<string key="processHtmlClass">process-math</string>
<xsl:if test="$b-has-webwork-reps or $b-has-sage">
<map key="renderActions">
<array key="findScript">
<number>10</number>
<raw>
<xsl:text>function (doc) {&#xa;</xsl:text>
<xsl:text> document.querySelectorAll('script[type^="math/tex"]').forEach(function(node) {&#xa;</xsl:text>
<xsl:text> var display = !!node.type.match(/; *mode=display/);&#xa;</xsl:text>
<xsl:text> var math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);&#xa;</xsl:text>
<xsl:text> var text = document.createTextNode('');&#xa;</xsl:text>
<xsl:text> node.parentNode.replaceChild(text, node);&#xa;</xsl:text>
<xsl:text> math.start = {node: text, delim: '', n: 0};&#xa;</xsl:text>
<xsl:text> math.end = {node: text, delim: '', n: 0};&#xa;</xsl:text>
<xsl:text> doc.math.push(math);&#xa;</xsl:text>
<xsl:text> });&#xa;</xsl:text>
<xsl:text> }</xsl:text>
</raw>
<string></string>
</array>
</map>
</xsl:if>
</map>
<map key="chtml">
<number key="scale">0.98</number>
<boolean key="mtextInheritFont">true</boolean>
</map>
<map key="loader">
<array key="load">
<string>input/asciimath</string>
<string>[tex]/extpfeil</string>
<string>[tex]/amscd</string>
<string>[tex]/color</string>
<string>[tex]/newcommand</string>
<string>[pretext]/mathjaxknowl3.js</string>
</array>
<map key="paths">
<string key="pretext">
<xsl:value-of select="$html.jslib.dir"/>
</string>
</map>
</map>
<map key="startup">
<xsl:choose>
<xsl:when test="$b-debug-react">
<boolean key="typeset">false</boolean>
</xsl:when>
<xsl:otherwise>
<!-- tell Runestone components that MathJax is all loaded -->
<raw>
<xsl:text>pageReady() {&#xa;</xsl:text>
<xsl:text> return MathJax.startup.defaultPageReady().then(function () {&#xa;</xsl:text>
<xsl:text> console.log("in ready function");&#xa;</xsl:text>
<xsl:text> rsMathReady();&#xa;</xsl:text>
<xsl:text> }&#xa;</xsl:text>
<xsl:text> )}</xsl:text>
</raw>
</xsl:otherwise>
</xsl:choose>
</map>
<!-- optional presentation mode gets clickable, large math -->
<xsl:if test="$b-html-presentation">
<map key="options">
<map key="menuOptions">
<map key="settings">
<string key="zoom">Click</string>
<string key="zscale">300%</string>
</map>
</map>
</map>
</xsl:if>
</map>
</xsl:if>
</map>
</xsl:with-param>
</xsl:call-template>
<xsl:text>;</xsl:text>
<xsl:text>&#xa;</xsl:text>
</xsl:element>
</xsl:with-param>
</xsl:call-template>
<xsl:text>;</xsl:text>
<xsl:text>&#xa;</xsl:text>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
<!-- mathjax javascript -->
<xsl:element name="script">
<!-- probably should be universal, but only adding for MJ 4 -->
<!-- TODO: make a literal "script" element with this attribute -->
<xsl:if test="$mathjax4-testing">
<xsl:attribute name="type">
<xsl:text>text/javascript</xsl:text>
</xsl:attribute>
</xsl:if>
<xsl:attribute name="src">
<xsl:choose>
<xsl:when test="$mathjax4-testing">
Expand Down