yun@dicom

导航

Markdown 中, KaTeX 排版特效样例

1. 每个数字增加一个外框

\[\mathbf{A = np.array ([3, 1, 4, 1, 5, 9])} = \colorbox{lightyellow}{$ \begin{array}{|c|c|c|c|c|c|} \hline 3 & 1 & 4 & 1 & 5 & 9\\ \hline \end{array} $} \tag{1-1} \]

\mathbf{A = np.array ([3, 1, 4, 1, 5, 9])} = \colorbox{lightyellow}{$
\begin{array}{|c|c|c|c|c|c|}
\hline
3 & 1 & 4 & 1 & 5 & 9\\
\hline
\end{array}
$}
\tag{1-1}

上面的代码展示了技巧: 每个数字前后增加竖线, 然后上下加两条线 !

2. 调整字体, 比如下面的封面

\[\mathbf{\textcolor{blue}{\Huge{NumPy 可视化教程}}} \]

\mathbf{\textcolor{blue}{\Huge{NumPy 可视化教程}}}

3. Markdown 不支持带背景色的文本? 用数学搞定

\[\colorbox{peachpuff}{$\text{\large{1.1 第一章 第一节}}$} \]

\colorbox{peachpuff}{$\text{\large{1.1 第一章 第一节}}$}

4. 等号对齐

\[\begin{aligned} \mathbf{A \times 2} &= \colorbox{lightblue}{$ \begin{array}{|c|c|c|} 1 & 2 & 3\\ \end{array} $} \times 2 \\ &= \colorbox{thistle}{$ \begin{array}{|c|c|c|} 1\times2 & 2\times2 & 3\times2\\ \end{array} $} \\ &= \colorbox{lightcoral}{$ \begin{array}{|c|c|c|} 2 & 4 & 6\\ \end{array} $} \tag{1-17} \end{aligned} \]

\begin{aligned}
\mathbf{A \times 2} &= 
\colorbox{lightblue}{$
\begin{array}{|c|c|c|}
1 & 2 & 3\\
\end{array}
$}
\times 2 \\
&= 
\colorbox{thistle}{$
\begin{array}{|c|c|c|}
1\times2 & 2\times2 & 3\times2\\
\end{array}
$} \\
&= 
\colorbox{lightcoral}{$
\begin{array}{|c|c|c|}
2 & 4 & 6\\
\end{array}
$}
\tag{1-17}
\end{aligned}

如果有多个等号需要对齐, 要用到 &&=. 见下面的例子

\[\begin{aligned} y_{1,1} &= \text{内容1} &&= \text{内容2} &&= \text{内容3} \\ y_{1,2} &= \text{内容40} &&= \text{内容50} &&= \text{内容60} \\ y_{1,3} &= \text{内容700} &&= \text{内容8000} &&= \text{内容90000} \end{aligned} \]

\begin{aligned}
y_{1,1} &= \text{内容1} &&= \text{内容2} &&= \text{内容3} \\
y_{1,2} &= \text{内容40} &&= \text{内容50} &&= \text{内容60} \\
y_{1,3} &= \text{内容700} &&= \text{内容8000} &&= \text{内容90000}
\end{aligned}

5. 箭头上带文字

\[A \xrightarrow{x<0} B \]

\[A \xrightarrow{\text{当 }x<0} B \]

A \xrightarrow{x<0} B
A \xrightarrow{\text{当 }x<0} B

如果是双向箭头, 需要更特殊的技巧

\[A \overset{\text{}转置}{\Longleftrightarrow} B \]

\[\colorbox{lightcyan}{np.dot (R, M)} \overset{\text{}转置}{\Longleftrightarrow} \colorbox{lightcyan}{$\text{np.dot}(M^{\mathrm{T}}, C)$} \]

A \overset{\text{}转置}{\Longleftrightarrow} B
\colorbox{lightcyan}{np.dot (R, M)} \overset{\text{}转置}{\Longleftrightarrow} 
\colorbox{lightcyan}{$\text{np.dot}(M^{\mathrm{T}}, C)$}

6. 一个矩阵五颜六色

\[=\begin{array}{c|c} \colorbox{lightcoral}{${y_{1,1}}$} = \colorbox{lightblue}{${R_1}$} \cdot \colorbox{lightgreen}{${C_1}$} & \colorbox{lightcoral}{${y_{1,2}}$} = \colorbox{lightblue}{${R_1}$} \cdot \colorbox{lightgreen}{${C_2}$} \\ \hline \colorbox{lightcoral}{${y_{2,1}}$} = \colorbox{lightblue}{${R_2}$} \cdot \colorbox{lightgreen}{${C_1}$} & \colorbox{lightcoral}{${y_{2,2}}$} = \colorbox{lightblue}{${R_2}$} \cdot \colorbox{lightgreen}{${C_2}$} \\ \end{array} \]

