KScript 文档
ZH

KScript.Document

无需安装 TeX,即可创建包含 LaTeX 数学公式的论文、报告、信件和发票,并保存为 PDF、LaTeX、Word、Markdown 或 HTML。

在脚本顶部导入:

<import package="KScript.Document" />
import KScript.Document

根元素为 <KScript.Document> 的文件会自动导入它。

在 .kscript 文件中,只需在 import 里写 Document { },不要写 KScript.Document { }。

KScript.Document 会生成 PDF 和 Word 文件,因此文档中的示例无法直接运行。请将其复制到 .ks.xml 文件中,然后用 kscript 运行。

元素语句

#<Abstract>Abstract

标题下方的摘要。空行表示新段落。

保存文本及 { } 值。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Abstract>We measure how gliders move.</Abstract>
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Abstract "We measure how gliders move."
}

#<Author>Author

一位作者,包含所属机构和电子邮箱。其文本为姓名。

保存文本及 { } 值。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
affiliation带有 { } 的 string其工作或学习单位。
email带有 { } 的 string其电子邮箱地址。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Author affiliation="University of Cambridge" email="ada@example.org">Ada Lovelace</Author>
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Author(affiliation: 'University of Cambridge', email: 'ada@example.org') "Ada Lovelace"
}

#<Bibliography>Bibliography

通过 [@key] 引用的文献列表:来自 BibTeX 文件、其中的 Source 元素,或两者兼用。若未指定,列表将放在文末。

包含语句。在 { } 中包含语句。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
path文件路径BibTeX(.bib)文件,路径相对于脚本。
title带有 { } 的 string标题(默认为 References)。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Paragraph>Life is universal [@berlekamp1982].</Paragraph>
  <Bibliography path="refs.bib" />
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Paragraph "Life is universal [@berlekamp1982]."
  Bibliography(path: 'refs.bib')
}

#<Code>Code

以等宽字体显示的源代码块,按原样呈现。

保存文本及 { } 值。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
id带有 { } 的 string用于 [#id] 交叉引用的名称,例如 id: "intro"。
language带有 { } 的 string语言,例如 python,用于 Markdown 和 HTML。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Code language="python">print('Hello')</Code>
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Code(language: 'python') "print('Hello')"
}

#<Contents>Contents

目录,在 PDF 中包含页码。

自闭合。独立的语句。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
title带有 { } 的 string标题(默认为 Contents)。
depth表达式列出的层级数:2 表示列出章节和小节(默认为 3)。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Contents depth="2" />
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Contents(depth: 2)
}

#<KScript.Document>Document

一个文档。其内容(Section、Paragraph、List、Table、Figure、Math 等)由模板排版,并保存到 to: 指定的位置,或保存到 into: 中。for、if 和 let 等语句可在其中使用。

容纳语句。在 { } 中容纳语句。仅限脚本使用。

属性选项值说明
templateplain|paper|report|letter|invoice外观:plain(默认)、paper、report、letter 或 invoice。
title带有 { } 的 string文档标题。
subtitle带有 { } 的 string标题下方的一行文字。
author带有 { } 的 string作者,或用 ; 分隔的多位作者(Author 元素可添加所属机构和邮箱)。
date带有 { } 的 string标题下方显示的日期,例如 { @now()->format('d MMMM yyyy') }。
keywords带有 { } 的 string关键词,以列表形式给出:['cellular automata', 'Game of Life'](XML 中为 cellular automata, Game of Life)。它们显示在摘要下方,并保存在文件属性中。
to带有 { } 的 string保存位置,按扩展名决定:paper.pdf、paper.tex、paper.docx、paper.md 或 paper.html。可用列表保存多种格式:[paper.pdf, paper.docx]。
into新变量的名称接收文档的变量,之后可用于 ->save()、->pages() 或 ->convert(to: markdown)。
papera4 | a3 | a5 | b5 | letter | legal纸张尺寸:a4(默认)、a3、a5、b5、letter、legal,或 '15cm x 20cm' 等尺寸。
landscapetrue 或 falsetrue 将纸张横向排版。
margin带有 { } 的 string页边距:单一值(2.5cm),或像 CSS 那样分别设置上/右/下/左('2cm 2.5cm')。
fontserif|sans|mono正文字体:serif(Times)、sans(Helvetica)或 mono(Courier)。
heading_fontserif|sans|mono标题字体:serif、sans 或 mono。
size带有 { } 的 string正文字号(磅),例如 11。
line_height带有 { } 的 string行距,即基线到基线的距离,以字号的倍数表示,例如 1.4。
alignjustify|left|center|right正文对齐方式:justify、left、center 或 right。
accent带有 { } 的 string标题和标题页的颜色:'#1f4e79' 或 'navy' 等颜色名称。
numberedtrue 或 falsefalse 表示章节不编号。
title_pagetrue 或 falsetrue 将标题、作者、日期和摘要放在单独的一页上。
chapterstrue 或 falsetrue 使每个顶级章节从新的一页开始。
citationsnumeric|author-year数字编号([1])或作者-年份((Conway, 1970))。
header带有 { } 的 string页眉:'左 | 中 | 右',可使用 [page]、[pages]、[title]、[author] 和 [date]。
footer带有 { } 的 string页脚,格式同页眉。默认显示页码。
rulesbooktabs|grid|none表格线:booktabs(上、下及表头下方)、grid 或 none。
language带有 { } 的 string语言,例如 en 或 en-GB,用于 Word 和 HTML。
示例
<KScript.Document template="paper" title="Gliders" author="Ada Lovelace" to="gliders.pdf">
  <Section title="Introduction">
    <Paragraph>A glider moves one cell every \( 4 \) generations.</Paragraph>
  </Section>
</KScript.Document>
import KScript.Document

Document(template: paper, title: 'Gliders', author: 'Ada Lovelace', to: 'gliders.pdf') {
  Section(title: 'Introduction') {
    Paragraph "A glider moves one cell every \( 4 \) generations."
  }
}

#<Figure>Figure

带标题的图片(PNG 或 JPEG)。带标题的图将被编号。

自闭合。独立的语句。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
path 必需文件路径图片,路径相对于脚本。
caption带有 { } 的 string标题,下方显示为“图 1:...”。
id带有 { } 的 string用于 [#id] 交叉引用的名称,例如 id: "intro"。
width表达式占正文宽度的比例,取值 0 到 1(或 '60%')。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Figure path="glider.png" caption="A glider" id="fig-glider" width="0.5" />
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Figure(path: 'glider.png', caption: 'A glider', id: 'fig-glider', width: 0.5)
}

#<Invoice>Invoice

发票:包含开票方和收票方地址、发票编号和日期、项目表格(含小计、税额和总计)以及备注。

自闭合。独立的语句。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
items 必填表达式项目行:一个由映射组成的列表,每项包含 description、quantity(默认为 1)和 price(或 amount)。
number带有 { } 的 string发票编号。
from带有 { } 的 string您的姓名和地址,每行一项。
to带有 { } 的 string客户的姓名和地址。
date带有 { } 的 string发票日期(默认:今天)。
due带有 { } 的 string付款截止日期。
currency带有 { } 的 string货币代码(GBP、USD、EUR、JPY 等)或符号(默认 GBP)。
tax表达式税率百分比,例如 20。若不需要税项行,则省略。
tax_name带有 { } 的 string税项名称(默认 VAT)。
paid表达式已付金额,与应付余额一同显示。
notes带有 { } 的 string表格下方的付款信息或条款。
title带有 { } 的 string标题(默认 Invoice)。
示例
<KScript.Document template="invoice" to="invoice.pdf">
  <Invoice number="INV-042" from="Life Lab Ltd\n1 High Street" to="Acme plc\n2 Low Road" currency="GBP" tax="20" items="[{ description: 'Consulting', quantity: 3, price: 450 }]" />
</KScript.Document>
import KScript.Document

Document(template: invoice, to: 'invoice.pdf') {
  Invoice(number: 'INV-042', from: 'Life Lab Ltd\n1 High Street', to: 'Acme plc\n2 Low Road', currency: 'GBP', tax: 20, items: [{ description: 'Consulting', quantity: 3, price: 450 }])
}

#<Item>Item

列表中的一项。其文本的用法与 Paragraph 相同。

包含 { } 值的文本。仅用于脚本。应放在 <List>List 之中。

示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <List>
    <Item>Gliders move by **one cell** diagonally.</Item>
  </List>
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  List {
    Item "Gliders move by **one cell** diagonally."
  }
}

#<Latex>Latex

LaTeX,来源可以是其文本、latex 值或 .tex 文件,其解析方式与文档部分相同:章节、段落、列表、数学公式、引文、图表以及常用文本命令。其文本按原样使用,因此花括号遵循 LaTeX 的语法。

保存文本及 { } 值。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
path文件路径相对于脚本的 .tex 文件。
value表达式latex 值,例如返回 latex 块的函数所给出的结果。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Latex path="chapters/method.tex" />
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Latex(path: 'chapters/method.tex')
}

#<Letter>Letter

信函:右侧为发件人地址和日期,之后是收件人、主题行、称呼、置于其中的正文、结束语和签名。

包含语句。在 { } 中包含语句。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
from带有 { } 的 string发件人的姓名和地址,每行一项。
to带有 { } 的 string收件人的姓名和地址,每行一项。
date带有 { } 的 string日期(默认:今天,格式如 2026 年 11 月 20 日)。
subject带有 { } 的 string主题行,以粗体显示。
opening带有 { } 的 string称呼,例如 'Dear Sam,'。
closing带有 { } 的 string结束语,例如 'Yours sincerely,'。
signature带有 { } 的 string结束语下方的姓名,如有需要可加一行职务。
示例
<KScript.Document template="letter" to="letter.pdf">
  <Letter from="1 High Street\nCambridge" to="Sam Smith\n2 Low Road\nLeeds" subject="Your order" opening="Dear Sam," closing="Best wishes," signature="Ada">
    <Paragraph>Thank you for your order.</Paragraph>
  </Letter>
</KScript.Document>
import KScript.Document

Document(template: letter, to: 'letter.pdf') {
  Letter(from: '1 High Street\nCambridge', to: 'Sam Smith\n2 Low Road\nLeeds', subject: 'Your order', opening: 'Dear Sam,', closing: 'Best wishes,', signature: 'Ada') {
    Paragraph "Thank you for your order."
  }
}

#<List>List

由 Item 元素组成的项目符号或编号列表。紧跟在 Item 之后的 List 会嵌套在该项中。

包含语句。在 { } 中包含语句。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
stylebullet|'number'|letter|romanbullet(默认)、number、letter 或 roman。
start表达式起始编号,例如 3。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <List style="number">
    <Item>Measure</Item>
    <Item>Compare</Item>
  </List>
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  List(style: number) {
    Item "Measure"
    Item "Compare"
  }
}

#<Markdown>Markdown

Markdown,来源可以是其文本或文件:# 标题会成为章节,并支持段落、列表、表格、代码、引文、图片和数学公式(脚本中使用 [ ],文件中也可使用 $ \(and\) $$)。

保存文本及 { } 值。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
path文件路径相对于脚本的 Markdown 文件。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Markdown path="chapters/results.md" />
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Markdown(path: 'chapters/results.md')
}

#<Math>Math

LaTeX 格式的独立公式,除非设置 numbered: false,否则带编号。其文本按原样使用,因此花括号遵循 LaTeX 的语法。在 matrix、cases 和 aligned 中,换行即表示一行的结束。

保存文本及 { } 值。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
id带有 { } 的 string用于 [#id] 交叉引用的名称,例如 id: "intro"。
numberedtrue 或 falsefalse 表示不为该公式编号。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Math id="eq-growth">N(t) = N_0 e^{rt}</Math>
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Math(id: 'eq-growth') "N(t) = N_0 e^{rt}"
}

#<PageBreak>PageBreak

开始新的一页。

自闭合。独立的语句。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <PageBreak />
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  PageBreak
}

#<Paragraph>Paragraph

段落。其文本可包含 粗体、斜体、code、链接、( 数学公式 )、[@key] 引用、[#id] 交叉引用和 ^[脚注]。空行会开始新的段落。

保存文本及 { } 值。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
id带有 { } 的 string用于 [#id] 交叉引用的名称,例如 id: "intro"。
alignleft|center|right|justifyleft、center、right 或 justify(默认使用文档的设置)。
linestrue 或 falsetrue 会保留文本中的换行,如地址或诗歌。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Paragraph>Gliders travel **diagonally**, one cell every \( 4 \) generations.^[Down and to the right.]</Paragraph>
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Paragraph "Gliders travel **diagonally**, one cell every \( 4 \) generations.^[Down and to the right.]"
}

#<Quote>Quote

引文,两侧均缩进。

保存文本及 { } 值。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
by带有 { } 的 string说话者。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Quote by="John Conway">I hate the Game of Life.</Quote>
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Quote(by: 'John Conway') "I hate the Game of Life."
}

#<Rule>Rule

横跨文本的水平线。

自闭合。独立的语句。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Rule />
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Rule
}

#<Section>Section

带编号标题的章节。嵌套在 Section 中的 Section 为子章节。

包含语句。在 { } 中包含语句。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
title 必填带有 { } 的 string标题。可包含 粗体、斜体 和 ( 数学公式 )。
id带有 { } 的 string用于 [#id] 交叉引用的名称,例如 id: "intro"。
numberedtrue 或 falsefalse 表示该章节不编号。
new_pagetrue 或 falsetrue 表示该章节从新的一页开始。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Section title="Methods" id="methods">
    <Paragraph>We ran each pattern for 1000 generations.</Paragraph>
  </Section>
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Section(title: 'Methods', id: 'methods') {
    Paragraph "We ran each pattern for 1000 generations."
  }
}

#<Source>Source

需要引用的文献,完整列出:id: 为 [@key] 所用的键,其他属性为 BibTeX 字段(author、title、year、journal、volume、pages、publisher、url、doi 等)。

自闭合。独立的一条陈述。仅用于脚本。应放在 <Bibliography>Bibliography 之中。其他属性为源的更多字段,例如 journal、volume、pages、url 或 doi。其他选项为源的更多字段,例如 journal、volume、pages、url 或 doi。

