KScript docs
EN

KScript.Document

Papers, reports, letters and invoices with LaTeX maths, saved as PDF, LaTeX, Word, Markdown or HTML without a TeX install.

Import it at the top of a script:

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

A file whose root element is <KScript.Document> imports it for you.

A .kscript file names a package only in its import: write Document { }, never KScript.Document { }.

KScript.Document writes PDF and Word files, so its examples can't run inside the docs. Copy one into a .ks.xml file and run it with kscript.

ElementsStatements

#<Abstract>Abstract

The summary under the title. A blank line starts a new paragraph.

Holds text with { } values. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

Example
<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

An author, with an affiliation and email. Its text is the name.

Holds text with { } values. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
affiliationa string with { }Where they work or study.
emaila string with { }Their email address.
Example
<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

The list of works cited with [@key]: from a BibTeX file, Source elements inside it, or both. Without one, the list goes at the end.

Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
patha file pathA BibTeX (.bib) file, relative to the script.
titlea string with { }The heading (default References).
Example
<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

A block of source code in a monospaced font, used as written.

Holds text with { } values. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
ida string with { }A name for [#id] cross-references, such as id: "intro".
languagea string with { }The language, such as python, for Markdown and HTML.
Example
<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

The table of contents, with page numbers in PDFs.

Self-closing.A statement on its own. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
titlea string with { }The heading (default Contents).
depthexpressionHow many levels to list: 2 lists sections and subsections (default 3).
Example
<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

A document. Its content (Section, Paragraph, List, Table, Figure, Math...) is laid out by the template and saved where to: says, or kept in into:. Statements such as for, if and let work inside it.

Holds statements.Holds statements in { }. Scripts only.

AttributeOptionValueDescription
templateplain|paper|report|letter|invoiceThe look: plain (the default), paper, report, letter or invoice.
titlea string with { }The document's title.
subtitlea string with { }A line under the title.
authora string with { }The author, or several separated by ; (Author elements can add affiliations and emails).
datea string with { }The date shown under the title, such as { @now()->format('d MMMM yyyy') }.
keywordsa string with { }Keywords, as a list: ['cellular automata', 'Game of Life'] (in XML, cellular automata, Game of Life). They are shown under the abstract and saved in the file's properties.
toa string with { }Where to save it, by extension: paper.pdf, paper.tex, paper.docx, paper.md or paper.html. Save several with a list: [paper.pdf, paper.docx].
intoa new variable's nameA variable that receives the document, for ->save(), ->pages() or ->convert(to: markdown) later.
papera4 | a3 | a5 | b5 | letter | legalPaper size: a4 (the default), a3, a5, b5, letter, legal, or a size such as '15cm x 20cm'.
landscapetrue or falsetrue turns the paper sideways.
margina string with { }Margins: one size (2.5cm), or top/right/bottom/left like CSS ('2cm 2.5cm').
fontserif|sans|monoBody text: serif (Times), sans (Helvetica) or mono (Courier).
heading_fontserif|sans|monoHeadings: serif, sans or mono.
sizea string with { }Body text size in points, such as 11.
line_heighta string with { }Baseline to baseline as a multiple of the text size, such as 1.4.
alignjustify|left|center|rightBody text: justify, left, center or right.
accenta string with { }The colour of headings and the title page: '#1f4e79' or a name such as 'navy'.
numberedtrue or falsefalse leaves sections without numbers.
title_pagetrue or falsetrue puts the title, authors, date and abstract on a page of their own.
chapterstrue or falsetrue starts each top-level section on a new page.
citationsnumeric|author-yearnumeric ([1]) or author-year ((Conway, 1970)).
headera string with { }The page header: 'left | centre | right', with [page], [pages], [title], [author] and [date].
footera string with { }The page footer, like header. The default shows the page number.
rulesbooktabs|grid|noneTable lines: booktabs (above, below and under the header), grid or none.
languagea string with { }The language, such as en or en-GB, for Word and HTML.
Example
<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

An image (PNG or JPEG) with a caption. Figures with a caption are numbered.

Self-closing.A statement on its own. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
path requireda file pathThe image, relative to the script.
captiona string with { }The caption, shown below as 'Figure 1: ...'.
ida string with { }A name for [#id] cross-references, such as id: "intro".
widthexpressionThe share of the text width, from 0 to 1 (or '60%').
Example
<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

An invoice: from and to addresses, the invoice number and dates, a table of items with subtotal, tax and total, and notes.

Self-closing.A statement on its own. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
items requiredexpressionThe lines: a list of maps with description, quantity (default 1) and price (or amount).
numbera string with { }The invoice number.
froma string with { }Your name and address, one line each.
toa string with { }The customer's name and address.
datea string with { }The invoice date (default: today).
duea string with { }When payment is due.
currencya string with { }A currency code (GBP, USD, EUR, JPY...) or symbol (default GBP).
taxexpressionThe tax rate in percent, such as 20. Leave it out for no tax line.
tax_namea string with { }What the tax is called (default VAT).
paidexpressionAn amount already paid, shown with the balance due.
notesa string with { }Payment details or terms, under the table.
titlea string with { }The heading (default Invoice).
Example
<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

An item of a list. Its text works like a Paragraph's.

Holds text with { } values. Scripts only. Goes inside <List>List.

Example
<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, from its text, a latex value or a .tex file, read as a document part reads it: sections, paragraphs, lists, maths, quotes, figures and the usual text commands. Its text is used as written, so braces are LaTeX's.

Holds text with { } values. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
patha file pathA .tex file, relative to the script.
valueexpressionA latex value, such as what a function that returns a latex block gives.
Example
<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

A letter: the sender's address and date on the right, the recipient, a subject line, a greeting, the body inside it, a closing and a signature.

Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
froma string with { }The sender's name and address, one line each.
toa string with { }The recipient's name and address, one line each.
datea string with { }The date (default: today, as 20 November 2026).
subjecta string with { }The subject line, shown in bold.
openinga string with { }The greeting, such as 'Dear Sam,'.
closinga string with { }The sign-off, such as 'Yours sincerely,'.
signaturea string with { }The name under the sign-off, with a line for a title if needed.
Example
<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

A bulleted or numbered list of Item elements. A List right after an Item is nested in it.

Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
stylebullet|'number'|letter|romanbullet (the default), number, letter or roman.
startexpressionThe first number, such as 3.
Example
<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, from its text or a file: # headings become sections, with paragraphs, lists, tables, code, quotes, images and maths ([ ] in scripts, also $ \(and\) $$ in files).

Holds text with { } values. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
patha file pathA Markdown file, relative to the script.
Example
<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

A displayed equation in LaTeX, numbered unless numbered: false. Its text is used as written, so braces are LaTeX's. Inside matrix, cases and aligned, a new line ends a row.

Holds text with { } values. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
ida string with { }A name for [#id] cross-references, such as id: "intro".
numberedtrue or falsefalse leaves it without an equation number.
Example
<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

Starts a new page.

Self-closing.A statement on its own. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

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

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

#<Paragraph>Paragraph

A paragraph. Its text may hold bold, italic, code, links, ( maths ), [@key] citations, [#id] cross-references and ^[footnotes]. A blank line starts a new paragraph.

Holds text with { } values. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
ida string with { }A name for [#id] cross-references, such as id: "intro".
alignleft|center|right|justifyleft, center, right or justify (the document's setting is the default).
linestrue or falsetrue keeps the line breaks in the text, as in an address or a poem.
Example
<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

A quotation, set in from both margins.

Holds text with { } values. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
bya string with { }Who said it.
Example
<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

A horizontal line across the text.

Self-closing.A statement on its own. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

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

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

#<Section>Section

A section with a numbered heading. A Section inside a Section is a subsection.

Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
title requireda string with { }The heading. It may hold bold, italic and ( maths ).
ida string with { }A name for [#id] cross-references, such as id: "intro".
numberedtrue or falsefalse leaves this section without a number.
new_pagetrue or falsetrue starts it on a new page.
Example
<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

A work to cite, written out: id: is the key for [@key], and other attributes are BibTeX fields (author, title, year, journal, volume, pages, publisher, url, doi...).

Self-closing.A statement on its own. Scripts only. Goes inside <Bibliography>Bibliography. Other attributes are more fields of the source, such as journal, volume, pages, url or doi.Other options are more fields of the source, such as journal, volume, pages, url or doi.

AttributeOptionValueDescription
id requireda string with { }The citation key, as in [@conway1970].
typea stringarticle, book, inproceedings, misc...
authora string with { }Authors, separated by ' and ' as in BibTeX.
titlea string with { }The title.
yeara string with { }The year.
Example
<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

A table: from rows:, or from Tr rows inside it. Tables with a caption are numbered. It takes the same rows, columns, align and header as the terminal's table.

Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
captiona string with { }The caption, shown above the table as 'Table 1: ...'.
ida string with { }A name for [#id] cross-references, such as id: "intro".
rowsexpressionThe rows: a list of maps (each a row), or a list of lists (the first is the heading row).
columnsexpressionWhich values of each map to show, in order: a list of keys ['name', 'age'], or a map from heading to key { Name: 'name' }. Defaults to the first row's keys.
alignexpressionleft, center or right for each column, such as ['left', 'right']. Numbers line up right by default.
widthsexpressionRelative column widths for a table across the page, such as [3, 1, 1].
rulesbooktabs|grid|nonebooktabs, grid or none (the document's setting is the default).
headertrue or falsefalse: no heading row.
sizeexpressionText size relative to the body, such as 0.9.
Example
<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

A table row: cells separated by |, each like a Paragraph's text. | is a | inside a cell.

Holds text with { } values. Scripts only. Goes inside <Table>Table.

AttributeOptionValueDescription
headertrue or falsetrue makes it a heading row, repeated at the top of each page.
Example
<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

Vertical space.

Self-closing.A statement on its own. Scripts only. Goes inside <KScript.Document>Document or <fn>fn.

AttributeOptionValueDescription
height requireda string with { }How much: a length such as '12pt', '5mm' or '1cm'.
Example
<KScript.Document title="Gliders" to="gliders.pdf">
  <VSpace height="1cm" />
</KScript.Document>
import KScript.Document

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

Commands

Commands are called with @, in any expression: @name(arguments).

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

Makes a document from Markdown or LaTeX source. A fenced value supplies the format. A plain string defaults to Markdown.
ParameterTakes
text requiredany value
as optionalone of these words: 'markdown'|'latex'

Arguments can be named: @document(…, as: …).

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

#@document_templates() → map

The built-in document templates and what each is for: { paper: '...', report: '...' }.
Example
<echo>{ @document_templates()->keys() }</echo>
echo @document_templates()->keys()

#@latex_escape(text) → string

Text made safe to put in a LaTeX file: \ $ & % # _ ~ ^ escaped.
Example
<echo>{ @latex_escape('50% of $10 &amp; up') }</echo>
echo @latex_escape('50% of $10 & up')

Methods

On markdown values: ->find(), ->replace(), ->insert(), ->save(), ->pages(), ->words()

On latex values: ->find(), ->replace(), ->insert(), ->save(), ->pages(), ->words()

On document values: ->save(), ->bytes(), ->find(), ->replace(), ->insert(), ->pages(), ->words()