KScript 文档
ZH

文档:KScript.Document

表格

表格可以由数据生成,数据通常是一个映射列表:

<let name="people" value="[{ name: 'Ada', born: 1815 }, { name: 'Alan', born: 1912 }]" />

<import package="KScript.Document" />

<Document title="People" to="'people.pdf'">
  <Table caption="Two mathematicians." id="tab-people" rows="$people" columns="{ Name: 'name', Born: 'born' }" align="['left', 'right']" />
</Document>
$people = [{ name: 'Ada', born: 1815 }, { name: 'Alan', born: 1912 }]

import KScript.Document

Document(title: 'People', to: 'people.pdf') {
  Table(caption: 'Two mathematicians.', id: 'tab-people', rows: $people, columns: { Name: 'name', Born: 'born' }, align: ['left', 'right'])
}

columns: 是从表头到键的映射(或键的列表,同时也作为表头)。若未提供,则以第一个映射的键作为列。当行的字段已知时(例如脚本中写入的映射列表或模型记录),编辑器会补全键,kscript check 会报告某行缺少的键。除非 align: 另有指定,数字默认右对齐。

或者用 Tr 写入行,单元格之间用 | 分隔:

<KScript.Document title="Scores" to="'scores.pdf'">
  <Table caption="Scores." align="['left', 'right', 'right']" widths="[3, 1, 1]" rules="grid">
    <Tr header="true">Team | Played | Points</Tr>
    <Tr>Red | 3 | 7</Tr>
    <Tr>Blue | 3 | **9**</Tr>
  </Table>
</KScript.Document>
import KScript.Document

Document(title: 'Scores', to: 'scores.pdf') {
  Table(caption: 'Scores.', align: ['left', 'right', 'right'], widths: [3, 1, 1], rules: grid) {
    Tr(header: true) "Team | Played | Points"
    Tr "Red | 3 | 7"
    Tr "Blue | 3 | **9**"
  }
}

widths: 会使表格宽度与文本宽度一致,并按这些比例分配宽度。未提供时,各列宽度与其文本宽度相同。rules: 可取 booktabs(上方、下方及表头下方有线,默认)、grid 或 none;size: 0.9 会使文字变小。标题会为表格编号;当表格跨页时,表头行会重复出现(在 LaTeX 中,超过 30 行的表格会因同样原因成为 longtable)。没有任何行的表格会从 LaTeX 和 Markdown 文件中省略。