latex
LaTeX backend for lowering symbolic expressions to math strings.
This package implements the LaTeX lowering backend that converts symbolic
expression AST nodes into LaTeX math strings. Like the JAX and CVXPy
backends, lowering uses a visitor pattern where each expression type has a
corresponding visitor function registered via @visitor.
The visitor functions are split across submodules that mirror the
openscvx.symbolic.expr package structure. Importing this package
triggers registration of all visitors, which now cover every shipped node
type — arithmetic, linear algebra, elementwise math, arrays, constraints,
logic, spatial, Lie-group, STL, and Vmap. A future node added without a
visitor raises NotImplementedError — the designed fallback until its
visitor lands. (The legacy stljax operators are intentionally left
unregistered; the GMSR ox.stl surface is the rendered one.)
Example::
from openscvx.symbolic.lowerers.latex import LatexLowerer
lowerer = LatexLowerer()
s = lowerer.lower(ox.Norm(x) - 5.0) # '\left\| x \right\| - 5'
LatexLowerer
¶
LaTeX backend for lowering symbolic expressions to math strings.
This class implements the visitor pattern for converting symbolic
expression AST nodes to LaTeX. Each expression type has a corresponding
visitor function decorated with @visitor that returns a str.
The lowering is recursive: each visitor lowers its children first, then
composes them into a LaTeX fragment, calling :func:wrap on children that
may need parentheses for the surrounding precedence.
Unlike :class:~openscvx.symbolic.lowerers.jax.JaxLowerer, the LaTeX
lowerer is stateless — rendering is pure string assembly with no trace,
node cache, or memoization.
Example
Lower an expression to a LaTeX string::
lowerer = LatexLowerer()
s = lowerer.lower(ox.Norm(x) - 5.0)
# '\left\| x \right\| - 5'
Source code in openscvx/symbolic/lowerers/latex/_lowerer.py
lower(expr: Expr) -> str
¶
Lower a symbolic expression to a LaTeX string.
Main entry point for lowering. Delegates to :func:dispatch, which
looks up the appropriate visitor based on the expression type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Expr
|
Symbolic expression to lower (any Expr subclass) |
required |
Returns:
| Type | Description |
|---|---|
str
|
LaTeX math string (no |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If no visitor exists for the expression type |
Source code in openscvx/symbolic/lowerers/latex/_lowerer.py
format_constant(value: np.ndarray) -> str
¶
Render a numeric constant as LaTeX.
Scalars render via %g. Vectors render as a column bmatrix and
matrices as a 2-D bmatrix, up to 6 entries per axis. Larger arrays
render as a placeholder \mathrm{const} \in \mathbb{R}^{...} rather
than an unreadable wall of numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
ndarray
|
The constant value (scalar, vector, matrix, or higher-rank array). |
required |
Returns:
| Type | Description |
|---|---|
str
|
The LaTeX fragment for the constant. |
Source code in openscvx/symbolic/lowerers/latex/_lowerer.py
latex_symbol(name: str) -> str
¶
Render a variable name as a LaTeX symbol.
Rules, in order:
- A Greek-word name renders as its command:
'alpha'->'\alpha'. - A single character renders bare:
'x'->'x'. - A
base_subname splits on the first underscore into a subscript:'x_pos'->'x_{\mathrm{pos}}'(base and subscript are rendered recursively). - Any other multi-letter name renders upright:
'position'->'\mathrm{position}', with underscores escaped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The variable name to render. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The LaTeX fragment for the name. |
Source code in openscvx/symbolic/lowerers/latex/_lowerer.py
problem_to_latex(symbolic: SymbolicProblem, dynamics_dict: dict, lam_cost: Union[float, np.ndarray], *, dynamics: str, constraints: str, weights: str = 'symbolic', env: Literal['align', 'aligned'] = 'align') -> str
¶
Assemble a Mayer-form LaTeX formulation of a problem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbolic
|
SymbolicProblem
|
The preprocessed :class: |
required |
dynamics_dict
|
dict
|
The user-authored |
required |
lam_cost
|
Union[float, ndarray]
|
Objective weights — a scalar or an |
required |
dynamics
|
str
|
Detail level for the dynamics section — |
required |
constraints
|
str
|
Detail level for the constraint section — |
required |
weights
|
str
|
How objective coefficients render — |
'symbolic'
|
env
|
Literal['align', 'aligned']
|
Output envelope. |
'align'
|
Returns:
| Type | Description |
|---|---|
str
|
One LaTeX display-math fragment: the formulation block followed, in |
str
|
|
str
|
joined by newlines with no connective glue. No |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |