KScript 文档
ZH

文档:KScript.Document

数学

Math 是独占一行的公式。其文本为 LaTeX,按原样保留,因此花括号和反斜杠无需转义。较长的公式可以放在 ```latex 块中分多行书写,较长的 Paragraph 可以在行末使用 \(参见 长字符串和文本块)。公式带有编号,id: 允许文本通过 [#id] 引用编号:

<KScript.Document title="Maths" to="'maths.pdf'">
  <Math id="eq-gauss">\int_{-\infty}^{\infty} e^{-x^2} \, dx = \sqrt{\pi}</Math>
  <Math numbered="false">\sum_{k=1}^{n} k = \frac{n(n+1)}{2}</Math>
  <Math>A = \begin{pmatrix} a &amp; b \\ c &amp; d \end{pmatrix}, \quad \det A = ad - bc</Math>
</KScript.Document>
import KScript.Document

Document(title: 'Maths', to: 'maths.pdf') {
  Math(id: 'eq-gauss') "\int_{-\infty}^{\infty} e^{-x^2} \, dx = \sqrt{\pi}"
  Math(numbered: false) "\sum_{k=1}^{n} k = \frac{n(n+1)}{2}"
  Math "A = \begin{pmatrix} a & b \\\\ c & d \end{pmatrix}, \quad \det A = ad - bc"
}

在 "..." 字符串中,\\ 用来表示一个反斜杠,因此 LaTeX 的换行符 \\ 需要写成 \\\\。环境中的换行也会结束一行:

<KScript.Document title="Cases" to="'cases.pdf'">
  <Math>|x| = \begin{cases}
    x &amp; \text{if } x \ge 0
    -x &amp; \text{otherwise}
  \end{cases}</Math>
</KScript.Document>
import KScript.Document

Document(title: 'Cases', to: 'cases.pdf') {
  Math "|x| = \begin{cases}
    x & \text{if } x \ge 0
    -x & \text{otherwise}
  \end{cases}"
}

数学理解的内容:

  • 使用 TeX 间距的字母、数字和运算符;^ 和 _ 用于上标和下标;' 用于撇号(素数)。
  • \frac, \dfrac, \tfrac, \binom, \sqrt, \sqrt[n].
  • 希腊字母(\alpha 到 \Omega)、关系符号(\le、\approx、\in、\subseteq、\to……)、运算符(\pm、\times、\cdot、\cup……)、箭头,以及 \infty、\partial、\nabla、\forall、\exists 等符号。
  • 带上下限的大型运算符:\sum、\prod、\int、\iint、\oint、\bigcup……;以及函数,如 \sin、\log、\lim、\max 和 \operatorname{name}。
  • \left( ... \right) 以及其他会随内容自动伸缩的定界符。
  • matrix、pmatrix、bmatrix、vmatrix、cases、aligned 和 array 环境。
  • \text{...}, \mathbf, \mathit, \mathrm, \mathbb, \mathcal, \mathsf, \mathtt;重音符号如 \hat、\bar、\vec、\dot、\tilde、\overline、\underline;以及空格 \,、\;、\quad、\qquad。

未知命令会报错,并指出是哪个命令,因此拼写错误会在脚本运行时被发现,而不是等到生成 PDF 之后。每种格式都以自己的形式呈现数学公式:PDF 中为绘制图形,LaTeX 中为 amsmath,.docx 中为 Word 公式,HTML 中为 MathML。