=\begin{array}{c|c}
\colorbox{lightcoral}{${y_{1,1}}$} = \colorbox{lightblue}{${R_1}$} \cdot \colorbox{lightgreen}{${C_1}$} &
\colorbox{lightcoral}{${y_{1,2}}$} = \colorbox{lightblue}{${R_1}$} \cdot \colorbox{lightgreen}{${C_2}$} \\
\hline
\colorbox{lightcoral}{${y_{2,1}}$} = \colorbox{lightblue}{${R_2}$} \cdot \colorbox{lightgreen}{${C_1}$} &
\colorbox{lightcoral}{${y_{2,2}}$} = \colorbox{lightblue}{${R_2}$} \cdot \colorbox{lightgreen}{${C_2}$} \\
\end{array}

7. 调整字体大小

\[\text{\tiny\color{red} 微小文本} % 最小 \text{\scriptsize\color{red} 脚本大小} \text{\footnotesize\color{red} 脚注大小} \text{\small\color{red} 小号文本} \text{\normalsize\color{red} 正常大小} % 默认大小 \text{\large\color{red} 大号文本} \text{\Large\color{red} 更大文本} \text{\LARGE\color{red} 再大文本} \text{\huge\color{red} 巨大文本} \text{\Huge\color{red} 最大文本} % 最大 \]

\text{\tiny\color{red} 微小文本}           % 最小
\text{\scriptsize\color{red} 脚本大小}
\text{\footnotesize\color{red} 脚注大小}
\text{\small\color{red} 小号文本}
\text{\normalsize\color{red} 正常大小}     % 默认大小
\text{\large\color{red} 大号文本}
\text{\Large\color{red} 更大文本}
\text{\LARGE\color{red} 再大文本}
\text{\huge\color{red} 巨大文本}
\text{\Huge\color{red} 最大文本}           % 最大

8. 文本或公式,加外框

\[\boxed{\mathbf{a^2 + b^2 = c^2}} \]

\boxed{\mathbf{a^2 + b^2 = c^2}}

9. LaTeX 中的间距命令(从大到小)

1. 大间距

  • \qquad = 2em
  • \quad = 1em(常用)

2. 中等间距(比 \quad 小)

  • \;\thickspace ≈ 5/18 em
  • \:\medspace ≈ 4/18 em

3. 小间距

  • \,\thinspace ≈ 3/18 em(最常用的较小空格)
  • \enspace = 0.5 em

