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

Support MathML mphantom and TeX-like phantom, hphantom, vphantom #2143

Merged
merged 1 commit into from
Oct 29, 2024
Merged
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
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
Loading