expr2katex 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- expr2katex-0.1.0.dist-info/METADATA +168 -0
- expr2katex-0.1.0.dist-info/RECORD +5 -0
- expr2katex-0.1.0.dist-info/WHEEL +4 -0
- expr2katex-0.1.0.dist-info/licenses/LICENSE +21 -0
- expr2katex.py +345 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: expr2katex
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Convert a Python expression string to a KaTeX string
|
|
5
|
+
Project-URL: Homepage, https://github.com/YOUR_USERNAME/expr2katex
|
|
6
|
+
Project-URL: Repository, https://github.com/YOUR_USERNAME/expr2katex
|
|
7
|
+
Project-URL: Issues, https://github.com/YOUR_USERNAME/expr2katex/issues
|
|
8
|
+
License: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: ast,expression,katex,latex,math
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Topic :: Text Processing :: Markup :: LaTeX
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# expr2katex
|
|
25
|
+
|
|
26
|
+
Convert a Python expression string to a [KaTeX](https://katex.org/) string.
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from expr2katex import expr2katex
|
|
30
|
+
|
|
31
|
+
katex, err = expr2katex("sin(a) + (c + 3) * b")
|
|
32
|
+
# katex → \sin\left(a\right)+\left(c+3\right)\times b
|
|
33
|
+
# err → None
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Features
|
|
37
|
+
|
|
38
|
+
- **Zero dependencies** — built on Python's standard `ast` module only
|
|
39
|
+
- **Precedence-aware parenthesisation** — reconstructs grouping from the AST structure, so `(a + b) * c` renders correctly without any string heuristics
|
|
40
|
+
- **KaTeX-native functions** — `sin`, `cos`, `sqrt`, `log`, etc. render with their native KaTeX commands (`\sin`, `\sqrt{x}`, …)
|
|
41
|
+
- **Unknown functions** fall back to `\operatorname{foo}` automatically
|
|
42
|
+
- **Configurable division style** — `\frac{a}{b}` or `a \div b`
|
|
43
|
+
- **Symbol style overrides** — control how variables and function names are rendered per symbol
|
|
44
|
+
- **SyntaxError highlighting** — invalid expressions return a KaTeX string with the offending token highlighted in red, designed for real-time UI previews
|
|
45
|
+
|
|
46
|
+
## Installation
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install expr2katex
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
|
|
54
|
+
### Basic
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from expr2katex import expr2katex
|
|
58
|
+
|
|
59
|
+
katex, err = expr2katex("sin(a) + b * c")
|
|
60
|
+
# \sin\left(a\right)+b\times c
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Division style
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
expr2katex("a / b") # \frac{a}{b} (default)
|
|
67
|
+
expr2katex("a / b", div_style="div") # a \div b
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Power and roots
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
expr2katex("a ** 2") # a^{2}
|
|
74
|
+
expr2katex("sqrt(x)") # \sqrt{x}
|
|
75
|
+
expr2katex("sqrt(x, 3)") # \sqrt[3]{x} (n-th root)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Symbol style overrides
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from expr2katex import expr2katex, SymbolStyle
|
|
82
|
+
|
|
83
|
+
expr2katex("foo(x)", symbol_styles={
|
|
84
|
+
"foo": SymbolStyle.TEXT, # \text{foo}\left(x\right)
|
|
85
|
+
"x": SymbolStyle.BOLD, # \boldsymbol{x}
|
|
86
|
+
})
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
| `SymbolStyle` | Output | Default for |
|
|
90
|
+
|-----------------|-------------------------|--------------|
|
|
91
|
+
| `ITALIC` | `x` | variables |
|
|
92
|
+
| `OPERATORNAME` | `\operatorname{foo}` | functions |
|
|
93
|
+
| `TEXT` | `\text{foo}` | |
|
|
94
|
+
| `BOLD` | `\boldsymbol{x}` | |
|
|
95
|
+
|
|
96
|
+
### SyntaxError highlighting
|
|
97
|
+
|
|
98
|
+
When the expression is invalid, `expr2katex` returns a KaTeX string with the offending token highlighted in red — useful for real-time formula editors.
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
katex, err = expr2katex("sin(a + (b * c")
|
|
102
|
+
# katex → \text{sin(a + }\textcolor{red}{\underbrace{\text{(}}}\text{b * c}
|
|
103
|
+
# err → '(' was never closed
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Choose between `underbrace` (default) and `underline`:
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
expr2katex("sin(a + (b * c", error_style="underline")
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Raising on error
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
# SyntaxError is re-raised instead of being returned
|
|
116
|
+
expr2katex("sin(a + (b * c", raise_on_error=True)
|
|
117
|
+
# SyntaxError: '(' was never closed
|
|
118
|
+
|
|
119
|
+
# All non-SyntaxError exceptions are always raised regardless of raise_on_error.
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## API reference
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
def expr2katex(
|
|
126
|
+
expr: str,
|
|
127
|
+
raise_on_error: bool = False,
|
|
128
|
+
div_style: Literal["frac", "div"] = "frac",
|
|
129
|
+
symbol_styles: dict[str, SymbolStyle] | None = None,
|
|
130
|
+
error_style: Literal["underbrace", "underline"] = "underbrace",
|
|
131
|
+
) -> tuple[str, str | None]: ...
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
| Parameter | Type | Default | Description |
|
|
135
|
+
|-----------------|-----------------------------------|---------------|-------------|
|
|
136
|
+
| `expr` | `str` | — | Python expression string to convert |
|
|
137
|
+
| `raise_on_error`| `bool` | `False` | Re-raise `SyntaxError` instead of returning highlighted KaTeX |
|
|
138
|
+
| `div_style` | `"frac"` \| `"div"` | `"frac"` | Division rendering style |
|
|
139
|
+
| `symbol_styles` | `dict[str, SymbolStyle] \| None` | `None` | Per-symbol style overrides |
|
|
140
|
+
| `error_style` | `"underbrace"` \| `"underline"` | `"underbrace"`| Error highlight decoration |
|
|
141
|
+
|
|
142
|
+
**Returns** `(katex: str, err: str | None)` — `err` is `None` on success, or `SyntaxError.msg` on failure.
|
|
143
|
+
|
|
144
|
+
## Supported operators
|
|
145
|
+
|
|
146
|
+
| Python | KaTeX output |
|
|
147
|
+
|---------|-----------------------|
|
|
148
|
+
| `+` | `a+b` |
|
|
149
|
+
| `-` | `a-b` |
|
|
150
|
+
| `*` | `a\times b` |
|
|
151
|
+
| `/` | `\frac{a}{b}` |
|
|
152
|
+
| `//` | `\lfloor\frac{a}{b}\rfloor` |
|
|
153
|
+
| `%` | `a\bmod b` |
|
|
154
|
+
| `**` | `a^{b}` |
|
|
155
|
+
| `-x` | `-x` |
|
|
156
|
+
| `+x` | `x` (omitted) |
|
|
157
|
+
|
|
158
|
+
## KaTeX-native functions
|
|
159
|
+
|
|
160
|
+
The following function names are rendered with their native KaTeX command (`\sin`, `\cos`, …). All other names fall back to `\operatorname{name}`.
|
|
161
|
+
|
|
162
|
+
`sin` `cos` `tan` `arcsin` `arccos` `arctan` `sinh` `cosh` `tanh` `log` `ln` `exp` `min` `max` `gcd` `lcm` `det` `dim` `sqrt`
|
|
163
|
+
|
|
164
|
+
`sqrt` uses the dedicated `\sqrt{x}` template. Pass a second argument for n-th roots: `sqrt(x, n)` → `\sqrt[n]{x}`.
|
|
165
|
+
|
|
166
|
+
## License
|
|
167
|
+
|
|
168
|
+
MIT
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
expr2katex.py,sha256=XnSBPnGOqJSm8hKhpk7GvjcZzCyOT69etxCoAnHMA9U,13232
|
|
2
|
+
expr2katex-0.1.0.dist-info/METADATA,sha256=VZDJqiwrrlsgszASRwZZ2hUFr_A4ByaQYeCuKgfpj7w,5885
|
|
3
|
+
expr2katex-0.1.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
4
|
+
expr2katex-0.1.0.dist-info/licenses/LICENSE,sha256=Ka5j80Kk4YykLmHMuc8eGizbcj3Uwj9JJRAh07rwIwM,1098
|
|
5
|
+
expr2katex-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 hiroshiasayadev-prog
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
expr2katex.py
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from typing import Literal
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class SymbolStyle(Enum):
|
|
7
|
+
"""Rendering style for variables and function names in KaTeX output."""
|
|
8
|
+
|
|
9
|
+
ITALIC = "italic"
|
|
10
|
+
"""Render as plain italic (default for variables). Example: ``x``"""
|
|
11
|
+
|
|
12
|
+
OPERATORNAME = "operatorname"
|
|
13
|
+
"""Render using ``\\operatorname{}``. Example: ``\\operatorname{foo}``"""
|
|
14
|
+
|
|
15
|
+
TEXT = "text"
|
|
16
|
+
"""Render using ``\\text{}``. Example: ``\\text{foo}``"""
|
|
17
|
+
|
|
18
|
+
BOLD = "bold"
|
|
19
|
+
"""Render using ``\\boldsymbol{}``. Example: ``\\boldsymbol{x}``"""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# Functions that KaTeX renders natively with a leading backslash (e.g. \sin, \cos).
|
|
23
|
+
# All other function names fall back to \operatorname{name}.
|
|
24
|
+
KATEX_NATIVE = {
|
|
25
|
+
"sin", "cos", "tan", "arcsin", "arccos", "arctan",
|
|
26
|
+
"sinh", "cosh", "tanh", "log", "ln", "exp",
|
|
27
|
+
"min", "max", "gcd", "lcm", "det", "dim",
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
# Operator precedence used to decide whether parentheses are needed around a
|
|
31
|
+
# child BinOp node when it appears inside a higher-precedence parent BinOp.
|
|
32
|
+
PRECEDENCE = {
|
|
33
|
+
ast.Add: 1,
|
|
34
|
+
ast.Sub: 1,
|
|
35
|
+
ast.Mult: 2,
|
|
36
|
+
ast.Div: 2,
|
|
37
|
+
ast.FloorDiv: 2,
|
|
38
|
+
ast.Mod: 2,
|
|
39
|
+
ast.Pow: 3,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _apply_symbol_style(name: str, style: SymbolStyle) -> str:
|
|
44
|
+
"""Wrap *name* in the KaTeX command that corresponds to *style*.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
name: The raw symbol name (e.g. ``"x"`` or ``"foo"``).
|
|
48
|
+
style: The desired ``SymbolStyle``.
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
A KaTeX string with the appropriate command applied.
|
|
52
|
+
|
|
53
|
+
Examples:
|
|
54
|
+
>>> _apply_symbol_style("x", SymbolStyle.BOLD)
|
|
55
|
+
'\\\\boldsymbol{x}'
|
|
56
|
+
>>> _apply_symbol_style("foo", SymbolStyle.OPERATORNAME)
|
|
57
|
+
'\\\\operatorname{foo}'
|
|
58
|
+
"""
|
|
59
|
+
if style == SymbolStyle.ITALIC:
|
|
60
|
+
return name
|
|
61
|
+
elif style == SymbolStyle.OPERATORNAME:
|
|
62
|
+
return f"\\operatorname{{{name}}}"
|
|
63
|
+
elif style == SymbolStyle.TEXT:
|
|
64
|
+
return f"\\text{{{name}}}"
|
|
65
|
+
elif style == SymbolStyle.BOLD:
|
|
66
|
+
return f"\\boldsymbol{{{name}}}"
|
|
67
|
+
return name
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _func2katex(name: str, style: SymbolStyle | None) -> str:
|
|
71
|
+
"""Convert a function name to its KaTeX representation.
|
|
72
|
+
|
|
73
|
+
Resolution order:
|
|
74
|
+
1. If *style* is explicitly provided, delegate to :func:`_apply_symbol_style`.
|
|
75
|
+
2. If *name* is in :data:`KATEX_NATIVE`, render as ``\\name``.
|
|
76
|
+
3. Otherwise fall back to ``\\operatorname{name}``.
|
|
77
|
+
|
|
78
|
+
``sqrt`` is handled separately in :func:`_convert` and should not be
|
|
79
|
+
passed here.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
name: The function name as it appears in the source expression.
|
|
83
|
+
style: An explicit ``SymbolStyle`` override, or ``None`` to use the
|
|
84
|
+
default resolution order.
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
A KaTeX string for the function name only (without arguments).
|
|
88
|
+
|
|
89
|
+
Examples:
|
|
90
|
+
>>> _func2katex("sin", None)
|
|
91
|
+
'\\\\sin'
|
|
92
|
+
>>> _func2katex("foo", None)
|
|
93
|
+
'\\\\operatorname{foo}'
|
|
94
|
+
>>> _func2katex("foo", SymbolStyle.TEXT)
|
|
95
|
+
'\\\\text{foo}'
|
|
96
|
+
"""
|
|
97
|
+
if style is not None:
|
|
98
|
+
return _apply_symbol_style(name, style)
|
|
99
|
+
if name in KATEX_NATIVE:
|
|
100
|
+
return f"\\{name}"
|
|
101
|
+
return f"\\operatorname{{{name}}}"
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _convert(
|
|
105
|
+
node: ast.expr,
|
|
106
|
+
parent_op: type | None,
|
|
107
|
+
div_style: Literal["frac", "div"],
|
|
108
|
+
symbol_styles: dict[str, SymbolStyle] | None,
|
|
109
|
+
) -> str:
|
|
110
|
+
"""Recursively convert an AST node to a KaTeX string.
|
|
111
|
+
|
|
112
|
+
Traversal is depth-first; leaf nodes (``Constant``, ``Name``) return
|
|
113
|
+
immediately, while compound nodes (``BinOp``, ``UnaryOp``, ``Call``)
|
|
114
|
+
recurse into their children before assembling the result.
|
|
115
|
+
|
|
116
|
+
Parentheses are inserted around a ``BinOp`` child whenever its precedence
|
|
117
|
+
is strictly lower than the *parent_op* precedence, reconstructing the
|
|
118
|
+
grouping that the Python parser encoded structurally in the AST.
|
|
119
|
+
|
|
120
|
+
Args:
|
|
121
|
+
node: The current AST node to convert.
|
|
122
|
+
parent_op: The ``type`` of the enclosing ``BinOp`` operator (e.g.
|
|
123
|
+
``ast.Mult``), used for precedence-based parenthesisation.
|
|
124
|
+
Pass ``None`` at the root or when precedence context is irrelevant.
|
|
125
|
+
div_style: Controls division rendering — ``"frac"`` for
|
|
126
|
+
``\\frac{l}{r}``, ``"div"`` for ``l \\div r``.
|
|
127
|
+
symbol_styles: Optional name-to-style mapping forwarded from
|
|
128
|
+
:func:`expr2katex`.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
A KaTeX string representing *node*.
|
|
132
|
+
"""
|
|
133
|
+
# Leaf: numeric or string literal
|
|
134
|
+
if isinstance(node, ast.Constant):
|
|
135
|
+
return str(node.value)
|
|
136
|
+
|
|
137
|
+
# Leaf: variable name
|
|
138
|
+
if isinstance(node, ast.Name):
|
|
139
|
+
style = symbol_styles.get(node.id) if symbol_styles else None
|
|
140
|
+
if style is None:
|
|
141
|
+
style = SymbolStyle.ITALIC
|
|
142
|
+
return _apply_symbol_style(node.id, style)
|
|
143
|
+
|
|
144
|
+
# Unary operators: negate or identity
|
|
145
|
+
if isinstance(node, ast.UnaryOp):
|
|
146
|
+
operand = _convert(node.operand, None, div_style, symbol_styles)
|
|
147
|
+
# Wrap BinOp operands in parentheses so that -(a+b) does not render as -a+b.
|
|
148
|
+
if isinstance(node.operand, ast.BinOp):
|
|
149
|
+
operand = f"\\left({operand}\\right)"
|
|
150
|
+
if isinstance(node.op, ast.USub):
|
|
151
|
+
return f"-{operand}"
|
|
152
|
+
elif isinstance(node.op, ast.UAdd):
|
|
153
|
+
# Unary plus carries no mathematical meaning; omit it.
|
|
154
|
+
return operand
|
|
155
|
+
return operand
|
|
156
|
+
|
|
157
|
+
# Binary operators
|
|
158
|
+
if isinstance(node, ast.BinOp):
|
|
159
|
+
op_type = type(node.op)
|
|
160
|
+
|
|
161
|
+
# Division has two distinct visual forms controlled by div_style.
|
|
162
|
+
if isinstance(node.op, ast.Div):
|
|
163
|
+
left = _convert(node.left, op_type, div_style, symbol_styles)
|
|
164
|
+
right = _convert(node.right, op_type, div_style, symbol_styles)
|
|
165
|
+
if div_style == "frac":
|
|
166
|
+
return f"\\frac{{{left}}}{{{right}}}"
|
|
167
|
+
else:
|
|
168
|
+
return f"{left} \\div {right}"
|
|
169
|
+
|
|
170
|
+
# Exponentiation uses superscript notation.
|
|
171
|
+
if isinstance(node.op, ast.Pow):
|
|
172
|
+
left = _convert(node.left, op_type, div_style, symbol_styles)
|
|
173
|
+
right = _convert(node.right, op_type, div_style, symbol_styles)
|
|
174
|
+
return f"{left}^{{{right}}}"
|
|
175
|
+
|
|
176
|
+
left = _convert(node.left, op_type, div_style, symbol_styles)
|
|
177
|
+
right = _convert(node.right, op_type, div_style, symbol_styles)
|
|
178
|
+
|
|
179
|
+
if isinstance(node.op, ast.Add):
|
|
180
|
+
result = f"{left}+{right}"
|
|
181
|
+
elif isinstance(node.op, ast.Sub):
|
|
182
|
+
result = f"{left}-{right}"
|
|
183
|
+
elif isinstance(node.op, ast.Mult):
|
|
184
|
+
result = f"{left}\\times {right}"
|
|
185
|
+
elif isinstance(node.op, ast.FloorDiv):
|
|
186
|
+
result = f"\\lfloor\\frac{{{left}}}{{{right}}}\\rfloor"
|
|
187
|
+
elif isinstance(node.op, ast.Mod):
|
|
188
|
+
result = f"{left}\\bmod {right}"
|
|
189
|
+
else:
|
|
190
|
+
result = f"{left}?{right}"
|
|
191
|
+
|
|
192
|
+
# Wrap in parentheses when this node has lower precedence than its
|
|
193
|
+
# parent, preserving the grouping encoded in the AST structure.
|
|
194
|
+
if (
|
|
195
|
+
parent_op is not None
|
|
196
|
+
and op_type in PRECEDENCE
|
|
197
|
+
and parent_op in PRECEDENCE
|
|
198
|
+
and PRECEDENCE[op_type] < PRECEDENCE[parent_op]
|
|
199
|
+
):
|
|
200
|
+
return f"\\left({result}\\right)"
|
|
201
|
+
|
|
202
|
+
return result
|
|
203
|
+
|
|
204
|
+
# Function call
|
|
205
|
+
if isinstance(node, ast.Call):
|
|
206
|
+
func_name = node.func.id if isinstance(node.func, ast.Name) else str(node.func)
|
|
207
|
+
style = symbol_styles.get(func_name) if symbol_styles else None
|
|
208
|
+
katex_func = _func2katex(func_name, style)
|
|
209
|
+
|
|
210
|
+
args = [_convert(a, None, div_style, symbol_styles) for a in node.args]
|
|
211
|
+
|
|
212
|
+
# sqrt has a dedicated KaTeX template; handle before the generic path.
|
|
213
|
+
if func_name == "sqrt":
|
|
214
|
+
if len(args) == 1:
|
|
215
|
+
return f"\\sqrt{{{args[0]}}}"
|
|
216
|
+
elif len(args) == 2:
|
|
217
|
+
# sqrt(x, n) renders as the n-th root of x.
|
|
218
|
+
return f"\\sqrt[{args[1]}]{{{args[0]}}}"
|
|
219
|
+
|
|
220
|
+
return f"{katex_func}\\left({', '.join(args)}\\right)"
|
|
221
|
+
|
|
222
|
+
return ""
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _highlight_error(
|
|
226
|
+
expr: str,
|
|
227
|
+
offset: int,
|
|
228
|
+
error_style: Literal["underbrace", "underline"],
|
|
229
|
+
) -> str:
|
|
230
|
+
"""Build a KaTeX string that highlights the offending character in *expr*.
|
|
231
|
+
|
|
232
|
+
The character at *offset* (1-based, as reported by ``SyntaxError.offset``)
|
|
233
|
+
is wrapped in ``\\textcolor{red}`` combined with the decoration chosen by
|
|
234
|
+
*error_style*. The surrounding text is wrapped in ``\\text{}`` so that
|
|
235
|
+
spaces and special characters are rendered literally.
|
|
236
|
+
|
|
237
|
+
Args:
|
|
238
|
+
expr: The original expression string that caused the ``SyntaxError``.
|
|
239
|
+
offset: 1-based character index of the offending token, taken directly
|
|
240
|
+
from ``SyntaxError.offset``.
|
|
241
|
+
error_style: ``"underbrace"`` wraps the token in
|
|
242
|
+
``\\textcolor{red}{\\underbrace{...}}``;
|
|
243
|
+
``"underline"`` uses ``\\textcolor{red}{\\underline{...}}``.
|
|
244
|
+
|
|
245
|
+
Returns:
|
|
246
|
+
A KaTeX string with the error position highlighted in red.
|
|
247
|
+
|
|
248
|
+
Examples:
|
|
249
|
+
>>> _highlight_error("a+(b", 3, "underbrace")
|
|
250
|
+
'\\\\text{a+}\\\\textcolor{red}{\\\\underbrace{\\\\text{(}}}\\\\text{b}'
|
|
251
|
+
"""
|
|
252
|
+
# offset is 1-based; convert to 0-based index.
|
|
253
|
+
idx = offset - 1
|
|
254
|
+
before = expr[:idx]
|
|
255
|
+
error_char = expr[idx] if idx < len(expr) else ""
|
|
256
|
+
after = expr[idx + 1:] if idx + 1 < len(expr) else ""
|
|
257
|
+
|
|
258
|
+
def escape(s: str) -> str:
|
|
259
|
+
"""Wrap a raw string in \\text{} so KaTeX renders it literally."""
|
|
260
|
+
return f"\\text{{{s}}}" if s else ""
|
|
261
|
+
|
|
262
|
+
if error_style == "underbrace":
|
|
263
|
+
highlighted = f"\\textcolor{{red}}{{\\underbrace{{{escape(error_char)}}}}}" if error_char else ""
|
|
264
|
+
else:
|
|
265
|
+
highlighted = f"\\textcolor{{red}}{{\\underline{{{escape(error_char)}}}}}" if error_char else ""
|
|
266
|
+
|
|
267
|
+
return f"\\text{{{before}}}{highlighted}\\text{{{after}}}"
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def expr2katex(
|
|
271
|
+
expr: str,
|
|
272
|
+
raise_on_error: bool = False,
|
|
273
|
+
div_style: Literal["frac", "div"] = "frac",
|
|
274
|
+
symbol_styles: dict[str, SymbolStyle] | None = None,
|
|
275
|
+
error_style: Literal["underbrace", "underline"] = "underbrace",
|
|
276
|
+
) -> tuple[str, str | None]:
|
|
277
|
+
"""Convert a Python expression string to a KaTeX string.
|
|
278
|
+
|
|
279
|
+
Parses *expr* into an AST and recursively converts each node into its
|
|
280
|
+
KaTeX representation via :func:`_convert`. If a ``SyntaxError`` is
|
|
281
|
+
detected, the offending token is highlighted in red and the error message
|
|
282
|
+
is returned as the second element of the tuple.
|
|
283
|
+
|
|
284
|
+
Args:
|
|
285
|
+
expr:
|
|
286
|
+
A Python expression string to convert (e.g. ``"sin(a) + b * c"``).
|
|
287
|
+
raise_on_error:
|
|
288
|
+
If ``True``, re-raises ``SyntaxError`` instead of returning a
|
|
289
|
+
highlighted KaTeX string. Other exceptions are always re-raised
|
|
290
|
+
regardless of this flag.
|
|
291
|
+
div_style:
|
|
292
|
+
Controls how the ``/`` operator is rendered.
|
|
293
|
+
``"frac"`` (default) produces ``\\frac{l}{r}``.
|
|
294
|
+
``"div"`` produces ``l \\div r``.
|
|
295
|
+
symbol_styles:
|
|
296
|
+
Optional mapping from a symbol name to a ``SymbolStyle``, applied
|
|
297
|
+
to both variables (``ast.Name``) and function names (``ast.Call``).
|
|
298
|
+
Variables default to ``ITALIC``; functions default to
|
|
299
|
+
``OPERATORNAME``. KaTeX-native functions (e.g. ``sin``, ``sqrt``)
|
|
300
|
+
always use their native command and ignore this mapping unless
|
|
301
|
+
explicitly overridden.
|
|
302
|
+
error_style:
|
|
303
|
+
Controls how the offending token is decorated on ``SyntaxError``.
|
|
304
|
+
``"underbrace"`` (default) uses
|
|
305
|
+
``\\textcolor{red}{\\underbrace{...}}``.
|
|
306
|
+
``"underline"`` uses ``\\textcolor{red}{\\underline{...}}``.
|
|
307
|
+
Ignored when ``raise_on_error=True``.
|
|
308
|
+
|
|
309
|
+
Returns:
|
|
310
|
+
A tuple ``(katex, err)`` where:
|
|
311
|
+
|
|
312
|
+
- ``katex`` — the KaTeX string. Always present, even on
|
|
313
|
+
``SyntaxError`` (unless ``raise_on_error=True``). The error token
|
|
314
|
+
is highlighted according to *error_style*.
|
|
315
|
+
- ``err`` — ``None`` on success, or ``SyntaxError.msg`` on failure.
|
|
316
|
+
|
|
317
|
+
Raises:
|
|
318
|
+
SyntaxError: if ``raise_on_error=True`` and *expr* is syntactically
|
|
319
|
+
invalid.
|
|
320
|
+
Exception: any exception raised during AST traversal is always
|
|
321
|
+
propagated, regardless of ``raise_on_error``.
|
|
322
|
+
|
|
323
|
+
Examples:
|
|
324
|
+
>>> expr2katex("sin(a) + b * c")
|
|
325
|
+
('\\\\sin\\\\left(a\\\\right)+b\\\\times c', None)
|
|
326
|
+
|
|
327
|
+
>>> expr2katex("a / b", div_style="div")
|
|
328
|
+
('a \\\\div b', None)
|
|
329
|
+
|
|
330
|
+
>>> expr2katex("sin(a+(c+3)*b")
|
|
331
|
+
('\\\\text{sin(a+}\\\\textcolor{red}{\\\\underbrace{\\\\text{(}}}\\\\text{c+3*b}', "'(' was never closed")
|
|
332
|
+
|
|
333
|
+
>>> expr2katex("sin(a+(c+3)*b", raise_on_error=True)
|
|
334
|
+
SyntaxError: '(' was never closed
|
|
335
|
+
"""
|
|
336
|
+
try:
|
|
337
|
+
tree = ast.parse(expr, mode="eval")
|
|
338
|
+
except SyntaxError as e:
|
|
339
|
+
if raise_on_error:
|
|
340
|
+
raise
|
|
341
|
+
katex = _highlight_error(expr, e.offset or 0, error_style)
|
|
342
|
+
return (katex, e.msg)
|
|
343
|
+
|
|
344
|
+
katex = _convert(tree.body, None, div_style, symbol_styles)
|
|
345
|
+
return (katex, None)
|