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
+20
View File
@@ -14,6 +14,16 @@ import type { BatchDeleteResult, PageQuery, PageResult } from './types'
/** 草稿 / 撰写中 / 已完成. Mirrors `app.models.paper.PAPER_STATUSES`. */
export type PaperStatus = 'draft' | 'writing' | 'done'
/** How a display formula is set on its line. Mirrors `FORMULA_ALIGNMENTS`. */
export type FormulaAlign = 'left' | 'center' | 'right'
/** What the editor offers, in the order it offers them. */
export const FORMULA_ALIGN_OPTIONS: { value: FormulaAlign; label: string }[] = [
{ value: 'center', label: '公式居中' },
{ value: 'left', label: '公式居左' },
{ value: 'right', label: '公式居右' },
]
/** Display labels and tag colours for the three states, in one place. */
export const PAPER_STATUS_LABELS: Record<PaperStatus, string> = {
draft: '草稿',
@@ -84,6 +94,14 @@ export interface Sentence {
/** Position inside that paragraph. */
sort: number
content: string
/**
* How a display formula in this sentence sits on its line.
*
* `center` (the default) is what a paper wants almost always. The equation
* number is not affected: `\tag{1}` is always at the right margin, as every
* numbered style prints it.
*/
formula_align: FormulaAlign
/**
* Whether a line break prints before this sentence.
*
@@ -191,6 +209,8 @@ export interface SentenceInput {
content: string
/** Start a new line at this sentence. */
line_break_before?: boolean
/** How a display formula in this sentence is set. */
formula_align?: FormulaAlign
citations: CitationInput[]
}
@@ -179,10 +179,12 @@ const anchor = computed(() => `paragraph-${props.paragraph.paper_template_filed_
>
<template v-for="item in line.items" :key="item.sentence.id">
<!-- A display formula is a block by definition, so it takes its own
line whether or not the writer asked for a break. -->
line whether or not the writer asked for a break. How it sits on
that line is the writer's choice, per formula. -->
<span
v-if="item.display"
class="sentence sentence--display"
:class="`is-${item.sentence.formula_align}`"
v-html="item.html"
></span>
<span v-else class="sentence" v-html="item.html"></span>
@@ -330,6 +332,20 @@ const anchor = computed(() => `paragraph-${props.paragraph.paper_template_filed_
white-space: normal;
}
/* KaTeX centres a display formula in two nested blocks, so an alignment has to
reach both of them. The equation number is unaffected: `\tag{1}` is
positioned against the right margin in every case, which is how a numbered
style prints it. */
.sentence--display.is-left :deep(.katex-display),
.sentence--display.is-left :deep(.katex-display > .katex) {
text-align: left;
}
.sentence--display.is-right :deep(.katex-display),
.sentence--display.is-right :deep(.katex-display > .katex) {
text-align: right;
}
/* The fallback when KaTeX cannot read a formula: the source, in red, so it can
be found and fixed instead of silently vanishing. */
.paragraph-body :deep(.math-error) {
@@ -338,10 +354,12 @@ const anchor = computed(() => `paragraph-${props.paragraph.paper_template_filed_
}
.paragraph-body :deep(.katex) {
/* Formulas inherit the paragraph's size and colour, and are never justified
apart — a stretched equation is unreadable. */
/* Only the size. `text-align` is deliberately *not* set here: KaTeX centres a
display formula in this very element, and a `text-align: left` in this rule
silently overrode it — every display formula in the paper sat against the
left margin, with only its number at the right. Alignment is the per-formula
choice above (`.is-left` / `.is-right`), and the default is KaTeX's centre. */
font-size: 1.05em;
text-align: left;
}
.cite-mark {
@@ -28,13 +28,16 @@ import { ElMessage } from 'element-plus'
import { errorMessage } from '@/api/client'
import {
FORMULA_ALIGN_OPTIONS,
getParagraph,
replaceParagraph,
type FormulaAlign,
type PaperDocument,
type PaperParagraph,
type SentenceInput,
} from '@/api/papers'
import { PARAGRAPH_UNSET_LABEL, foldWhitespace } from '@/utils/format'
import { hasDisplayMath } from '@/utils/math'
const props = defineProps<{
/** Visibility, used with `v-model`. */
@@ -67,6 +70,8 @@ interface SentenceDraft {
content: string
/** Print a line break before this sentence. */
lineBreak: boolean
/** How a display formula in this sentence is set. */
align: FormulaAlign
citations: CitationDraft[]
}
@@ -134,6 +139,7 @@ watch(
sort: sentence.sort,
content: sentence.content,
lineBreak: sentence.line_break_before,
align: sentence.formula_align,
citations: sentence.citations.map((citation) => ({
key: nextKey++,
reference_id: citation.reference_id,
@@ -160,6 +166,7 @@ function addSentence(): void {
sort: nextSort(),
content: '',
lineBreak: false,
align: 'center',
citations: [],
})
}
@@ -247,6 +254,7 @@ async function submit(): Promise<void> {
sort: row.sort,
content: foldWhitespace(row.content),
line_break_before: row.lineBreak,
formula_align: row.align,
citations: row.citations
.filter((citation) => citation.quote.trim().length > 0)
.map((citation) => ({
@@ -342,7 +350,8 @@ async function submit(): Promise<void> {
<div class="editor-toolbar">
<span class="toolbar-title">共 {{ orderedRows.length }} 句</span>
<span class="toolbar-hint">
按 sort 升序拼成这个段落;公式写 LaTeX:行内 $x$,独立成行 $$x$$
按 sort 升序拼成这个段落;公式写 LaTeX:行内 $x$,独立成行 $$x$$(编号用
\tag{1},自动靠右)
</span>
<div class="toolbar-spacer" />
<el-button size="small" :icon="Refresh" :disabled="!orderedRows.length" @click="autoNumber">
@@ -378,6 +387,22 @@ async function submit(): Promise<void> {
>
<el-checkbox v-model="row.lineBreak" size="small" label="另起一行" />
</el-tooltip>
<!-- Only a sentence with display maths has a line of its own, so
only that sentence has an alignment to choose. -->
<el-select
v-if="hasDisplayMath(row.content)"
v-model="row.align"
size="small"
class="align-select"
>
<el-option
v-for="option in FORMULA_ALIGN_OPTIONS"
:key="option.value"
:value="option.value"
:label="option.label"
/>
</el-select>
<div class="toolbar-spacer" />
<el-button
link
@@ -563,6 +588,10 @@ async function submit(): Promise<void> {
width: 104px;
}
.align-select {
width: 112px;
}
.citations {
display: flex;
flex-direction: column;