Skip to content

Commit

Permalink
feat(math): Support MathML mphantom and TeX-like phantom, hphantom, v…
Browse files Browse the repository at this point in the history
…phantom
  • Loading branch information
Omikhleia authored and Didier Willis committed Oct 20, 2024
1 parent 3a0ef46 commit 46e5954
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/math/base-elements.lua
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,43 @@ end

function elements.stackbox.output (_, _, _, _) end

elements.phantom = pl.class(elements.stackbox) -- inherit from stackbox
elements.phantom._type = "Phantom"

function elements.phantom:_init (children, special)
-- MathML core 3.3.7:
-- "Its layout algorithm is the same as the mrow element".
-- Also not the MathML states that <mphantom> is sort of legacy, "implemented
-- for compatibility with full MathML. Authors whose only target is MathML
-- Core are encouraged to use CSS for styling."
-- The thing is that we don't have CSS in SILE, so supporting <mphantom> is
-- a must.
elements.stackbox._init(self, "H", children)
self.special = special
end

function elements.phantom:shape ()
elements.stackbox.shape(self)
-- From https://latexref.xyz:
-- "The \vphantom variant produces an invisible box with the same vertical size
-- as subformula, the same height and depth, but having zero width.
-- And \hphantom makes a box with the same width as subformula but
-- with zero height and depth."
if self.special == "v" then
self.width = SILE.types.length()
elseif self.special == "h" then
self.height = SILE.types.length()
self.depth = SILE.types.length()
end
end

function elements.phantom:output (_, _, _)
-- Note the trick here: when the tree is rendered, the node's output
-- function is invoked, then all its children's output functions.
-- So we just cancel the list of children here, before it's rendered.
self.children = {}
end

elements.subscript = pl.class(elements.mbox)
elements.subscript._type = "Subscript"

Expand Down
5 changes: 5 additions & 0 deletions packages/math/texlike.lua
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,11 @@ compileToMathML(
% 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
5 changes: 5 additions & 0 deletions packages/math/typesetter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ 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 text = content[1]
Expand Down

0 comments on commit 46e5954

Please sign in to comment.