属性选项值说明
id 必填带有 { } 的 string引用键,如 [@conway1970]。
typea stringarticle、book、inproceedings、misc 等。
author带有 { } 的 string作者,按 BibTeX 的方式以 ' and ' 分隔。
title带有 { } 的 string标题。
year带有 { } 的 string年份。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Paragraph>Life was first described in 1970 [@gardner1970].</Paragraph>
  <Bibliography>
    <Source id="gardner1970" type="article" author="Martin Gardner" title="Mathematical Games" journal="Scientific American" year="1970" volume="223" pages="120--123" />
  </Bibliography>
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Paragraph "Life was first described in 1970 [@gardner1970]."
  Bibliography {
    Source(id: 'gardner1970', type: 'article', author: 'Martin Gardner', title: 'Mathematical Games', journal: 'Scientific American', year: 1970, volume: 223, pages: '120--123')
  }
}

#<Table>Table

表格:由 rows: 生成,或由其中的 Tr 行生成。带标题的表格会编号。它与终端表格接受相同的 rows、columns、align 和 header 设置。

包含语句。在 { } 中包含语句。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
caption带有 { } 的 string标题,显示在表格上方,格式为 'Table 1: ...'。
id带有 { } 的 string用于 [#id] 交叉引用的名称,例如 id: "intro"。
rows表达式行:由映射(每个映射为一行)组成的列表,或由列表组成的列表(第一项为表头行)。
columns表达式每个映射中要显示的值及其顺序:键的列表 ['name', 'age'],或表头到键的映射 { Name: 'name' }。默认为第一行的键。
align表达式每列的对齐方式:left、center 或 right,例如 ['left', 'right']。数字默认右对齐。
widths表达式整页表格的相对列宽,例如 [3, 1, 1]。
rulesbooktabs|grid|nonebooktabs、grid 或 none(默认使用文档的设置)。
headertrue 或 falsefalse:没有表头行。
size表达式相对于正文的文字大小,例如 0.9。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Table caption="Patterns" rows="$patterns" columns="{ Pattern: 'name', Period: 'period' }" align="['left', 'right']" />
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Table(caption: 'Patterns', rows: $patterns, columns: { Pattern: 'name', Period: 'period' }, align: ['left', 'right'])
}

#<Tr>Tr

表格行:单元格之间以 | 分隔,每个单元格都像一个段落的文本。| 在单元格内表示 |。

包含 { } 值的文本。仅用于脚本。放在 <Table>Table 之中。

属性选项值说明
headertrue 或 false设为 true 时此行为标题行,会在每一页顶部重复显示。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <Table caption="Patterns">
    <Tr header="true">Pattern | Period | Speed</Tr>
    <Tr>Glider | 4 | c/4</Tr>
  </Table>
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  Table(caption: 'Patterns') {
    Tr(header: true) "Pattern | Period | Speed"
    Tr "Glider | 4 | c/4"
  }
}

#<VSpace>VSpace

垂直间距。

自闭合。独立的语句。仅用于脚本。应放在 <KScript.Document>Document 或 <fn>fn 内。

属性选项值说明
height 必需带有 { } 的 string大小:长度值,例如 '12pt'、'5mm' 或 '1cm'。
示例
<KScript.Document title="Gliders" to="gliders.pdf">
  <VSpace height="1cm" />
</KScript.Document>
import KScript.Document

Document(title: 'Gliders', to: 'gliders.pdf') {
  VSpace(height: '1cm')
}

命令

命令可通过 @ 调用,可用于任何表达式:@name(arguments)。

#@document(text, as?: 'markdown'|'latex') → document

从 Markdown 或 LaTeX 源代码生成文档。带围栏的值会指定格式。普通字符串默认为 Markdown。
参数接受
text 必需任意值
as 可选以下单词之一:'markdown'|'latex'

参数可以命名:@document(…, as: …)。

示例
<echo>{ @document('# Notes')->save('notes.pdf') }</echo>
echo @document('# Notes')->save('notes.pdf')

#@document_templates() → map

内置文档模板及其用途:{ paper: '...', report: '...' }。
示例
<echo>{ @document_templates()->keys() }</echo>
echo @document_templates()->keys()

#@latex_escape(text) → string

转义后可安全放入 LaTeX 文件的文本:\ { } $ & % # _ ~ ^ 已转义。
示例
<echo>{ @latex_escape('50% of $10 &amp; up') }</echo>
echo @latex_escape('50% of $10 & up')

方法

对于 markdown 值:->find()、->replace()、->insert()、->save()、->pages()、->words()

对于 latex 值:->find()、->replace()、->insert()、->save()、->pages()、->words()

对于 document 值:->save()、->bytes()、->find()、->replace()、->insert()、->pages()、->words()