Files
paper-doc/backend/alembic/versions/a1d47c6f2e58_add_formula_align.py
govin e8e129221c 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.
2026-09-23 13:14:52 +08:00

58 lines
1.9 KiB
Python

"""let a formula say how it is set on its line
Display maths is centred by default, and that is what a paper wants almost
every time — but not always. A derivation that continues a previous line, or a
formula the writer wants against the right margin, is a layout decision about
*that formula*, and there was nowhere to record it: the renderer centred
everything.
So the sentence carries it: ``formula_align`` is ``center`` (the default),
``left`` or ``right``, and it only means anything for a sentence that contains
display maths (``$$ … $$``). The equation number is unaffected — ``\\tag{1}``
is always at the right margin, which is the convention in every style that
numbers equations.
Why the sentence rather than the template field
-----------------------------------------------
A field (``template_title``) describes a *heading*: its text, level and
typography. Alignment belongs to one formula, and one paragraph legitimately
mixes them — a centred equation followed by a right-aligned continuation. A
field-level setting would force every formula under that heading to look the
same, and the writer would have no way to make an exception.
Existing rows default to ``center``: today's documents render exactly as they
do now.
Revision ID: a1d47c6f2e58
Revises: f8c2d5a73b91
Create Date: 2026-09-23
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "a1d47c6f2e58"
down_revision: str | None = "f8c2d5a73b91"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
op.add_column(
"paper_sentence",
sa.Column(
"formula_align",
sa.String(length=8),
server_default=sa.text("'center'"),
nullable=False,
),
)
def downgrade() -> None:
op.drop_column("paper_sentence", "formula_align")