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

Fix math mathvariants #2147

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
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
314 changes: 163 additions & 151 deletions packages/math/base-elements.lua

Large diffs are not rendered by default.

95 changes: 82 additions & 13 deletions packages/math/texlike.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ local mathGrammar = function (_ENV)
return ret
end
local group = P"{" * V"mathlist" * (P"}" + E("`}` expected"))
-- Simple amsmath-like \text command (no embedded math)
local textgroup = P"{" * C((1-P"}")^1) * (P"}" + E("`}` expected"))
local element_no_infix =
V"def" +
V"text" + -- Important: before command
V"command" +
group +
V"argument" +
Expand Down Expand Up @@ -115,6 +118,11 @@ local mathGrammar = function (_ENV)
sub = element_no_infix * _ * P"_" * _ * element_no_infix
atom = natural + C(utf8code - S"\\{}%^_&") +
(P"\\{" + P"\\}") / function (s) return string.sub(s, -1) end
text = (
P"\\text" *
Cg(parameters, "options") *
textgroup
)
command = (
P"\\" *
Cg(ctrl_sequence_name, "command") *
Expand Down Expand Up @@ -252,6 +260,23 @@ local compileToStr = function (argEnv, mathlist)
end
end

local function isBigOperator (tree)
if tree.command ~= "mo" then
return false
end
-- Case \mo[atom=big]{ops}
-- E.g. \mo[atom=big]{lim}
if tree.options and tree.options.atom == "big" then
return true
end
-- Case \mo{ops} where ops is registered as big operator (unicode-symbols)
-- E.g. \mo{∑) or \sum
if tree[1] and symbolDefaults[tree[1]] and symbolDefaults[tree[1]].atom == atomType.bigOperator then
return true
end
return false
end

local function compileToMathML_aux (_, arg_env, tree)
if type(tree) == "string" then
return tree
Expand Down Expand Up @@ -328,21 +353,13 @@ local function compileToMathML_aux (_, arg_env, tree)
tree.options = {}
-- Translate TeX-like sub/superscripts to `munderover` or `msubsup`,
-- depending on whether the base is a big operator
elseif tree.id == "sup" and tree[1].command == "mo" and tree[1].atom == atomType.bigOperator then
elseif tree.id == "sup" and isBigOperator(tree[1]) then
tree.command = "mover"
elseif tree.id == "sub" and tree[1].command == "mo" and symbolDefaults[tree[1][1]].atom == atomType.bigOperator then
elseif tree.id == "sub" and isBigOperator(tree[1]) then
tree.command = "munder"
elseif
tree.id == "subsup"
and tree[1].command == "mo"
and symbolDefaults[tree[1][1]].atom == atomType.bigOperator
then
elseif tree.id == "subsup" and isBigOperator(tree[1])then
tree.command = "munderover"
elseif
tree.id == "supsub"
and tree[1].command == "mo"
and symbolDefaults[tree[1][1]].atom == atomType.bigOperator
then
elseif tree.id == "supsub" and isBigOperator(tree[1]) then
tree.command = "munderover"
local tmp = tree[2]
tree[2] = tree[3]
Expand All @@ -365,6 +382,8 @@ local function compileToMathML_aux (_, arg_env, tree)
return compileToMathML_aux(nil, compiledArgs, tree[1])
end)
return nil
elseif tree.id == "text" then
tree.command = "mtext"
elseif tree.id == "command" and commands[tree.command] then
local argTypes = commands[tree.command][1]
local cmdFun = commands[tree.command][2]
Expand Down Expand Up @@ -425,7 +444,7 @@ local function printMathML (tree)
if tree.options then
local options = {}
for k, v in pairs(tree.options) do
table.insert(options, k .. "=" .. v)
table.insert(options, k .. "=" .. tostring(v))
end
if #options > 0 then
result = result .. "[" .. table.concat(options, ", ") .. "]"
Expand Down Expand Up @@ -480,6 +499,21 @@ compileToMathML(
\def{bi}{\mi[mathvariant=bold-italic]{#1}}
\def{dsi}{\mi[mathvariant=double-struck]{#1}}

\def{lim}{\mo[atom=big]{lim}}

% From amsmath:
\def{to}{\mo[atom=bin]{→}}
\def{gcd}{\mo[atom=big]{gcd}}
\def{sup}{\mo[atom=big]{sup}}
\def{inf}{\mo[atom=big]{inf}}
\def{max}{\mo[atom=big]{max}}
\def{min}{\mo[atom=big]{min}}
% Those use U+202F NARROW NO-BREAK SPACE in their names
\def{limsup}{\mo[atom=big]{lim sup}}
\def{liminf}{\mo[atom=big]{lim inf}}
\def{projlim}{\mo[atom=big]{proj lim}}
\def{injlim}{\mo[atom=big]{inj lim}}

% Standard spaces gleaned from plain TeX
\def{thinspace}{\mspace[width=thin]}
\def{negthinspace}{\mspace[width=-thin]}
Expand All @@ -496,9 +530,44 @@ compileToMathML(
\def{quad}{\mspace[width=1em]}
\def{qquad}{\mspace[width=2em]}

% MathML says a single-character identifier must be in italic by default.
% TeX however has the following Greek capital macros rendered in upright shape.
% It so common that you've probably never seen Γ(x) written with an italic gamma.
\def{Gamma}{\mi[mathvariant=normal]{Γ}}
\def{Delta}{\mi[mathvariant=normal]{Δ}}
\def{Theta}{\mi[mathvariant=normal]{Θ}}
\def{Lambda}{\mi[mathvariant=normal]{Λ}}
\def{Xi}{\mi[mathvariant=normal]{Ξ}}
\def{Pi}{\mi[mathvariant=normal]{Π}}
\def{Sigma}{\mi[mathvariant=normal]{Σ}}
\def{Upsilon}{\mi[mathvariant=normal]{Υ}}
\def{Phi}{\mi[mathvariant=normal]{Φ}}
\def{Psi}{\mi[mathvariant=normal]{Ψ}}
\def{Omega}{\mi[mathvariant=normal]{Ω}}
% Some calligraphic (script), fraktur, double-struck styles:
% Convenience for compatibility with LaTeX.
\def{mathcal}{\mi[mathvariant=script]{#1}}
\def{mathfrak}{\mi[mathvariant=fraktur]{#1}}
\def{mathbb}{\mi[mathvariant=double-struck]{#1}}
% Some style-switching commands for compatibility with LaTeX math.
% Caveat emptor: LaTeX would allow these to apply to a whole formula.
% We can't do that in MathML, as mathvariant applies to token elements only.
% Also note that LaTeX and related packages may have many more such commands.
% We only provide a few common ('historical') ones here.
\def{mathrm}{\mi[mathvariant=normal]{#1}}
\def{mathbf}{\mi[mathvariant=bold]{#1}}
\def{mathit}{\mi[mathvariant=italic]{#1}}
\def{mathsf}{\mi[mathvariant=sans-serif]{#1}}
\def{mathtt}{\mi[mathvariant=monospace]{#1}}

% Modulus operator forms
\def{bmod}{\mo{mod}}
\def{pmod}{\quad(\mo{mod} #1)}

% Phantom commands from TeX/LaTeX
\def{phantom}{\mphantom{#1}}
\def{hphantom}{\mphantom[special=h]{#1}}
\def{vphantom}{\mphantom[special=v]{#1}}
]==],
})
)
Expand Down
41 changes: 32 additions & 9 deletions packages/math/typesetter.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
-- Interpret a MathML or TeX-like AST, typeset it and add it to the output.
local b = require("packages.math.base-elements")
local syms = require("packages.math.unicode-symbols")
local mathvariants = require("packages.math.unicode-mathvariants")
local mathVariantToScriptType, scriptType = mathvariants.mathVariantToScriptType, mathvariants.scriptType

-- Shorthands for atom types, used in the `atom` command option
local atomTypeShort = {
Expand Down Expand Up @@ -41,17 +43,22 @@ function ConvertMathML (_, content)
return b.stackbox("V", convertChildren(content))
elseif content.command == "mrow" then
return b.stackbox("H", convertChildren(content))
elseif content.command == "mphantom" then
-- MathML's standard mphantom corresponds to TeX's \phantom only.
-- Let's support a special attribute "h" or "v" for TeX-like \hphantom or \vphantom.
local special = content.options.special
return b.phantom(convertChildren(content), special)
elseif content.command == "mi" then
local script = content.options.mathvariant and b.mathVariantToScriptType(content.options.mathvariant)
local script = content.options.mathvariant and mathVariantToScriptType(content.options.mathvariant)
local text = content[1]
if type(text) ~= "string" then
SU.error("mi command contains " .. text .. ", which is not text")
SU.error("mi command contains content which is not text")
end
script = script or (luautf8.len(text) == 1 and b.scriptType.italic or b.scriptType.upright)
script = script or (luautf8.len(text) == 1 and scriptType.italic or scriptType.upright)
return b.text("identifier", {}, script, text)
elseif content.command == "mo" then
local script = content.options.mathvariant and b.mathVariantToScriptType(content.options.mathvariant)
or b.scriptType.upright
local script = content.options.mathvariant and mathVariantToScriptType(content.options.mathvariant)
or scriptType.upright
local text = content[1]
local attributes = {}
if syms.symbolDefaults[text] then
Expand All @@ -67,15 +74,15 @@ function ConvertMathML (_, content)
end
end
if type(text) ~= "string" then
SU.error("mo command contains " .. text .. ", which is not text")
SU.error("mo command contains content which is not text")
end
return b.text("operator", attributes, script, text)
elseif content.command == "mn" then
local script = content.options.mathvariant and b.mathVariantToScriptType(content.options.mathvariant)
or b.scriptType.upright
local script = content.options.mathvariant and mathVariantToScriptType(content.options.mathvariant)
or scriptType.upright
local text = content[1]
if type(text) ~= "string" then
SU.error("mn command contains " .. text .. ", which is not text")
SU.error("mn command contains content which is not text")
end
if string.sub(text, 1, 1) == "-" then
text = "−" .. string.sub(text, 2)
Expand Down Expand Up @@ -129,13 +136,29 @@ function ConvertMathML (_, content)
local children = convertChildren(content)
-- "The <msqrt> element generates an anonymous <mrow> box called the msqrt base
return b.sqrt(b.stackbox("H", children))
elseif content.command == "mroot" then
local children = convertChildren(content)
return b.sqrt(children[1], children[2])
elseif content.command == "mtable" or content.command == "table" then
local children = convertChildren(content)
return b.table(children, content.options)
elseif content.command == "mtr" then
return b.mtr(convertChildren(content))
elseif content.command == "mtd" then
return b.stackbox("H", convertChildren(content))
elseif content.command == "mtext" or content.command == "ms" then
if #content > 1 then
SU.error("Wrong number of children in " .. content.command .. ": " .. #content)
end
local text = content[1] or "" -- empty mtext is allowed, and found in examples...
if type(text) ~= "string" then
SU.error(content.command .. " command contains content which is not text")
end
-- MathML Core 3.2.1.1 Layout of <mtext> has some wording about forced line breaks
-- and soft wrap opportunities: ignored here.
-- There's also some explanations about CSS, italic correction etc. which we ignore too.
text = text:gsub("[\n\r]", " ")
return b.text("string", {}, scriptType.upright, text:gsub("%s+", " "))
else
SU.error("Unknown math command " .. content.command)
end
Expand Down
Loading
Loading