Nodeclass#
A read-only handle into a Document for navigation and reading.
A Node is a cheap reference — a shared pointer into the owning document
plus an identity, never a copy of the subtree. It carries no editing methods;
all mutation goes through the owning Document. Accessors return content
only for the node kinds that carry it and None otherwise, so the same
handle works uniformly across kinds.
You obtain handles from Document.root(), Document.errors(),
Document.find_commands(), the create_* staging constructors, and the
navigation methods here.
doc = texform.Parser().parse(r"\frac{x}{y}")["document"]
cmd = doc.root().children()[0]
cmd.kind() # 'Command'
cmd.is_command("frac") # TrueKind#
Predicates#
is_command#
is_command(name: str | None = None) -> boolReport whether the node is a command, optionally a specific one.
| Parameter | Type | Default | Description |
|---|---|---|---|
name | str | | None | A command name to match, or |
bool — True if the node is a command (and matches name when given).
node.is_command() # any command
node.is_command("frac") # specifically \fracis_char#
is_char(value: str | None = None) -> boolReport whether the node is a char, optionally a specific one.
| Parameter | Type | Default | Description |
|---|---|---|---|
value | str | | None | A character to match, or |
bool — True if the node is a char (and holds value when given).
node.is_char("+")is_error#
is_error() -> boolReport whether the node is an Error placeholder.
bool — True for an Error node.
is_known#
is_known() -> bool | NoneReport whether the document's knowledge base knows this command or environment.
bool | None — True or False for command-like nodes and environments, None for other kinds.
Navigation#
parent#
parent() -> Node | NoneReturn the parent node, or None for the root.
Node | None — The parent handle, or None.
children#
children() -> list[Node]Return the node's direct children.
list[Node] — The child handles in order.
doc = texform.Parser().parse(r"\frac{x}{y} + a")["document"]
[n.kind() for n in doc.root().children()] # ['Command', 'Char', 'Char']next_sibling#
next_sibling() -> Node | NoneReturn the following sibling, or None if this is the last child.
Node | None — The next sibling handle, or None.
prev_sibling#
prev_sibling() -> Node | NoneReturn the preceding sibling, or None if this is the first child.
Node | None — The previous sibling handle, or None.
ancestors#
ancestors() -> list[Node]Return the node's ancestors from the parent upward.
list[Node] — The ancestor handles, nearest first.
descendants#
descendants() -> list[Node]Return every descendant of the node.
list[Node] — The descendant handles in document order.
path#
path() -> str | NoneReturn the node's current path from the root, or None if detached.
The path reflects the tree now and changes after edits; Document.node_at resolves it.
str | Noneslot#
slot() -> NodeSlot | NoneReturn the slot this node fills in its parent, or None for the root and detached nodes.
NodeSlot | None — A dict with the slot kind and, for indexed slots such as children and arguments, its index.
Content accessors#
command_name#
command_name() -> str | NoneReturn the command name for a Command / Declarative node.
str | None — The name without the leading backslash, or None for other kinds.
env_name#
env_name() -> str | NoneReturn the environment name for an Environment node.
str | None — The environment name, or None for other kinds.
text#
text() -> str | NoneReturn the value of a Text node.
str | None — The text string, or None for other kinds.
char#
char() -> str | NoneReturn the character of a Char node.
str | None — The single character, or None for other kinds.
prime_count#
prime_count() -> int | NoneReturn the symbol count of a math Prime node.
Scripted determines whether these symbols form a superscript.
int | None — The count (greater than zero), or None for other kinds.
doc = texform.Parser().parse(r"f''")["document"]
prime = next(n for n in doc.root().descendants() if n.kind() == "Prime")
prime.prime_count() # 2error_parts#
error_parts() -> ErrorParts | NoneReturn the decomposed content of an Error node.
ErrorParts | None — A dict {"message": str, "snippet": str}, or None for other kinds.
doc = texform.Parser().parse(r"\sqrt[")["document"]
err = next(n for n in doc.root().descendants() if n.is_error())
err.error_parts() # {'message': '...', 'snippet': '\sqrt['}content_mode#
content_mode() -> RuntimeContentMode | NoneReturn the runtime content mode where applicable.
RuntimeContentMode | None — "math" or "text" for nodes that carry a mode, or None.
Groups#
group_kind#
group_kind() -> GroupKindRef | NoneReturn the group kind for a Group node.
GroupKindRef | None — A dict with a kind discriminator (Explicit, Implicit, InlineMath, or Delimited); a Delimited group also carries left and right delimiter values. None for other kinds.
doc = texform.Parser().parse(r"{x}")["document"]
group = next(n for n in doc.root().descendants() if n.kind() == "Group")
group.group_kind() # {'kind': 'Explicit'}Command arguments#
arg_count#
arg_count() -> intReturn the number of argument slots for a command-like node.
int — The slot count (zero for nodes that take no arguments).
arg#
arg(index: int) -> ArgRef | NoneReturn the argument at index, or None if the slot is empty.
| Parameter | Type | Description |
|---|---|---|
index | int | The zero-based slot index. |
ArgRef | None — An argument-value dict — content kinds (Math, Text) carry a live node; leaf kinds carry a value — or None for an empty slot.
doc = texform.Parser().parse(r"\frac{x}{y}")["document"]
frac = next(n for n in doc.root().descendants() if n.is_command("frac"))
frac.arg_count() # 2
frac.arg(0) # {'kind': 'Math', 'node': <Node>}arg_slots#
arg_slots() -> list[ArgRef | None]Return all argument slots, with None for any empty slot.
list[ArgRef | None] — The list of argument-value dicts and None placeholders.
Scripts and infix#
script_base#
script_base() -> Node | NoneReturn the base of a Scripted node.
Node | None — The base node handle, or None for other kinds.
subscript#
subscript() -> Node | NoneReturn the subscript slot of a Scripted node.
Node | None — The subscript node handle, or None if absent or another kind.
doc = texform.Parser().parse(r"x_i^2")["document"]
sc = next(n for n in doc.root().descendants() if n.kind() == "Scripted")
sc.subscript().kind(), sc.superscript().kind() # ('Char', 'Char')superscript#
superscript() -> Node | NoneReturn the superscript slot of a Scripted node.
Node | None — The superscript node handle, or None if absent or another kind.
infix_left#
infix_left() -> Node | NoneReturn the left operand of an Infix node.
Node | None — The left operand handle, or None for other kinds.
infix_right#
infix_right() -> Node | NoneReturn the right operand of an Infix node.
Node | None — The right operand handle, or None for other kinds.
env_body#
env_body() -> Node | NoneReturn the body group of an Environment node.
Node | None — The body node handle, or None for other kinds.
NodeKindtype#
Literal['Root', 'Group', 'Command', 'Infix', 'Declarative', 'Environment', 'Scripted', 'Prime', 'Text', 'Char', 'ActiveSpace', 'AlignmentTab', 'Error']Node kind as reported by Node.kind(). Python uses PascalCase ("Root", "Command", "ActiveSpace").
RuntimeContentModetype#
Literal['math', 'text']Runtime content mode reported by the tree API: "math" or "text".
ErrorPartsdict type#
The decomposed content of an Error placeholder node.
| Field | Type | Description |
|---|---|---|
message | str | The reason the fragment failed to parse. |
snippet | str | The captured source text, re-emitted verbatim on serialization. |
GroupKindExplicitdict type#
A group written with explicit braces { ... }.
| Field | Type | Description |
|---|---|---|
kind | Literal[ | Discriminator; always |
GroupKindImplicitdict type#
A group with no surrounding braces, inferred from structure.
| Field | Type | Description |
|---|---|---|
kind | Literal[ | Discriminator; always |
GroupKindDelimiteddict type#
A group bounded by a delimiter pair, such as \left( ... \right).
| Field | Type | Description |
|---|---|---|
kind | Literal[ | Discriminator; always |
left | Delimiter | The opening delimiter value. |
right | Delimiter | The closing delimiter value. |
GroupKindInlineMathdict type#
A group introduced by inline math shift, such as $ ... $ in text mode.
| Field | Type | Description |
|---|---|---|
kind | Literal[ | Discriminator; always |
GroupKindReftype#
GroupKindExplicit | GroupKindImplicit | GroupKindDelimited | GroupKindInlineMathThe kind of a Group node: explicit, implicit, delimited, or inline-math.
DelimiterValuetype#
strA delimiter string: a character, a backslash-prefixed control, or a dot.
MathArgdict type#
A command argument carrying math-mode content.
| Field | Type | Description |
|---|---|---|
kind | Literal[ | Discriminator; always |
node | Node | — |
TextArgdict type#
A command argument carrying text-mode content.
| Field | Type | Description |
|---|---|---|
kind | Literal[ | Discriminator; always |
node | Node | — |
DelimiterArgdict type#
A command argument that is a single delimiter token.
| Field | Type | Description |
|---|---|---|
kind | Literal[ | Discriminator; always |
value | Delimiter | — |
CSNameArgdict type#
A command argument that is a control-sequence name.
| Field | Type | Description |
|---|---|---|
kind | Literal[ | Discriminator; always |
value | str | — |
DimensionArgdict type#
A command argument that is a TeX dimension, such as 2pt.
| Field | Type | Description |
|---|---|---|
kind | Literal[ | Discriminator; always |
value | str | — |
IntegerArgdict type#
A command argument that is an integer literal.
| Field | Type | Description |
|---|---|---|
kind | Literal[ | Discriminator; always |
value | str | — |
KeyValArgdict type#
A command argument that is a key=value list.
| Field | Type | Description |
|---|---|---|
kind | Literal[ | Discriminator; always |
value | str | — |
ColumnArgdict type#
A command argument that is a tabular column specification.
| Field | Type | Description |
|---|---|---|
kind | Literal[ | Discriminator; always |
value | str | — |
BooleanArgdict type#
A command argument that is a star flag, modeled as a boolean.
| Field | Type | Description |
|---|---|---|
kind | Literal[ | Discriminator; always |
value | bool | — |
ArgReftype#
MathArg | TextArg | DelimiterArg | OperatorNameArg | CSNameArg | DimensionArg | IntegerArg | KeyValArg | ColumnArg | BooleanArgA read command argument: one of the discriminated argument-value dicts.
ArgFormKindtype#
Literal['mandatory', 'optional', 'star', 'group', 'until', 'delimited', 'paired']NodeSlotKindtype#
Literal['child', 'arg', 'script_base', 'subscript', 'superscript', 'infix_left', 'infix_right', 'env_body']ArgFormdict type#
| Field | Type |
|---|---|
kind | Arg |
open | Not |
close | Not |
OperatorNameArgdict type#
| Field | Type |
|---|---|
form | Arg |
kind | Literal[ |
node | Node |
Pairedclass#
Immutable content and delimiter pair for an argspec Paired slot.
Pass it as a slot value to select an explicit delimiter pair. value is a detached node or source written between open and close. Instances compare and hash by value.
doc.create_command("sqrt", [texform.Paired("x", "[", "]"), "y"])Paired#
Paired(value: Node | str, open: str, close: str) -> None| Parameter | Type |
|---|---|
value | Node | |
open | str |
close | str |
Argtype#
Node | str | bool | None | PairedSlot input: source written inside the slot, a detached content node, a star flag, absence, or an explicit Paired delimiter pair.
NodeSlotdict type#
| Field | Type |
|---|---|
kind | Node |
index | int | |