frontend: centre the display formula, and let one be set left or right

Display formulas were sitting against the left margin with only their number at
the right, and the cause was a rule I had written myself: the paragraph's
stylesheet set `text-align: left` on `.katex`, which is the very element KaTeX
centres a display formula in. A well-meant attempt to keep a formula from being
justified apart had quietly overridden the centring, and it applied to every
formula in every paper — measured on a paragraph of known width, the formula's
midpoint sat 607px left of centre.

The override is gone; the rule carries only a font size now, and a comment says
why nothing about alignment belongs there.

`paper_sentence.formula_align` then adds the choice the writer asked for: a
display formula can be `center` (the default, and what a paper wants almost
always), `left` or `right`. It is per sentence rather than a template-field
setting — one paragraph legitimately mixes a centred equation with a
right-aligned continuation, and a field describes a heading, not a formula — and
the paragraph editor offers it only on rows that contain display maths, since
only those have a line of their own. The equation number is untouched: `\tag{1}`
prints against the right margin in all three cases, as every numbered style
does.

Measured in the browser on a 1276px paragraph: centred puts the formula 596px
from each edge, left puts it at 0, right puts it at 1225, and the tag is at the
right in every case. 56 paper checks (2 new: the round trip and the refusal of
an alignment the renderer does not know), 61 reference checks, 99 MCP checks,
`vue-tsc` and the build all pass.
This commit is contained in:
2026-09-23 13:14:52 +08:00
parent c65efa32ac
commit e8e129221c
9 changed files with 228 additions and 18 deletions
+16 -1
View File
@@ -31,12 +31,16 @@ is assembled on read from the paper's template plus these rows. See
from typing import TYPE_CHECKING
from sqlalchemy import Boolean, ForeignKey, Index, Integer, Text, text
from sqlalchemy import Boolean, ForeignKey, Index, Integer, String, Text, text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.base import Base
from app.models.mixins import TimestampMixin
#: How a display formula may be set on its line. ``center`` is what a paper
#: wants almost always, and the default for every existing row.
FORMULA_ALIGNMENTS: tuple[str, ...] = ("left", "center", "right")
if TYPE_CHECKING: # pragma: no cover - typing only
from app.models.paper import Paper
from app.models.paper_sentence_reference import PaperSentenceReference
@@ -101,6 +105,17 @@ class PaperSentence(TimestampMixin, Base):
server_default=text("0"),
)
#: How a display formula in this sentence sits on its line: one of
#: :data:`FORMULA_ALIGNMENTS`. Meaningless for a sentence with no display
#: maths, and it never moves the equation number — ``\tag{1}`` is at the
#: right margin whatever this says, as every numbered style prints it.
formula_align: Mapped[str] = mapped_column(
String(8),
nullable=False,
default="center",
server_default=text("'center'"),
)
paper: Mapped["Paper"] = relationship(back_populates="sentences")
#: References quoted by this sentence, in citation order. Eager-loaded