ml_switcheroo.core.tikz¶

TikZ Concrete Syntax Tree (CST) Data Structures.

This package provides a pure Python representation of TikZ/PGF code designed to represent neural network graphs with high fidelity. It supports: - Nodes with HTML-like label tables for layer metadata. - Edges representing data flow. - Trivia preservation (comments/whitespace) for round-trip stability.

Submodules¶

Classes¶

TikzBaseNode

Abstract base class for all TikZ CST nodes.

TriviaNode

Represents non-semantic textual elements (whitespace, newlines).

TikzComment

Represents a LaTeX comment (e.g. % My Comment).

TikzOption

Represents a TikZ option like [draw=black] or [circle].

TikzTable

Represents an HTML-like table structure used inside TikZ Node labels.

TikzNode

Represents a \node command.

TikzEdge

Represents a \draw command connecting two nodes.

TikzGraph

The root container representing the tikzpicture environment.

Package Contents¶

class ml_switcheroo.core.tikz.TikzBaseNode[source]¶

Bases: abc.ABC

Abstract base class for all TikZ CST nodes.

abstractmethod to_text() → str[source]¶

Render this node to its string representation.

Returns:

The TikZ/LaTeX source code for this construct.

Return type:

str

class ml_switcheroo.core.tikz.TriviaNode[source]¶

Bases: TikzBaseNode

Represents non-semantic textual elements (whitespace, newlines).

content: str¶

The raw whitespace content.

to_text() → str[source]¶

Returns the raw whitespace content.

class ml_switcheroo.core.tikz.TikzComment[source]¶

Bases: TikzBaseNode

Represents a LaTeX comment (e.g. % My Comment). Includes the percent sign in the content or adds it during export.

text: str¶

The comment text.

trailing_newline: bool = True¶

Whether to append a newline after the comment.

to_text() → str[source]¶

Formats the comment with a leading percent sign.

class ml_switcheroo.core.tikz.TikzOption[source]¶

Bases: TikzBaseNode

Represents a TikZ option like [draw=black] or [circle].

key: str¶

Option key.

value: str | None = None¶

Optional value for key-value pairs.

to_text() → str[source]¶

Returns key=value or just key.

class ml_switcheroo.core.tikz.TikzTable[source]¶

Bases: TikzBaseNode

Represents an HTML-like table structure used inside TikZ Node labels. Uses LaTeX tabular environment syntax.

Example:

\begin{tabular}{c}
    \textbf{LayerName} \\
    param: val
\end{tabular}
rows: List[List[str]] = []¶

List of rows, where each row is a list of cell strings.

align: str = 'c'¶

Column alignment (c=center, l=left, r=right).

to_text() → str[source]¶

Renders the tabular environment string.

class ml_switcheroo.core.tikz.TikzNode[source]¶

Bases: TikzBaseNode

Represents a \node command.

Structure:

\node [options] (id) at (x, y) {label_content};
node_id: str¶

Unique identifier for the node (used for edges).

x: float¶

X Coordinate.

y: float¶

Y Coordinate.

content: str | TikzTable¶

Inner content (Text or Table).

options: List[TikzOption] = []¶

List of TikZ options.

leading_trivia: List[TriviaNode] = []¶

Whitespace/Comments before the node command.

to_text() → str[source]¶

Constructs the full node command string.

class ml_switcheroo.core.tikz.TikzEdge[source]¶

Bases: TikzBaseNode

Represents a \draw command connecting two nodes.

Structure:

\draw [options] (src) -- (tgt);
source_id: str¶

Source node ID.

target_id: str¶

Target node ID.

options: List[TikzOption] = []¶

List of styling options.

connector: str = '--'¶

Connector style (e.g. -- or ->).

leading_trivia: List[TriviaNode] = []¶

Whitespace before the draw command.

to_text() → str[source]¶

Constructs the draw command string.

class ml_switcheroo.core.tikz.TikzGraph[source]¶

Bases: TikzBaseNode

The root container representing the tikzpicture environment.

Structure:

\begin{tikzpicture}
    ... children ...
\end{tikzpicture}
children: List[TikzBaseNode] = []¶

List of nodes, edges, comments, and trivia.

options: List[TikzOption] = []¶

Global environment options.

to_text() → str[source]¶

Constructs the complete environment string.