Skip to content

Commit 19fd61f

Browse files
Omikhleiaalerque
authored andcommitted
fix(math): Avoid page breaks before display math equations
See #2160. This is a conservative workaround. Introducing the predisplay/postdisplay penalties is the right thing to do, but a more general solution to the full issue requires a more subtle handling of in-paragraph display math equations, which is not considered here.
1 parent 626fe7a commit 19fd61f

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

packages/math/init.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,31 @@ function package.declareSettings (_)
5656
type = "VGlue",
5757
default = SILE.types.node.vglue("2ex plus 1pt"),
5858
})
59+
60+
-- Penalties for breaking before and after a display math formula
61+
-- See TeX's \predisplaypenalty and \postdisplaypenalty
62+
SILE.settings:declare({
63+
parameter = "math.predisplaypenalty",
64+
type = "integer",
65+
default = 10000, -- strict no break by default as in (La)TeX
66+
help = "Penalty for breaking before a display math formula",
67+
})
68+
SILE.settings:declare({
69+
parameter = "math.postdisplaypenalty",
70+
type = "integer",
71+
-- (La)TeX's default is 0 (a normal line break penalty allowing a break
72+
-- after a display math formula)
73+
-- See https://github.com/sile-typesetter/sile/issues/2160
74+
-- And see implementation in handleMath(): we are not yet doing the right
75+
-- things with respect to paragraphing, so setting a lower value for now
76+
-- to ease breaking after a display math formula rather than before
77+
-- when the formula is in the middle of a paragraph.
78+
-- (In TeX, these penalties would apply in horizontal mode, with a display
79+
-- math formula being a horizontal full-width box, our implementation
80+
-- currently use them as vertical penalties).
81+
default = -50,
82+
help = "Penalty for breaking after a display math formula",
83+
})
5984
end
6085

6186
function package:registerCommands ()

packages/math/typesetter.lua

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,15 @@ local function handleMath (_, mbox, options)
211211
mbox:shapeTree()
212212

213213
if mode == "display" then
214-
SILE.typesetter:endline()
214+
-- See https://github.com/sile-typesetter/sile/issues/2160
215+
-- We are not excactly doing the right things here with respect to
216+
-- paragraphing expectations.
217+
-- The vertical penalty will flush the previous paragraph, if any.
218+
SILE.call("penalty", { penalty = SILE.settings:get("math.predisplaypenalty"), vertical = true })
215219
SILE.typesetter:pushExplicitVglue(SILE.settings:get("math.displayskip"))
220+
-- Repeating the penalty after the skip does not hurt but should not be
221+
-- necessary if our page builder did its stuff correctly.
222+
SILE.call("penalty", { penalty = SILE.settings:get("math.predisplaypenalty"), vertical = true })
216223
SILE.settings:temporarily(function ()
217224
-- Center the equation in the space available up to the counter (if any),
218225
-- respecting the fixed part of the left and right skips.
@@ -233,9 +240,14 @@ local function handleMath (_, mbox, options)
233240
elseif options.number then
234241
SILE.call("math:numberingstyle", options)
235242
end
236-
SILE.typesetter:endline()
243+
-- The vertical penalty will flush the equation.
244+
-- It must be done in the temporary settings block, because these have
245+
-- to apply as line boxes are being built.
246+
SILE.call("penalty", { penalty = SILE.settings:get("math.postdisplaypenalty"), vertical = true })
237247
end)
238248
SILE.typesetter:pushExplicitVglue(SILE.settings:get("math.displayskip"))
249+
-- Repeating: Same remark as for the predisplay penalty above.
250+
SILE.call("penalty", { penalty = SILE.settings:get("math.postdisplaypenalty"), vertical = true })
239251
else
240252
SILE.typesetter:pushHorizontal(mbox)
241253
end

0 commit comments

Comments
 (0)