Skip to content

Commit

Permalink
Merge pull request #91 from Omikhleia/feat-math-eqno-styling
Browse files Browse the repository at this point in the history
feat: Display math equation numbers now use a resilient style (eqno)
  • Loading branch information
Omikhleia authored Oct 7, 2024
2 parents 55fe608 + a4edebd commit fc17232
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 10 deletions.
48 changes: 48 additions & 0 deletions classes/resilient/book.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,45 @@ function class:_init (options)
self:registerCommand("urlstyle", function (_, content)
SILE.call("style:apply", { name = "url" }, content)
end)

-- Override the standard math:numberingstyle hook to rely on styles,
-- and also to subscribe for cross-references.
-- Package "math" is loaded by the markdown package.
self:registerCommand("math:numberingstyle", function (opts, _)
local text
local stylename = "eqno"
if opts.counter then
local altname = "eqno-" .. opts.counter
local fnSty = self:resolveStyle(altname, true) -- discardable
if not next(fnSty) then
fnSty = self:resolveStyle(stylename) -- fallback
else
stylename = altname
end

local display = fnSty.numbering and fnSty.numbering.display or "arabic"
-- Enforce the display style for the counter
-- (which might be the default 'equation' counter, or a custom one)
SILE.call("set-counter", { id = opts.counter, display = display })
text = self.packages.counters:formatCounter(SILE.scratch.counters[opts.counter])
elseif opts.number then
text = opts.number
else
SU.error("No counter or number provided for math numbering")
end
-- Cross-ref support (from markdown, we can get an id, in most of our packages,
-- we can get a marker, play fair with all)
local mark = opts.id or opts.marker
if mark then
local labelRefs = self.packages.labelrefs
labelRefs:pushLabelRef(text)
SILE.call("style:apply:number", { name = stylename, text = text })
SILE.call("label", { marker = mark })
labelRefs:popLabelRef()
else
SILE.call("style:apply:number", { name = stylename, text = text })
end
end)
end

function class:declareOptions ()
Expand Down Expand Up @@ -382,6 +421,15 @@ function class:registerStyles ()
self:registerStyle("url", { inherit = "code"}, {
})

-- display math equation numberstyle
self:registerStyle("eqno", {}, {
numbering = {
before = { text = "(" },
display = "arabic",
after = { text = ")" }
}
})

-- Special non-standard style for dropcaps (for commands initial-joined and initial-unjoined)
self:registerStyle("dropcap", {}, {
font = { family = "Zallman Caps" },
Expand Down
4 changes: 3 additions & 1 deletion examples/manual-front.sil
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
\begin{document}
\footnote:rule
\define[command=admon]{%
\define[command=admon]{% This is a quick but bad implementation. We ought to have used a style.
\novbreak
\smallskip%
\novbreak
\set[parameter=document.parindent, value=0]{%
\roughbox[enlarge=true, singlestroke=true, preserve=true,
padding=2%fw, bordercolor=#59b24c, fillcolor=230, shadowcolor=#96A8C7, shadow=false]{%
Expand Down
54 changes: 54 additions & 0 deletions examples/manual-styling/advanced/eqno.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## Styling equation numbers

Mathematical equations that are displayed on their own line (in the so-called "display" mode) can be numbered.
This feature is not available in Markdown, but is supported for input documents in Djot or SIL.
In Djot, for instance, you can write, using the default `equation` counter:

```
$$`e^{i\pi} = -1`{numbered=true}
```

The equation numbers are then rendered using the `eqno` numbering style, which is defined as follows by default:

```yaml
eqno:
numbering:
display: "arabic"
before:
text: "("
after:
text: ")"
```
But as any style, this can be adjusted to your needs.
For the sake of this example, we could have used old-style numbers, and put the equation numbers in square brackets:
```yaml
eqno:
font:
features: "+onum"
numbering:
display: "arabic"
before:
text: "["
after:
text: "]"
```
Moreover, in Djot or SIL, you can also specify a custom counter to be used for numbering an equation, starting from 1 on its first occurrence, and automatically incremented.
```
$$`e^{i\pi} = -1`{counter=myname}
```

By default, the `eqno` style applies, but if your style definion file contains a style appropriately named `eqno-myname`, it will be used instead.
This is something you will likely want to do, for these custom counters to have a distinct appearance.
Style inheritance is useful here, as you can define a general style for equation numbers, and then override a subset of its properties for specific counters.
For instance, we could want to switch to alpha numbering for this sample `myname` counter:

```yaml
eqno-myname:
inherit: eqno
numbering:
display: "alpha"
```
1 change: 1 addition & 0 deletions examples/manual-styling/advanced/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ Readers experienced with SILE’s standard packages also have to be aware that t
For instance, the (standard) **url** package provides a `urlstyle` hook command. While you could override it---that is, redefine that command---, please note that this is what the resilient classes already do, so as to delegate the decision to a style conveniently called `urlstyle` too.[^other-sile-hooks]

[^other-sile-hooks]: The same principle actually applies to page numbers, relying on the standard **folio** package. The latter provides a `foliostyle` hook, which the resilient styling system overrides with its own clever logic.
Likewise, the `math:numberstyle` hook from the **math** package is also redefined, to rely on the `eqno` style for display math equation numbers (and actually, if it exist and is non-emppty, to the `eqno-⟨counter⟩` style specific to the counter in question, by default `equation`), as described in the previous section.

In other terms, even for standard SILE packages, we may already have provided a style-aware version of their original customization hooks.
27 changes: 19 additions & 8 deletions examples/sile-resilient-manual-styles.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
md-mark:
style:
decoration:
line: "mark"
color: "orange"
rough: true

blockquote:
origin: "resilient.book"
Expand Down Expand Up @@ -173,6 +167,16 @@ epigraph-text:
paragraph:
align: "justify"

eqno:
origin: "resilient.book"
style:
numbering:
after:
text: ")"
before:
text: "("
display: "arabic"

fancytoc-base:
style:

Expand Down Expand Up @@ -532,6 +536,13 @@ lists-itemize6:
itemize:
symbol: ""

md-mark:
style:
decoration:
color: "orange"
line: "mark"
rough: true

poetry:
origin: "resilient.poetry"
style:
Expand Down Expand Up @@ -1050,9 +1061,9 @@ verbatim:
origin: "resilient.verbatim"
style:
paragraph:
after:
skip: "smallskip"
align: "obeylines"
before:
skip: "smallskip"
after:
skip: "smallskip"

3 changes: 2 additions & 1 deletion examples/sile-resilient-manual.silm
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ sile:
textsubsuper.fake: false
autodoc.highlighting: true
document.baselineskip: 1.2em
typesetter.italicCorrection: true
typesetter.italicCorrection: true # Available in SILE 0.15 (comment out for earlier versions)
packages:
- autodoc-resilient # REQUIRED FOR RESILIENT, do not use regular autodoc
- background # Some of the packages below might not be required...
Expand Down Expand Up @@ -78,6 +78,7 @@ parts:
- manual-styling/advanced/footnotes.md
- manual-styling/advanced/toclevels.md
- manual-styling/advanced/liststyles.md
- manual-styling/advanced/eqno.md
- manual-styling/advanced/other.md
# unfinished
# - manual-styling/captioned.md
Expand Down

0 comments on commit fc17232

Please sign in to comment.