KnowledgeBaseclass#
Immutable knowledge shared by parsers, engines, and documents.
A knowledge base defines the known commands, environments, characters, and delimiters that parsing, transforms, and Document construction and edits check against. Share one instance across a Parser, TransformEngine, and their documents.
Equality and hashing use instance identity: separately constructed knowledge bases are distinct even with identical recipes. Query results are copies. packages=None loads the default runtime packages; [] loads none.
kb = texform.KnowledgeBase(["ams"])
doc = texform.Parser(kb).parse(r"\frac{a}{b}")["document"]KnowledgeBase#
KnowledgeBase( packages: list[str] | None = None, items: list[ContextItem] | None = None, remove_commands: list[str] | None = None, remove_environments: list[str] | None = None, remove_delimiter_controls: list[str] | None = None,) -> NoneBuild the knowledge base once.
| Parameter | Type | Default | Description |
|---|---|---|---|
packages | list[ | None | Package names to load, or |
items | list[ | None | Extra command, environment, or delimiter items to add. |
remove_commands | list[ | None | Command names to remove after loading. |
remove_environments | list[ | None | Environment names to remove after loading. |
remove_delimiter_controls | list[ | None | Delimiter control names to remove after loading. |
ConfigError—If a package name or an item is invalid.
commands#
commands(mode: Literal['math', 'text']) -> list[dict[str, Any]]Return the command records for mode, sorted by name.
| Parameter | Type |
|---|---|
mode | Literal[ |
list[dict[str, Any]]environments#
environments(mode: Literal['math', 'text']) -> list[dict[str, Any]]Return the environment records for mode, sorted by name.
| Parameter | Type |
|---|---|
mode | Literal[ |
list[dict[str, Any]]characters#
characters(mode: Literal['math', 'text']) -> list[dict[str, Any]]Return the character records for mode, sorted by name.
| Parameter | Type |
|---|---|
mode | Literal[ |
list[dict[str, Any]]delimiters#
delimiters() -> list[dict[str, Any]]Return delimiter records sorted by name.
list[dict[str, Any]]lookup_command#
lookup_command( name: str, mode: Literal['math', 'text'],) -> dict[str, Any] | NoneLook up the full knowledge entry for a command in a given mode.
| Parameter | Type | Description |
|---|---|---|
name | str | The command name without the leading backslash. |
mode | Literal[ |
|
dict[str, Any] | None — A dict describing the command, including its parsed args slots, or None if the command is unknown in that mode.
lookup_explicit_command#
lookup_explicit_command( name: str, mode: Literal['math', 'text'],) -> dict[str, Any] | NoneLook up the knowledge entry for an explicit command in a given mode.
| Parameter | Type | Description |
|---|---|---|
name | str | The command name without the leading backslash. |
mode | Literal[ |
|
dict[str, Any] | None — A knowledge-entry dict, or None if unknown in that mode.
lookup_character#
lookup_character( name: str, mode: Literal['math', 'text'],) -> dict[str, Any] | NoneLook up the knowledge entry for a character in a given mode.
| Parameter | Type | Description |
|---|---|---|
name | str | The character name without the leading backslash. |
mode | Literal[ |
|
dict[str, Any] | None — A knowledge-entry dict, or None if unknown in that mode.
lookup_env#
lookup_env( name: str, mode: Literal['math', 'text'],) -> dict[str, Any] | NoneLook up the knowledge entry for an environment in a given mode.
| Parameter | Type | Description |
|---|---|---|
name | str | The environment name. |
mode | Literal[ |
|
dict[str, Any] | None — A knowledge-entry dict, or None if unknown in that mode.
is_delimiter_control#
is_delimiter_control(name: str) -> boolReport whether a name is a delimiter control such as langle.
| Parameter | Type | Description |
|---|---|---|
name | str | The control name without the leading backslash. |
bool — True if the name is a known delimiter control.
texform.KnowledgeBase().is_delimiter_control("langle") # Trueknows_command_name#
knows_command_name(name: str) -> boolReport whether a command name is known, ignoring mode.
| Parameter | Type | Description |
|---|---|---|
name | str | The command name without the leading backslash. |
bool — True if the command is known in any mode.
knows_env_name#
knows_env_name(name: str) -> boolReport whether an environment name is known, ignoring mode.
| Parameter | Type | Description |
|---|---|---|
name | str | The environment name. |
bool — True if the environment is known in any mode.
knows_character_name#
knows_character_name(name: str) -> boolReport whether a character name is known, ignoring mode.
| Parameter | Type | Description |
|---|---|---|
name | str | The character name without the leading backslash. |
bool — True if the character is known in any mode.
CommandItemdict type#
A custom command injected into parser knowledge.
| Field | Type | Description |
|---|---|---|
target | Literal[ | Discriminator; always |
name | str | Command name without the leading backslash. |
kind | Literal[ | Syntactic role: |
allowed_mode | Literal[ | Modes in which the command is valid: |
argspec | str | xparse-style argument specification, such as |
tags | Not | Optional free-form classification tags carried into the knowledge entry. |
EnvironmentItemdict type#
A custom environment injected into parser knowledge.
| Field | Type | Description |
|---|---|---|
target | Literal[ | Discriminator; always |
name | str | Environment name as written in |
allowed_mode | Literal[ | Modes in which the environment may appear. |
body_mode | Literal[ | Content mode of the environment body ( |
argspec | str | xparse-style argument specification for the environment header. |
tags | Not | Optional free-form classification tags. |
DelimiterItemdict type#
A custom delimiter control injected into parser knowledge.
| Field | Type | Description |
|---|---|---|
target | Literal[ | Discriminator; always |
name | str | Delimiter-control name without the leading backslash (for example |