4. 精确控制

  • \hspace{长度} - 自定义长度
  • \kern{长度} - 不换行的精确间距
  • \mspace{长度} - 数学模式专用(需要 amsmath

常用推荐

如果你觉得 \quad 太大,可以试试:

% 使用 \,(最常用的小空格)
A = B \, C

% 或者使用 \:(稍大一点)
A = B \: C

% 或者使用 \;(介于 \, 和 \quad 之间)
A = B \; C

在数学对齐中的具体应用

例子,可以在等号之间添加小空格:

$$
\begin{aligned}
y_{1,1} &= 
\colorbox{lightblue}{${R_1}$} \cdot \colorbox{lightgreen}{${C_1}$}
&\quad&= (1, 2, 3) \cdot (7, 9, 11)  % \quad 可能太大
&\quad&= 58                          % \quad 可能太大
\\
y_{1,1} &= 
\colorbox{lightblue}{${R_1}$} \cdot \colorbox{lightgreen}{${C_1}$}
&\,&= (1, 2, 3) \cdot (7, 9, 11)     % 使用 \, 更紧凑
&\,&= 58                             % 使用 \, 更紧凑
\end{aligned}
$$

% 或者使用 \enspace(0.5em,是 \quad 的一半)
\begin{aligned}
y_{1,1} &= 
\colorbox{lightblue}{${R_1}$} \cdot \colorbox{lightgreen}{${C_1}$}
&\enspace&= (1, 2, 3) \cdot (7, 9, 11)
&\enspace&= 58
\end{aligned}

更灵活的方法:使用 alignat* 环境

如果你需要更精确的控制,推荐使用 alignat* 环境:

% 使用 alignat* 可以完全控制列间距
\begin{alignat*}{3}  % 3 表示对齐点的数量
y_{1,1} &= 
\colorbox{lightblue}{${R_1}$} \cdot \colorbox{lightgreen}{${C_1}$}
&\quad& = (1, 2, 3) \cdot (7, 9, 11)
&\quad& = 58
\\
y_{1,2} &= 
\colorbox{lightblue}{${R_1}$} \cdot \colorbox{lightgreen}{${C_2}$}
&\quad& = (1, 2, 3) \cdot (8, 10, 12)
&\quad& = 64
\end{alignat*}

% 调整间距为更小的值
\begin{alignat*}{3}
y_{1,1} &= 
\colorbox{lightblue}{${R_1}$} \cdot \colorbox{lightgreen}{${C_1}$}
&\;& = (1, 2, 3) \cdot (7, 9, 11)  % 使用 \;
&\;& = 58
\\
y_{1,2} &= 
\colorbox{lightblue}{${R_1}$} \cdot \colorbox{lightgreen}{${C_2}$}
&\;& = (1, 2, 3) \cdot (8, 10, 12)
&\;& = 64
\end{alignat*}

最佳实践建议

  1. 默认情况:使用 \,(最常用的小空格)
  2. 需要微调时:使用 \hspace{2pt} 等精确控制
  3. 多列对齐时:考虑使用 alignat* 环境

完整示例

% 使用 \, 获得紧凑间距
$$
\begin{aligned}
y_{1,1} &= 
\colorbox{lightblue}{${R_1}$} \cdot \colorbox{lightgreen}{${C_1}$}
&\,&= (1, 2, 3) \cdot (7, 9, 11)
&\,&= 58
\\
y_{1,2} &= 
\colorbox{lightblue}{${R_1}$} \cdot \colorbox{lightgreen}{${C_2}$}
&\,&= (1, 2, 3) \cdot (8, 10, 12)
&\,&= 64
\end{aligned}
$$

这样就能获得比 \quad 更紧凑的对齐效果了

10. 上括号及下括号

10.1 无文字

\[\overbrace{a^2+b^=c^2} \]

\overbrace{a^2+b^=c^2}

10.2 带文字. 格式为: \overbrace{内容}^{文字}

\[\overbrace{a^2+b^2=c^2}^{\text{勾股定理}} \]

\overbrace{a^2+b^2=c^2}^{\text{勾股定理}}

10.3 多行文字

\[\overbrace{a^2+b^2=c^2}^{\substack{\text{勾股定理} \\ \text{(Pythagorean theorem)}}} \]

\[\overbrace{x_1+x_2+\cdots+x_n}^ {\substack{\text{多个变量的}\\\text{求和运算}}} \]

\overbrace{a^2+b^2=c^2}^{\substack{\text{勾股定理} \\ \text{(Pythagorean theorem)}}}
\overbrace{x_1+x_2+\cdots+x_n}^
{\substack{\text{多个变量的}\\\text{求和运算}}}

10.4 上下括号嵌套使用

\[\underbrace { \overbrace{a^2+b^2}^{第一部分} + \overbrace{c^2+d^2}^{第二部分} }_{总和} \]

\underbrace
{
  \overbrace{a^2+b^2}^{第一部分}
  +
  \overbrace{c^2+d^2}^{第二部分}
}_{总和}

复杂示例

\[\overbrace { \underbrace{a_1+a_2+\cdots+a_m}_{m\text{项}} + \underbrace{b_1+b_2+\cdots+b_n}_{n\text{项}} }^{m+n\text{项求和}} \]

\overbrace
{
  \underbrace{a_1+a_2+\cdots+a_m}_{m\text{项}}
  +
  \underbrace{b_1+b_2+\cdots+b_n}_{n\text{项}}
}^{m+n\text{项求和}}

10.5 多个括号

\[\overbrace{a}^{A} + \overbrace{b}^{B} + \overbrace{c}^{C} = \overbrace{a+b+c}^{A+B+C} \]

\overbrace{a}^{A} + 
\overbrace{b}^{B} + 
\overbrace{c}^{C} =
\overbrace{a+b+c}^{A+B+C}

11. 右括号

语法有点特殊, 需要 \left.\right} 一起配合使用

11.1 无文字

\[\left. \begin{array}{c} A \\ B \\ C \\ \end{array} \right\} \]

\left.
\begin{array}{c}
A \\
B \\
C \\
\end{array}
\right\}

说明:

  1. \left. 创建一个不可见的左定界符,用于与 \right} 配对
  2. 使用 array 环境来组织多行内容
  3. \right} 生成右边的大括号
  4. {c} 指定列居中对齐
  5. } 不匹配, 这才是正确语法 !!

11.2 带文字

\[\left. \begin{array}{c} A \\ B \\ C \\ \end{array} \right\} \text{ total: 123} \]

