Skip to content

Commit

Permalink
Merge 66317dc into 8ff8c62
Browse files Browse the repository at this point in the history
  • Loading branch information
Omikhleia committed May 7, 2024
2 parents 8ff8c62 + 66317dc commit 55e7231
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 7 deletions.
29 changes: 25 additions & 4 deletions packages/math/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function package:_init ()
return value * SILE.settings:get("math.font.size") / 18
end
})
self:loadPackage("counters")
end

function package.declareSettings (_)
Expand Down Expand Up @@ -62,23 +63,30 @@ end
function package:registerCommands ()

self:registerCommand("mathml", function (options, content)
local mode = (options and options.mode) and options.mode or 'text'
local mbox
xpcall(function()
mbox = self:ConvertMathML(content)
end, function(err) print(err); print(debug.traceback()) end)
self:handleMath(mbox, mode)
self:handleMath(mbox, options)
end)

self:registerCommand("math", function (options, content)
local mode = (options and options.mode) and options.mode or "text"
local mbox
xpcall(function()
mbox = self:ConvertMathML(self:compileToMathML({}, self:convertTexlike(content)))
end, function(err) print(err); print(debug.traceback()) end)
self:handleMath(mbox, mode)
self:handleMath(mbox, options)
end)

self:registerCommand("math:numberingstyle", function (options, _)
SILE.typesetter:typeset("(")
if options.counter then
SILE.call("show-counter", { id = options.counter })
elseif options.number then
SILE.typesetter:typeset(options.number)
end
SILE.typesetter:typeset(")")
end)
end

package.documentation = [[
Expand Down Expand Up @@ -344,6 +352,19 @@ Finally, here is a little secret. This notation:
\noindent In other words, the notation using \code{&} and \code{\\\\} is only a syntactic sugar for a two-dimensional array constructed with braces.
\paragraph{Numbered equations}
Equations can be numbered in display mode.
When \autodoc:parameter{numbered=true}, equations are numbered using a default “equation” counter:
\math[mode=display, numbered=true]{e^{i\pi} = -1}
A different counter can be set by using the option \autodoc:parameter{counter=<id>}, and this setting will also enable numbering.
It is also possible to impose direct numbering using the \autodoc:parameter{number=<value>} option.
The default numbering format is \autodoc:example{(n)}, but this style may be overridden by defining a custom \autodoc:command{\math:numberingstyle} command.
The \code{counter} or the direct value \code{number} is passed as a parameter to this hook, as well as any other options.
\paragraph{Missing features}
This package still lacks support for some mathematical constructs, but hopefully we’ll get there.
Among unsupported constructs are: decorating symbols with so-called accents, such as arrows or hats, “over” or “under” braces, and line breaking inside a formula.
Expand Down
28 changes: 25 additions & 3 deletions packages/math/typesetter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ function ConvertMathML (_, content)
end
end

local function handleMath (_, mbox, mode)
local function handleMath (_, mbox, options)
local mode = options and options.mode or 'text'
local counter = SU.boolean(options.numbered, false) and "equation"
counter = options.counter or counter -- overrides the default "equation" counter

if mode == 'display' then
mbox.mode = b.mathMode.display
elseif mode == 'text' then
Expand All @@ -144,10 +148,28 @@ local function handleMath (_, mbox, mode)
if mode == "display" then
SILE.typesetter:endline()
SILE.typesetter:pushExplicitVglue(SILE.settings:get("math.displayskip"))
SILE.call("center", {}, function()
SILE.settings:temporarily(function ()
-- Center the equation in the space available up to the counter (if any),
-- respecting the fixed part of the left and right skips.
local lskip = SILE.settings:get("document.lskip") or SILE.types.node.glue()
local rskip = SILE.settings:get("document.rskip") or SILE.types.node.glue()
SILE.settings:set("document.parindent", SILE.types.node.glue())
SILE.settings:set("current.parindent", SILE.types.node.glue())
SILE.settings:set("document.lskip", SILE.types.node.hfillglue(lskip.width.length))
SILE.settings:set("document.rskip", SILE.types.node.glue(rskip.width.length))
SILE.settings:set("typesetter.parfillskip", SILE.types.node.glue())
SILE.settings:set("document.spaceskip", SILE.types.length("1spc", 0, 0))
SILE.typesetter:pushHorizontal(mbox)
SILE.typesetter:pushExplicitGlue(SILE.nodefactory.hfillglue())
if counter then
options.counter = counter
SILE.call("increment-counter", { id = counter })
SILE.call("math:numberingstyle", options)
elseif options.number then
SILE.call("math:numberingstyle", options)
end
SILE.typesetter:endline()
end)
SILE.typesetter:endline()
SILE.typesetter:pushExplicitVglue(SILE.settings:get("math.displayskip"))
else
SILE.typesetter:pushHorizontal(mbox)
Expand Down
45 changes: 45 additions & 0 deletions tests/feat-math-display-numbered.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
\begin[papersize=a6]{document}
\nofolios
\neverindent
\use[module=packages.lorem]
\use[module=packages.math]
\font[size=11pt]
% Test numbered display math for all options
% and in all alignments

Begin justified
\math[mode=display, numbered=true]{e^{i\pi} = -1}
End justified.

\begin{center}
Begin centered
\math[mode=display, numbered=true]{e^{i\pi} = -1}
End centered
\end{center}

\begin{raggedright}
Begin left-aligned
\math[mode=display, numbered=true]{e^{i\pi} = -1}
End left-aligned.
\end{raggedright}

\begin{raggedleft}
Begin right-aligned
\math[mode=display, numbered=true]{e^{i\pi} = -1}
End right-aligned
\end{raggedleft}

Custom counter
\set-counter[id=my-counter, display=alpha]
\begin[mode=display, counter=my-counter]{math}
\{
\table[columnalign=right center left]{
u_0 &=& C \\
u_n &=& r \times u_{n−1}, \forall n ⩾ 1 \\
}
\end{math}

Direct numbering
\math[mode=display, number=5C]{1 = -e^{i\pi}}

\end{document}
35 changes: 35 additions & 0 deletions tests/feat-math-display-unnumbered.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
\begin[papersize=a6]{document}
\nofolios
\neverindent
\use[module=packages.lorem]
\use[module=packages.math]
\font[size=11pt]
% Test default (unnumbered) display math
% within a blockquote (to check it honors margin skips)
% and in all alignments

\begin{blockquote}
Begin justified
\math[mode=display]{e^{i\pi} = -1}
End justified.

\begin{center}
Begin centered
\math[mode=display]{e^{i\pi} = -1}
End centered
\end{center}

\begin{raggedright}
Begin left-aligned
\math[mode=display]{e^{i\pi} = -1}
End left-aligned.
\end{raggedright}

\begin{raggedleft}
Begin right-aligned
\math[mode=display]{e^{i\pi} = -1}
End right-aligned
\end{raggedleft}
\end{blockquote}

\end{document}

0 comments on commit 55e7231

Please sign in to comment.