2026-04-23 | 预计阅读:6 分钟 Read Time: 6 min read | latex

公式文本样式、水平线与大括号

在推导复杂的数学模型(如机器学习损失函数、经典物理受力模型)时,我们经常需要在数学公式内部插入说明文字、定义特殊的张量样式,或者使用大括号对公式中的特定多项式项进行归属归类标注。

一、公式内部字体控制与文本嵌入

在公式中,默认的所有字母都会被视为相互相乘的“数学变量”并自动倾斜。如果需要展示特定的向量或纯文本:

粗体张量与斜体强调:
$\mathbf{F} = m \cdot \mathbf{a}$

带纯文本下标的神经网络损失函数:
$L_{\mathrm{total}} = L_{\mathrm{train}} + L_{\mathrm{test}}$
$\text{收益} = \text{收入} - \text{成本}$

公式内部字体控制语法解析: 在这段代码中,\mathbf{F} 用于将字母 F 强行改变为标准的数学粗体(Math Bold),常用于代表物理学中的矢量或多维张量。\mathrm{total} 控制文本转译为正体罗马字(Roman Typeface),这在书写如 totalmaxmin 等代表具体物理含义的下标时是绝对的排版规范。否则,如果直接书写 L_total,在渲染时,字母 t, o, t, a, l 会因为被当做相乘的斜体字母而严重变形、字间距过宽,显得极其不专业。\text{文本} 更是可以直接在公式中输出标准的、带有系统默认字体外观的汉字。

二、公式的上/下划线与大括号标注机制

为了对公式复杂的各个多项式块进行原理解析,我们可以使用上/下划线和大括号:

上划线与下划线:
$\overline{A \cdot B} = \overline{A} + \overline{B}$

使用上大括号与下大括号进行概念归类:
$\underbrace{a + b + \cdots + z}_{26 \text{ 个英文字母}}$
$\overbrace{x_1 + x_2 + \dots + x_n}^{n \text{ 个特征样本}}$

水平划线与大括号归类语法解析: 上划线 \overline{表达式} 与下划线 \underline{表达式} 常用于布尔代数的求反逻辑或代数平均值修饰。而 \underbrace{多项式}_{说明}\overbrace{多项式}^{说明} 是数学教科书中用于详尽拆解核心控制项的最强排版命令。它会在多项式的下方或上方绘制一条自适应宽度的、圆润的矢量大括号,并允许我们使用下角标 _ 或上角标 ^ 在大括号的外侧附加纯文本说明。这在书写神经网络损失函数的“正则项”与“经验误差项”对比时,具有极佳的视觉引导和极高的技术表现力。

三、常见问题与避坑指南(FAQ)

  1. 问题:在 \text 或者 \mathrm 里面写汉字时,右侧预览页乱码或发生崩溃?
    • 解决方法:KaTeX 编译器对公式内汉字有一定限制。若要在公式内部写入汉字,必须使用 \text{中文字体} 语法包围。诸如 \mathrm{中文}\mathbf{中文} 等宏包本身是不支持宽字符中文渲染的,会发生严重的排版断裂。
  2. 问题:上大括号 \overbrace 指令中的上角标文字被挤压变形或重叠?
    • 解决方法:在大括号顶端的修饰文本中,如果写的内容较多,请嵌套使用 \text{...} 并可以配合公式空格控制符如 \quad 来平滑撑开间距,防止大字号英文符号与公式主体叠在一起。

Formula Text Styles, Rules, and Grouping Braces

When deriving algorithms, we often require direct descriptions within equations, boldface tensors, or visual brackets wrapping specific terms.

1. In-Equation Font Styling and Text Blocks

By default, LaTeX parses adjacent letters as variables in italic multiplication. For static constants, labels, or matrices, use font control macros:

Boldface vectors versus normal scalars:
$\mathbf{F} = m \cdot \mathbf{a}$

Romanized subscripts and literal text blocks:
$L_{\mathrm{total}} = L_{\mathrm{train}} + L_{\mathrm{test}}$
$\text{Profit} = \text{Revenue} - \text{Cost}$

In-Equation Typography Rules: The macro \mathbf{F} forces standard boldface, indicating vector or tensor fields. Subscripts like \mathrm{total} apply Roman regular font. Without it, typing L_total outputs deformed, widely spaced italicized letters. \text{...} outputs default system sans-serif strings.

2. Over/Underlines and Grouping Braces

Highlight parts of equations or group variables using lines and brackets:

Logical negation or mean averages:
$\overline{A \cdot B} = \overline{A} + \overline{B}$

Grouping items with overbraces and underbraces:
$\underbrace{a + b + \cdots + z}_{26 \text{ characters}}$
$\overbrace{x_1 + x_2 + \dots + x_n}^{n \text{ sample features}}$

Line Rule and Bracket Syntax: \overline{expr} and \underline{expr} apply horizontal lines. The grouping macros \underbrace{expr}_{note} and \overbrace{expr}^{note} render vector brackets below or above an algebraic term, adjusting width dynamically. Superscripts ^ or subscripts _ position annotations adjacent to the bracket center.

3. FAQ

  1. Why is my Chinese text inside math formulas rendering as broken boxes?
    • Solution: You must use \text{...} to enclose standard Chinese characters. Other font engines like \mathrm or \mathbf only support standard ASCII characters.
  2. How do I prevent wide annotations over an \overbrace from clashing with the equation?
    • Solution: Wrap the annotation inside \text{...} and use explicit spacer operators like \quad to buffer spacing.