\left.
\begin{array}{c}
A \\
B \\
C \\
\end{array}
\right\}
\text{ total: 123}

12. 用 box 来画示意图

12.1 基本格式

\[\begin{array}{c} \boxed{28\times28} \\ \quad \boxed{28\times28} \\ \qquad \boxed{28\times28} \\ \end{array} \]

\begin{array}{c}
\boxed{28\times28} \\
\quad \boxed{28\times28} \\
\qquad \boxed{28\times28} \\
\end{array}

12.2 带描述

\[\left. \begin{array}{c} \boxed{28\times28} \quad \text{(输入层)} \\ \quad \boxed{28\times28} \quad \text{(第一层)} \\ \qquad \boxed{28\times28} \quad \text{(第二层)} \\ \end{array} \right\} \quad \text{参数总数: } 123 \]

\left.
\begin{array}{c}
\boxed{28\times28} \quad \text{(输入层)} \\
\quad \boxed{28\times28} \quad \text{(第一层)} \\
\qquad \boxed{28\times28} \quad \text{(第二层)} \\
\end{array}
\right\}
\quad
\text{参数总数: } 123

12.3 调整 box 的上下间隔

\[\left. \begin{array}{c} \boxed{28\times28} \\[10pt] % 第一行后增加10pt间距 \quad \boxed{28\times28} \\[10pt] % 第二行后增加10pt间距 \qquad \boxed{28\times28} \\ % 最后一行不需要额外间距 \end{array} \right\} \text{ total: 123} \]

\left.
\begin{array}{c}
\boxed{28\times28} \\[10pt]  % 第一行后增加10pt间距
\quad \boxed{28\times28} \\[10pt]        % 第二行后增加10pt间距
\qquad \boxed{28\times28} \\              % 最后一行不需要额外间距
\end{array}
\right\}
\text{ total: 123}

12.4 嫌 box 太扁? 调整行高

用 rule

\[\left. \begin{array}{c} \boxed{\rule{0pt}{4.5ex} 28\times28} \\[8pt] \quad\boxed{\rule{0pt}{4.5ex} 28\times28} \\[8pt] \qquad\boxed{\rule{0pt}{4.5ex} 28\times28} \\ \end{array} \right\} \text{ total: 123} \]

\left.
\begin{array}{c}
\boxed{\rule{0pt}{4.5ex} 28\times28} \\[8pt]
\quad\boxed{\rule{0pt}{4.5ex} 28\times28} \\[8pt]
\qquad\boxed{\rule{0pt}{4.5ex} 28\times28} \\
\end{array}
\right\}
\text{ total: 123}

不过,上述语法导致文字不居中,改成下面

\[\left. \begin{array}{c} \boxed{28\times28\vphantom{\Big|}} \\[12pt] \quad\boxed{28\times28\vphantom{\Big|}} \\[12pt] \qquad\boxed{28\times28\vphantom{\Big|}} \\ \end{array} \right\} \text{ total: 123} \]

\left.
\begin{array}{c}
\boxed{28\times28\vphantom{\Big|}} \\[12pt]
\quad\boxed{28\times28\vphantom{\Big|}} \\[12pt]
\qquad\boxed{28\times28\vphantom{\Big|}} \\
\end{array}
\right\}
\text{ total: 123}

或者, 更高的高度

\[\left. \begin{array}{c} \boxed{28\times28\vphantom{\Bigg|}} \\[18pt] \quad\boxed{28\times28\vphantom{\Bigg|}} \\[18pt] \qquad\boxed{28\times28\vphantom{\Bigg|}} \\ \end{array} \right\} \text{ total: 123} \]

\left.
\begin{array}{c}
\boxed{28\times28\vphantom{\Bigg|}} \\[18pt]
\quad\boxed{28\times28\vphantom{\Bigg|}} \\[18pt]
\qquad\boxed{28\times28\vphantom{\Bigg|}} \\
\end{array}
\right\}
\text{ total: 123}

13. 嫌表格的默认高度不够? 用 \vphantom{\Bigg|} 来将其撑高 !

A B C
0 \(\vphantom{\Bigg|}\)
1 \(\vphantom{\Bigg|}\)
2 \(\vphantom{\Bigg|}\)
| A | B | C |
|---|---|---|
| 0 |   | $\vphantom{\Bigg\|}$ |
| 1 |   | $\vphantom{\Bigg\|}$ |
| 2 |   | $\vphantom{\Bigg\|}$ |

posted on 2026-01-31 15:58  yun@dicom  阅读(9)  评论(0)    收藏  举报