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
+24
View File
@@ -28,6 +28,7 @@ from typing import Literal
from pydantic import BaseModel, ConfigDict, Field, field_validator
from app.citation_styles import DEFAULT_STYLE as DEFAULT_REFERENCE_STYLE
from app.models.paper_sentence import FORMULA_ALIGNMENTS
#: Writing states, mirroring :data:`app.models.paper.PAPER_STATUSES`. Kept as a
#: literal so the generated OpenAPI document describes the enum for clients.
@@ -99,6 +100,8 @@ class SentenceInput(BaseModel):
content: str = ""
#: Start a new line at this sentence rather than running on from the last.
line_break_before: bool = False
#: How a display formula in this sentence is set: left, center or right.
formula_align: str = "center"
citations: list[CitationInput] = Field(default_factory=list)
@field_validator("content")
@@ -106,6 +109,18 @@ class SentenceInput(BaseModel):
def _normalise_content(cls, value: str) -> str:
return fold_whitespace(value)
@field_validator("formula_align")
@classmethod
def _check_align(cls, value: str) -> str:
return _check_formula_align(value)
def _check_formula_align(value: str) -> str:
"""Accept only the three alignments the renderer knows how to apply."""
if value not in FORMULA_ALIGNMENTS:
raise ValueError("公式对齐只能是:" + "、".join(FORMULA_ALIGNMENTS))
return value
class ParagraphUpdate(BaseModel):
"""Payload for ``PUT /papers/{id}/paragraphs/{sort}``.
@@ -138,6 +153,7 @@ class SentenceUpdate(BaseModel):
)
content: str | None = None
line_break_before: bool | None = None
formula_align: str | None = None
citations: list[CitationInput] | None = None
@field_validator("content")
@@ -145,6 +161,11 @@ class SentenceUpdate(BaseModel):
def _normalise_content(cls, value: str | None) -> str | None:
return None if value is None else fold_whitespace(value)
@field_validator("formula_align")
@classmethod
def _check_align(cls, value: str | None) -> str | None:
return None if value is None else _check_formula_align(value)
# --- reads ------------------------------------------------------------------
@@ -174,6 +195,9 @@ class SentenceRead(BaseModel):
#: Whether a line break prints before this sentence. See
#: :attr:`app.models.paper_sentence.PaperSentence.line_break_before`.
line_break_before: bool
#: How a display formula in this sentence is set. See
#: :attr:`app.models.paper_sentence.PaperSentence.formula_align`.
formula_align: str
citations: list[CitationRead]
#: Text to print before this sentence when the paragraph is put back
#: together — ``""`` or a single space, never anything else. Derived on