Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a subset of mathvariants #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mathml2latex",
"version": "1.1.3",
"version": "1.1.4",
"description": "Convert mathml to latex.",
"author": "Mika",
"license": "MIT",
Expand Down
32 changes: 27 additions & 5 deletions src/mathml2latex.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,38 +63,60 @@ function parseLeaf(node) {
return r;
}

// References:
// - https://w3c.github.io/mathml-core/#the-mathvariant-attribute
// - https://ftp.tu-chemnitz.de/pub/tex/info/symbols/comprehensive/symbols-a4.pdf#page=123
function formatMathVariant (node, it) {
const mathVariant = NodeTool.getAttr(node, "mathvariant", "").toLowerCase()
const substitutions = {
"normal": "\\mathrm",
"bold": "\\mathbf",
"italic": "\\mathrm",
"bold-italic": "\\boldsymbol",
"double-struck": "\\mathbb",
"script": "\\mathscr",
"fraktur": "\\mathfrak",
"sans-serif": "\\mathsf"
}
const substituted = substitutions[mathVariant]
const hasSubstitution = !!substituted

return hasSubstitution ? `${substituted}{${it}}` : it
}

// operator token, mathematical operators
function parseOperator(node) {
let it = NodeTool.getNodeText(node).trim();
it = MathSymbol.parseOperator(it);
return escapeSpecialChars(it);
return formatMathVariant(node, escapeSpecialChars(it));
}

// Math identifier
function parseElementMi(node){
let it = NodeTool.getNodeText(node).trim();
it = MathSymbol.parseIdentifier(it);
return escapeSpecialChars(it);
return formatMathVariant(node, escapeSpecialChars(it));
}

// Math Number
function parseElementMn(node){
let it = NodeTool.getNodeText(node).trim();
return escapeSpecialChars(it);
return formatMathVariant(node, escapeSpecialChars(it));
}

// Math String
function parseElementMs(node){
const content = NodeTool.getNodeText(node).trimRight();
const it = escapeSpecialChars(content);
return ['"', it, '"'].join('');
return ['"', formatMathVariant(node, it), '"'].join('');
}

// Math Text

function parseElementMtext(node){
const content = NodeTool.getNodeText(node)
const it = escapeSpecialChars(content);
return `\\text{${it}}`;
return `\\text{${formatMathVariant(node, it)}}`
}

// Math glyph (image)
Expand Down
21 changes: 20 additions & 1 deletion test/test-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,27 @@ const names = [

names.forEach(testOverlapFunName)


test("mo-math-func-name(overlap), multiply names", convert({
from: '<mo>sin</mo><mo>sinh</mo>',
to: '\\sin \\sinh ',
}));

const mathVariantPairs = [
["normal", "\\mathrm"],
["bold", "\\mathbf"],
["italic", "\\mathnormal"],
["bold-italic", "\\bm"],
["double-struck", "\\mathbb"],
["script", "\\mathscr"],
["fraktur", "\\mathfrak"],
["sans-serif", "\\mathsf"],
]

function testMathVariant([variant, translated]) {
test(`mi-mathvariant: ${variant}`, convert({
from: `<mi mathvariant="${variant}">R</mi>`,
to: `${translated}{R}`
}));
}

mathVariantPairs.forEach(testMathVariant)