Commit 12f4a6bf authored by Henrik Tramberend's avatar Henrik Tramberend
Browse files

Formatting

parent 8a6a1aed
......@@ -12,7 +12,7 @@
* - disable SVG font caches, since it doesn't work in speaker notes
* - reset menu settings in localStorage
* - use promise mechanism to ensure that math is typset before PDF print
* - fix links generated by referencing equations to jump to the slide
* - fix links generated by referencing equations to jump to the slide
* containing the referenced equation
* - inject class=fragment to incrementally show multi-line equations
* (if MathJax element in enclosed in div.math-incremental)
......@@ -23,105 +23,96 @@
"use strict";
let RevealMath = window.RevealMath || (function(){
let options = Reveal.getConfig().math || {};
let mathjax = options.mathjax || 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/';
let url = mathjax + 'tex-svg.js';
function loadScript( url, callback )
{
var head = document.querySelector( 'head' );
var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.id = 'MathJax-script';
script.src = url;
// Wrapper for callback to make sure it only fires once
var finish = function()
{
if( typeof callback === 'function' )
{
callback.call();
callback = null;
}
}
script.onload = finish;
// IE
script.onreadystatechange = function() {
if ( this.readyState === 'loaded' ) {
finish();
}
}
// Normal browsers
head.appendChild( script );
}
let RevealMath =
window.RevealMath ||
(function () {
let options = Reveal.getConfig().math || {};
let mathjax =
options.mathjax || "https://cdn.jsdelivr.net/npm/mathjax@3/es5/";
let url = mathjax + "tex-svg.js";
function loadScript(url, callback) {
var head = document.querySelector("head");
var script = document.createElement("script");
script.type = "text/javascript";
script.id = "MathJax-script";
script.src = url;
// Wrapper for callback to make sure it only fires once
var finish = function () {
if (typeof callback === "function") {
callback.call();
callback = null;
}
};
script.onload = finish;
// IE
script.onreadystatechange = function () {
if (this.readyState === "loaded") {
finish();
}
};
// Normal browsers
head.appendChild(script);
}
/*
* correct links generated by referencing equations, such that they
* point to the slide containing the referenced equation.
* Requires that each slide has a CSS id (as generated e.g. by pandoc/decker)
*/
function fixLinks()
{
for (var a of document.getElementsByTagName("a"))
{
var href = a.href;
if (href.baseVal)
{
var label = href.baseVal;
if (label.includes("#mjx-eqn"))
{
label = decodeURIComponent(label.substring(1));
var eqn = document.getElementById(label);
if (eqn)
{
var s = eqn.closest("section");
if (s)
{
a.href.baseVal = location.origin + location.pathname + "#" + s.id;
}
}
}
function fixLinks() {
for (var a of document.getElementsByTagName("a")) {
var href = a.href;
if (href.baseVal) {
var label = href.baseVal;
if (label.includes("#mjx-eqn")) {
label = decodeURIComponent(label.substring(1));
var eqn = document.getElementById(label);
if (eqn) {
var s = eqn.closest("section");
if (s) {
a.href.baseVal =
location.origin + location.pathname + "#" + s.id;
}
}
}
}
}
}
/*
* If a multi-line equation is enclosed in a div with class math-incremental,
* then add class fragment to the individual rows of the equation, making it
* appear row-by-row.
*/
function setupMathIncremental()
{
// unlabeled equations
for (let mrow of document.querySelectorAll('.reveal .math-incremental mjx-container svg g[data-mml-node="mtable"]:first-of-type > g[data-mml-node="mtr"]')) {
mrow.classList.add("fragment");
}
// unlabeled equations
for (let mrow of document.querySelectorAll('.reveal .math-incremental mjx-container svg g[data-mml-node="mtable"]:first-of-type g[data-mml-node="mlabeledtr"]')) {
mrow.classList.add("fragment");
}
function setupMathIncremental() {
// unlabeled equations
for (let mrow of document.querySelectorAll(
'.reveal .math-incremental mjx-container svg g[data-mml-node="mtable"]:first-of-type > g[data-mml-node="mtr"]'
)) {
mrow.classList.add("fragment");
}
// unlabeled equations
for (let mrow of document.querySelectorAll(
'.reveal .math-incremental mjx-container svg g[data-mml-node="mtable"]:first-of-type g[data-mml-node="mlabeledtr"]'
)) {
mrow.classList.add("fragment");
}
}
/*
* Inject CSS rules that (i) make SVG equations automatically shrink down to
* fit the enclosing container and (ii) remove pointer events from the
* fit the enclosing container and (ii) remove pointer events from the
* equation parts, such that Reveal's zoom plugin work nicely on equations, too.
*/
function injectStyle()
{
const style = document.createElement('style');
style.textContent = String.raw`
function injectStyle() {
const style = document.createElement("style");
style.textContent = String.raw`
/* fit equation into container */
mjx-container > svg {
object-fit: contain;
......@@ -139,113 +130,104 @@ let RevealMath = window.RevealMath || (function(){
pointer-events: all;
}
`;
document.head.append(style);
document.head.append(style);
}
return {
init: function() {
// remove menu settings, which are stored in localStorage.
// otherwise user could select CHTML renderer, which is not
// installed in decker.
if (window.localStorage)
{
window.localStorage.removeItem("MathJax-Menu-Settings");
}
// configure through global MathJax object
window.MathJax = {
loader: {
load: ['[tex]/ams'],
typeset: false
},
startup: {
ready: () => {
console.log('mathjax loaded');
//MathJax.startup.defaultReady();
}
},
svg: {
scale: 0.9, // global scaling factor for all expressions
minScale: .5, // smallest scaling factor to use
matchFontHeight: false, // true to match ex-height of surrounding font
mtextInheritFont: true, // true to make mtext elements use surrounding font
merrorInheritFont: true, // true to make merror text use surrounding font
mathmlSpacing: false, // true for MathML spacing rules, false for TeX rules
skipAttributes: {}, // RFDa and other attributes NOT to copy to the output
exFactor: .5, // default size of ex in em units
displayAlign: 'center', // default for indentalign when set to 'auto'
displayIndent: '0', // default for indentshift when set to 'auto'
fontCache: 'none', // or 'global' or 'none'
localID: null, // ID to use for local font cache (for single equation processing)
internalSpeechTitles: true, // insert <title> tags with speech content
titleID: 0 // initial id number to use for aria-labeledby titles
},
tex: {
tags: 'ams',
packages: {
'[+]': ['ams']
},
inlineMath: [ // start/end delimiter pairs for in-line math
['$', '$'],
['\\(', '\\)']
],
displayMath: [ // start/end delimiter pairs for display math
['$$', '$$'],
['\\[', '\\]']
]
},
options: {
enableMenu: false,
// disable assistive-mml, since it messes up speaker notes
menuOptions: {
settings: {
assistiveMml: false
}
},
renderActions: {
assistiveMml: [] // disable assistive mathml
}
}
};
// add user-defined Latex macros
let macros = { fragment: [ "\\class{fragment}{#1}", 1 ] };
if (options.macros)
{
macros = Object.assign(macros, options.macros);
}
window.MathJax.tex.macros = macros;
// use promise mechanism to make sure that math typesetting
// is performend before Reveal fires ready-event or
// generates a PDF
return new Promise( (resolve) => {
// load mathjax script
loadScript( url, () => {
// Typeset followed by an immediate reveal.js layout since
// the typesetting process could affect slide height
window.MathJax.startup.defaultReady();
MathJax.startup.promise.then(() => {
console.log("mathjax typeset done");
Reveal.layout();
fixLinks();
setupMathIncremental();
injectStyle();
resolve();
});
});
});
return {
init: function () {
// remove menu settings, which are stored in localStorage.
// otherwise user could select CHTML renderer, which is not
// installed in decker.
if (window.localStorage) {
window.localStorage.removeItem("MathJax-Menu-Settings");
}
}
})();
// configure through global MathJax object
window.MathJax = {
loader: {
load: ["[tex]/ams"],
typeset: false,
},
startup: {
ready: () => {
console.log("mathjax loaded");
//MathJax.startup.defaultReady();
},
},
svg: {
scale: 0.9, // global scaling factor for all expressions
minScale: 0.5, // smallest scaling factor to use
matchFontHeight: false, // true to match ex-height of surrounding font
mtextInheritFont: true, // true to make mtext elements use surrounding font
merrorInheritFont: true, // true to make merror text use surrounding font
mathmlSpacing: false, // true for MathML spacing rules, false for TeX rules
skipAttributes: {}, // RFDa and other attributes NOT to copy to the output
exFactor: 0.5, // default size of ex in em units
displayAlign: "center", // default for indentalign when set to 'auto'
displayIndent: "0", // default for indentshift when set to 'auto'
fontCache: "none", // or 'global' or 'none'
localID: null, // ID to use for local font cache (for single equation processing)
internalSpeechTitles: true, // insert <title> tags with speech content
titleID: 0, // initial id number to use for aria-labeledby titles
},
tex: {
tags: "ams",
packages: {
"[+]": ["ams"],
},
inlineMath: [
// start/end delimiter pairs for in-line math
["$", "$"],
["\\(", "\\)"],
],
displayMath: [
// start/end delimiter pairs for display math
["$$", "$$"],
["\\[", "\\]"],
],
},
options: {
enableMenu: false,
// disable assistive-mml, since it messes up speaker notes
menuOptions: {
settings: {
assistiveMml: false,
},
},
renderActions: {
assistiveMml: [], // disable assistive mathml
},
},
};
// add user-defined Latex macros
let macros = { fragment: ["\\class{fragment}{#1}", 1] };
if (options.macros) {
macros = Object.assign(macros, options.macros);
}
window.MathJax.tex.macros = macros;
// use promise mechanism to make sure that math typesetting
// is performend before Reveal fires ready-event or
// generates a PDF
return new Promise((resolve) => {
// load mathjax script
loadScript(url, () => {
// Typeset followed by an immediate reveal.js layout since
// the typesetting process could affect slide height
window.MathJax.startup.defaultReady();
MathJax.startup.promise.then(() => {
console.log("mathjax typeset done");
Reveal.layout();
fixLinks();
setupMathIncremental();
injectStyle();
resolve();
});
});
});
},
};
})();
Reveal.registerPlugin( 'math', RevealMath );
Reveal.registerPlugin("math", RevealMath);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment