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

多行公式、等号对齐与高级矩阵

在推导复杂的系统方程、执行多项式化简或设计三维空间物理引擎、3D 渲染相机裁剪矩阵时,我们需要排版跨越多行的连续等式,或者高精度的多维代数矩阵(Matrices)。

一、使用 aligned 环境实现长公式换行与等号对齐

在推导核心业务指标或微积分时,等式往往极长,需要进行物理换行并保持等号对齐:

长公式多行推导与等号对齐:
$
\begin{aligned}
f(x) &= (x + 3)^2 - 5 \\
&= (x^2 + 6x + 9) - 5 \\
&= x^2 + 6x + 4
\end{aligned}
$

公式对齐环境与标记符号解析: 在这段代码中,我们使用了 \begin{aligned}\end{aligned} 环境。在 LaTeX 中,aligned 极其适合嵌入在双美元符 $ 中。在环境内部:

  • 等号对齐符 &:这是核心的对齐锚点。它被放置在每一行需要垂直对准的等号 =(或者是其他运算符如 \ge)的前方。
  • 换行符 \\:表示在此处进行公式强行换行。 利用 aligned 排版能将繁杂杂乱的代数化简过程整理成一列极富韵律感、阅读效率极高的等式梯队。

二、多维矩阵与行列式排版

在图形学、量子计算与机器学习权重网络中,矩阵是最核心的数据载体:

圆括号矩阵与中括号矩阵:
$
P = \begin{pmatrix}
1 & 0 & -x \\
0 & 1 & -y \\
0 & 0 & 1
\end{pmatrix}
, \quad
M = \begin{bmatrix}
w_{11} & w_{12} \\
w_{21} & w_{22}
\end{bmatrix}
$

带绝对值线的行列式(Determinant):
$
|A| = \begin{vmatrix}
a & b \\
c & d
\end{vmatrix}
= ad - bc
$

各种矩阵环境的边界符号与行列控制解析: 在这段矩阵排版脚本中,我们使用了三种常见的矩阵环境:

  • pmatrix:自动在外侧包裹标准的圆括号
  • bmatrix:自动在外侧包裹标准的中括号(代表数学上的多维矩阵或向量)。
  • vmatrix:自动在外侧包裹单根垂直线(代表行列式的值计算)。 在矩阵体内部,每一行中不同列的元素之间使用 对齐符 & 分割,而行与行之间使用 双反斜杠 \\ 进行物理换行。在矩阵外侧,通过加入 \quad 命令可以产生一个等宽的空格间距,这对于在公式中同时列举两个平级矩阵(如 M 与 P)并做对比起到了极佳的排版点缀效果。

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

  1. 问题:在 aligned 或矩阵环境中写了最后一行的 \\ 换行符,结果公式底部出现了一大片空白,或者报错?
    • 解决方法:请注意:最后一行绝对不要书写换行符 \\。换行符是连接符,写在最后一行会被解析器理解为后面还有一个隐形空行,这会导致公式尾部出现多余空白或直接发生解析崩溃。
  2. 问题:矩阵元素中包含复杂的上下标或分数,导致矩阵高度拉伸不均匀,挤压到相邻元素的显示?
    • 解决方法:当矩阵内部有极高或极扁的元素(如分数 \frac)时,可以使用 \displaystyle 宏或者在表格/矩阵内部适当增加行高,或者也可以换成圆括号内联矩阵,将复杂的分数化简提取到矩阵外面。

Multi-line Equations, Alignment, and Matrices

For mathematical regressions, algebraic simplifications, or 3D camera transformation matrices, we need multi-line equation alignment and structured matrices.

1. Multi-line Alignment with the Aligned Environment

Align sequential equations vertically around operators to make long algebraic derivations readable:

Multi-line equation derivation aligned at equal signs:
$
\begin{aligned}
f(x) &= (x + 3)^2 - 5 \\
&= (x^2 + 6x + 9) - 5 \\
&= x^2 + 6x + 4
\end{aligned}
$

Alignment Syntax and Operators: We enclose the derivation inside \begin{aligned} and \end{aligned}. Inside:

  • The alignment marker & anchors the alignment pivot (positioned right before the equal signs =).
  • The line break marker \\ triggers a vertical breakpoint. This structures long equations into a clean, professional column format.

2. Multi-dimensional Matrices and Determinants

Matrices are fundamental representing neural network weights, transforms, or computer graphics coordinate updates:

Parentheses (pmatrix) and brackets (bmatrix) matrices:
$
P = \begin{pmatrix}
1 & 0 & -x \\
0 & 1 & -y \\
0 & 0 & 1
\end{pmatrix}
, \quad
M = \begin{bmatrix}
w_{11} & w_{12} \\
w_{21} & w_{22}
\end{bmatrix}
$

Determinant matrix bordered by straight vertical lines (vmatrix):
$
|A| = \begin{vmatrix}
a & b \\
c & d
\end{vmatrix}
= ad - bc
$

Matrix Environments and Layout Controls: We demonstrate three matrix variants: pmatrix (adds surrounding parentheses), bmatrix (adds square brackets), and vmatrix (adds vertical determinant bars). Elements on a single row are divided by &, and rows are separated by \\. Spacers like \quad add horizontal spacing between multiple arrays.

3. FAQ

  1. Why does adding a trailing \\ on the final row of a matrix cause compiling errors or blank space?
    • Solution: Never append a line-break operator \\ on the last row of aligned math blocks or matrices. It implies a non-existent row, triggering rendering errors.
  2. My fractions inside matrices look tiny and compressed?
    • Solution: Use the inline display macro \displaystyle\frac{...}{...} to force full-scale fraction sizing.