Merge pull request #86 from DARC-e-V/feature/thousand-separator

feature: thousand separators and exponent to support siunitx
This commit is contained in:
Matthias Jung
2026-02-15 12:53:56 +01:00
committed by GitHub
+94 -5
View File
@@ -1,11 +1,100 @@
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
function formatThousands(input) {
let s = String(input).trim();
// Extract exponent (e or E) and convert to \cdot 10^{...}
let exponent = "";
const expMatch = s.match(/([eE])([+-]?\d+)$/);
if (expMatch) {
let expValue = expMatch[2];
// Remove leading "+"
if (expValue[0] === "+") {
expValue = expValue.slice(1);
}
// Parse exponent as a numeric value
const expNum = parseInt(expValue, 10);
// Suppress exponent if it is 0 (no \cdot 10^0 shown)
if (expNum !== 0) {
exponent = "\\cdot 10^{" + expNum + "}";
}
s = s.slice(0, -expMatch[0].length);
}
// Sign (+, -, \pm). Also accept "\\pm" (double-escaped backslash) and optional whitespace.
let sign = "";
const pmMatch = s.match(/^(?:\\\\pm|\\pm)\s*/);
if (pmMatch) {
sign = "\\pm";
s = s.slice(pmMatch[0].length);
} else if (s[0] === "+" || s[0] === "-") {
sign = s[0];
s = s.slice(1);
}
// Find decimal point (last . or ,)
let intPart = s;
let fracSep = "";
let fracDigits = "";
const decMatch = s.match(/([.,])(\d+)$/);
if (decMatch) {
intPart = s.slice(0, decMatch.index);
fracSep = decMatch[1];
fracDigits = decMatch[2];
}
// Thousand grouping in integer part
if (intPart.length >= 5) {
intPart = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, "\\,");
}
// Thousand grouping in fractional part (from left in 3-digit blocks)
if (fracDigits.length >= 4) {
fracDigits = fracDigits.replace(/(\d{3})(?=\d)/g, "$1\\,");
}
// Reassemble
return sign + intPart + (fracDigits ? (fracSep + fracDigits) : "") + exponent;
}
var macros = { var macros = {
",": (context) => context.future()?.text === " " ? "{\\char`,}" : "\\char`,", ",": (context) => context.future()?.text === " " ? "{\\char`,}" : "\\char`,",
"\\qty": "{#1\\,\\mathrm{#2}}", "\\num": (context) => {
"\\qtyrange": "{#1\\text{--}#2\\,\\mathrm{#3}}", const [arg] = context.consumeArgs(1);
const raw = arg.reverse().map(t => t.text).join("");
return formatThousands(raw);
},
"\\qty": (context) => {
const [numTok, unitTok] = context.consumeArgs(2);
const rawNum = numTok.reverse().map(t => t.text).join("");
const unit = unitTok.reverse().map(t => t.text).join("");
const formattedNum = formatThousands(rawNum);
if (unit.startsWith("\\degree")) {
return formattedNum + "\\mathrm{" + unit + "}";
} else {
return formattedNum + "\\,\\mathrm{" + unit + "}";
}
},
"\\qtyrange": (context) => {
const [aTok, bTok, unitTok] = context.consumeArgs(3);
const aRaw = aTok.reverse().map(t => t.text).join("");
const bRaw = bTok.reverse().map(t => t.text).join("");
const unit = unitTok.reverse().map(t => t.text).join("");
const aFmt = formatThousands(aRaw);
const bFmt = formatThousands(bRaw);
if (unit.startsWith("\\degree")) {
return aFmt + "\\text{--}\\," + bFmt + "\\mathrm{" + unit + "}";
} else {
return aFmt + "\\,\\text{--}\\," + bFmt + "\\,\\mathrm{" + unit + "}";
}
},
"\\unit": "{\\mathrm{#1}}", "\\unit": "{\\mathrm{#1}}",
"\\num": "{#1}",
"\\squared": "{^{2}}", "\\squared": "{^{2}}",
"\\cubed": "{^{3}}", "\\cubed": "{^{3}}",
"\\per": "/", "\\per": "/",
@@ -52,8 +141,7 @@ document.addEventListener("DOMContentLoaded", function() {
"\\tesla": "\\text{T}", "\\tesla": "\\text{T}",
"\\bit": "\\text{b}", "\\bit": "\\text{b}",
"\\baud": "\\text{Bd}", "\\baud": "\\text{Bd}",
"\\degree": "^\\circ", "\\degreeCelsius": "\\,\\degree\\text{C}",
"\\degreeCelsius": "^\\circ\\text{C}",
}; };
renderMathInElement(document.body, { renderMathInElement(document.body, {
@@ -66,4 +154,5 @@ document.addEventListener("DOMContentLoaded", function() {
throwOnError: false, throwOnError: false,
macros: macros macros: macros
}); });
}); });