expr2katex 0.1.0__tar.gz

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.
@@ -0,0 +1,8 @@
1
+ .vscode/
2
+ .venv/
3
+ __pycache__/
4
+ *.pyc
5
+ *.pyo
6
+ dist/
7
+ *.egg-info/
8
+ .hatch/
@@ -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.
@@ -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,145 @@
1
+ # expr2katex
2
+
3
+ Convert a Python expression string to a [KaTeX](https://katex.org/) string.
4
+
5
+ ```python
6
+ from expr2katex import expr2katex
7
+
8
+ katex, err = expr2katex("sin(a) + (c + 3) * b")
9
+ # katex → \sin\left(a\right)+\left(c+3\right)\times b
10
+ # err → None
11
+ ```
12
+
13
+ ## Features
14
+
15
+ - **Zero dependencies** — built on Python's standard `ast` module only
16
+ - **Precedence-aware parenthesisation** — reconstructs grouping from the AST structure, so `(a + b) * c` renders correctly without any string heuristics
17
+ - **KaTeX-native functions** — `sin`, `cos`, `sqrt`, `log`, etc. render with their native KaTeX commands (`\sin`, `\sqrt{x}`, …)
18
+ - **Unknown functions** fall back to `\operatorname{foo}` automatically
19
+ - **Configurable division style** — `\frac{a}{b}` or `a \div b`
20
+ - **Symbol style overrides** — control how variables and function names are rendered per symbol
21
+ - **SyntaxError highlighting** — invalid expressions return a KaTeX string with the offending token highlighted in red, designed for real-time UI previews
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ pip install expr2katex
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ### Basic
32
+
33
+ ```python
34
+ from expr2katex import expr2katex
35
+
36
+ katex, err = expr2katex("sin(a) + b * c")
37
+ # \sin\left(a\right)+b\times c
38
+ ```
39
+
40
+ ### Division style
41
+
42
+ ```python
43
+ expr2katex("a / b") # \frac{a}{b} (default)
44
+ expr2katex("a / b", div_style="div") # a \div b
45
+ ```
46
+
47
+ ### Power and roots
48
+
49
+ ```python
50
+ expr2katex("a ** 2") # a^{2}
51
+ expr2katex("sqrt(x)") # \sqrt{x}
52
+ expr2katex("sqrt(x, 3)") # \sqrt[3]{x} (n-th root)
53
+ ```
54
+
55
+ ### Symbol style overrides
56
+
57
+ ```python
58
+ from expr2katex import expr2katex, SymbolStyle
59
+
60
+ expr2katex("foo(x)", symbol_styles={
61
+ "foo": SymbolStyle.TEXT, # \text{foo}\left(x\right)
62
+ "x": SymbolStyle.BOLD, # \boldsymbol{x}
63
+ })
64
+ ```
65
+
66
+ | `SymbolStyle` | Output | Default for |
67
+ |-----------------|-------------------------|--------------|
68
+ | `ITALIC` | `x` | variables |
69
+ | `OPERATORNAME` | `\operatorname{foo}` | functions |
70
+ | `TEXT` | `\text{foo}` | |
71
+ | `BOLD` | `\boldsymbol{x}` | |
72
+
73
+ ### SyntaxError highlighting
74
+
75
+ When the expression is invalid, `expr2katex` returns a KaTeX string with the offending token highlighted in red — useful for real-time formula editors.
76
+
77
+ ```python
78
+ katex, err = expr2katex("sin(a + (b * c")
79
+ # katex → \text{sin(a + }\textcolor{red}{\underbrace{\text{(}}}\text{b * c}
80
+ # err → '(' was never closed
81
+ ```
82
+
83
+ Choose between `underbrace` (default) and `underline`:
84
+
85
+ ```python
86
+ expr2katex("sin(a + (b * c", error_style="underline")
87
+ ```
88
+
89
+ ### Raising on error
90
+
91
+ ```python
92
+ # SyntaxError is re-raised instead of being returned
93
+ expr2katex("sin(a + (b * c", raise_on_error=True)
94
+ # SyntaxError: '(' was never closed
95
+
96
+ # All non-SyntaxError exceptions are always raised regardless of raise_on_error.
97
+ ```
98
+
99
+ ## API reference
100
+
101
+ ```python
102
+ def expr2katex(
103
+ expr: str,
104
+ raise_on_error: bool = False,
105
+ div_style: Literal["frac", "div"] = "frac",
106
+ symbol_styles: dict[str, SymbolStyle] | None = None,
107
+ error_style: Literal["underbrace", "underline"] = "underbrace",
108
+ ) -> tuple[str, str | None]: ...
109
+ ```
110
+
111
+ | Parameter | Type | Default | Description |
112
+ |-----------------|-----------------------------------|---------------|-------------|
113
+ | `expr` | `str` | — | Python expression string to convert |
114
+ | `raise_on_error`| `bool` | `False` | Re-raise `SyntaxError` instead of returning highlighted KaTeX |
115
+ | `div_style` | `"frac"` \| `"div"` | `"frac"` | Division rendering style |
116
+ | `symbol_styles` | `dict[str, SymbolStyle] \| None` | `None` | Per-symbol style overrides |
117
+ | `error_style` | `"underbrace"` \| `"underline"` | `"underbrace"`| Error highlight decoration |
118
+
119
+ **Returns** `(katex: str, err: str | None)` — `err` is `None` on success, or `SyntaxError.msg` on failure.
120
+
121
+ ## Supported operators
122
+
123
+ | Python | KaTeX output |
124
+ |---------|-----------------------|
125
+ | `+` | `a+b` |
126
+ | `-` | `a-b` |
127
+ | `*` | `a\times b` |
128
+ | `/` | `\frac{a}{b}` |
129
+ | `//` | `\lfloor\frac{a}{b}\rfloor` |
130
+ | `%` | `a\bmod b` |
131
+ | `**` | `a^{b}` |
132
+ | `-x` | `-x` |
133
+ | `+x` | `x` (omitted) |
134
+
135
+ ## KaTeX-native functions
136
+
137
+ The following function names are rendered with their native KaTeX command (`\sin`, `\cos`, …). All other names fall back to `\operatorname{name}`.
138
+
139
+ `sin` `cos` `tan` `arcsin` `arccos` `arctan` `sinh` `cosh` `tanh` `log` `ln` `exp` `min` `max` `gcd` `lcm` `det` `dim` `sqrt`
140
+
141
+ `sqrt` uses the dedicated `\sqrt{x}` template. Pass a second argument for n-th roots: `sqrt(x, n)` → `\sqrt[n]{x}`.
142
+
143
+ ## License
144
+
145
+ MIT
@@ -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)
@@ -0,0 +1,29 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "expr2katex"
7
+ version = "0.1.0"
8
+ description = "Convert a Python expression string to a KaTeX string"
9
+ readme = "README.md"
10
+ license = { text = "MIT" }
11
+ requires-python = ">=3.10"
12
+ keywords = ["katex", "latex", "math", "expression", "ast"]
13
+ classifiers = [
14
+ "Development Status :: 3 - Alpha",
15
+ "Intended Audience :: Developers",
16
+ "License :: OSI Approved :: MIT License",
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3.10",
19
+ "Programming Language :: Python :: 3.11",
20
+ "Programming Language :: Python :: 3.12",
21
+ "Programming Language :: Python :: 3.13",
22
+ "Topic :: Text Processing :: Markup :: LaTeX",
23
+ "Topic :: Software Development :: Libraries :: Python Modules",
24
+ ]
25
+
26
+ [project.urls]
27
+ Homepage = "https://github.com/YOUR_USERNAME/expr2katex"
28
+ Repository = "https://github.com/YOUR_USERNAME/expr2katex"
29
+ Issues = "https://github.com/YOUR_USERNAME/expr2katex/issues"
@@ -0,0 +1,145 @@
1
+ # expr2katex
2
+
3
+ Convert a Python expression string to a [KaTeX](https://katex.org/) string.
4
+
5
+ ```python
6
+ from expr2katex import expr2katex
7
+
8
+ katex, err = expr2katex("sin(a) + (c + 3) * b")
9
+ # katex → \sin\left(a\right)+\left(c+3\right)\times b
10
+ # err → None
11
+ ```
12
+
13
+ ## Features
14
+
15
+ - **Zero dependencies** — built on Python's standard `ast` module only
16
+ - **Precedence-aware parenthesisation** — reconstructs grouping from the AST structure, so `(a + b) * c` renders correctly without any string heuristics
17
+ - **KaTeX-native functions** — `sin`, `cos`, `sqrt`, `log`, etc. render with their native KaTeX commands (`\sin`, `\sqrt{x}`, …)
18
+ - **Unknown functions** fall back to `\operatorname{foo}` automatically
19
+ - **Configurable division style** — `\frac{a}{b}` or `a \div b`
20
+ - **Symbol style overrides** — control how variables and function names are rendered per symbol
21
+ - **SyntaxError highlighting** — invalid expressions return a KaTeX string with the offending token highlighted in red, designed for real-time UI previews
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ pip install expr2katex
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ### Basic
32
+
33
+ ```python
34
+ from expr2katex import expr2katex
35
+
36
+ katex, err = expr2katex("sin(a) + b * c")
37
+ # \sin\left(a\right)+b\times c
38
+ ```
39
+
40
+ ### Division style
41
+
42
+ ```python
43
+ expr2katex("a / b") # \frac{a}{b} (default)
44
+ expr2katex("a / b", div_style="div") # a \div b
45
+ ```
46
+
47
+ ### Power and roots
48
+
49
+ ```python
50
+ expr2katex("a ** 2") # a^{2}
51
+ expr2katex("sqrt(x)") # \sqrt{x}
52
+ expr2katex("sqrt(x, 3)") # \sqrt[3]{x} (n-th root)
53
+ ```
54
+
55
+ ### Symbol style overrides
56
+
57
+ ```python
58
+ from expr2katex import expr2katex, SymbolStyle
59
+
60
+ expr2katex("foo(x)", symbol_styles={
61
+ "foo": SymbolStyle.TEXT, # \text{foo}\left(x\right)
62
+ "x": SymbolStyle.BOLD, # \boldsymbol{x}
63
+ })
64
+ ```
65
+
66
+ | `SymbolStyle` | Output | Default for |
67
+ |-----------------|-------------------------|--------------|
68
+ | `ITALIC` | `x` | variables |
69
+ | `OPERATORNAME` | `\operatorname{foo}` | functions |
70
+ | `TEXT` | `\text{foo}` | |
71
+ | `BOLD` | `\boldsymbol{x}` | |
72
+
73
+ ### SyntaxError highlighting
74
+
75
+ When the expression is invalid, `expr2katex` returns a KaTeX string with the offending token highlighted in red — useful for real-time formula editors.
76
+
77
+ ```python
78
+ katex, err = expr2katex("sin(a + (b * c")
79
+ # katex → \text{sin(a + }\textcolor{red}{\underbrace{\text{(}}}\text{b * c}
80
+ # err → '(' was never closed
81
+ ```
82
+
83
+ Choose between `underbrace` (default) and `underline`:
84
+
85
+ ```python
86
+ expr2katex("sin(a + (b * c", error_style="underline")
87
+ ```
88
+
89
+ ### Raising on error
90
+
91
+ ```python
92
+ # SyntaxError is re-raised instead of being returned
93
+ expr2katex("sin(a + (b * c", raise_on_error=True)
94
+ # SyntaxError: '(' was never closed
95
+
96
+ # All non-SyntaxError exceptions are always raised regardless of raise_on_error.
97
+ ```
98
+
99
+ ## API reference
100
+
101
+ ```python
102
+ def expr2katex(
103
+ expr: str,
104
+ raise_on_error: bool = False,
105
+ div_style: Literal["frac", "div"] = "frac",
106
+ symbol_styles: dict[str, SymbolStyle] | None = None,
107
+ error_style: Literal["underbrace", "underline"] = "underbrace",
108
+ ) -> tuple[str, str | None]: ...
109
+ ```
110
+
111
+ | Parameter | Type | Default | Description |
112
+ |-----------------|-----------------------------------|---------------|-------------|
113
+ | `expr` | `str` | — | Python expression string to convert |
114
+ | `raise_on_error`| `bool` | `False` | Re-raise `SyntaxError` instead of returning highlighted KaTeX |
115
+ | `div_style` | `"frac"` \| `"div"` | `"frac"` | Division rendering style |
116
+ | `symbol_styles` | `dict[str, SymbolStyle] \| None` | `None` | Per-symbol style overrides |
117
+ | `error_style` | `"underbrace"` \| `"underline"` | `"underbrace"`| Error highlight decoration |
118
+
119
+ **Returns** `(katex: str, err: str | None)` — `err` is `None` on success, or `SyntaxError.msg` on failure.
120
+
121
+ ## Supported operators
122
+
123
+ | Python | KaTeX output |
124
+ |---------|-----------------------|
125
+ | `+` | `a+b` |
126
+ | `-` | `a-b` |
127
+ | `*` | `a\times b` |
128
+ | `/` | `\frac{a}{b}` |
129
+ | `//` | `\lfloor\frac{a}{b}\rfloor` |
130
+ | `%` | `a\bmod b` |
131
+ | `**` | `a^{b}` |
132
+ | `-x` | `-x` |
133
+ | `+x` | `x` (omitted) |
134
+
135
+ ## KaTeX-native functions
136
+
137
+ The following function names are rendered with their native KaTeX command (`\sin`, `\cos`, …). All other names fall back to `\operatorname{name}`.
138
+
139
+ `sin` `cos` `tan` `arcsin` `arccos` `arctan` `sinh` `cosh` `tanh` `log` `ln` `exp` `min` `max` `gcd` `lcm` `det` `dim` `sqrt`
140
+
141
+ `sqrt` uses the dedicated `\sqrt{x}` template. Pass a second argument for n-th roots: `sqrt(x, n)` → `\sqrt[n]{x}`.
142
+
143
+ ## License
144
+
145
+ MIT
@@ -0,0 +1,231 @@
1
+ import pytest
2
+ from expr2katex import expr2katex, SymbolStyle
3
+
4
+
5
+ # ---------------------------------------------------------------------------
6
+ # Helpers
7
+ # ---------------------------------------------------------------------------
8
+
9
+ def katex(expr, **kwargs):
10
+ """Return only the KaTeX string, ignoring the error field."""
11
+ result, _ = expr2katex(expr, **kwargs)
12
+ return result
13
+
14
+
15
+ def err(expr, **kwargs):
16
+ """Return only the error field."""
17
+ _, e = expr2katex(expr, **kwargs)
18
+ return e
19
+
20
+
21
+ # ---------------------------------------------------------------------------
22
+ # Constants and variables
23
+ # ---------------------------------------------------------------------------
24
+
25
+ class TestLeafNodes:
26
+ def test_integer_constant(self):
27
+ assert katex("1") == "1"
28
+
29
+ def test_float_constant(self):
30
+ assert katex("3.14") == "3.14"
31
+
32
+ def test_variable_default_italic(self):
33
+ # Variables render as plain names by default (italic in KaTeX).
34
+ assert katex("x") == "x"
35
+
36
+ def test_variable_text_style(self):
37
+ assert katex("x", symbol_styles={"x": SymbolStyle.TEXT}) == "\\text{x}"
38
+
39
+ def test_variable_bold_style(self):
40
+ assert katex("x", symbol_styles={"x": SymbolStyle.BOLD}) == "\\boldsymbol{x}"
41
+
42
+ def test_variable_operatorname_style(self):
43
+ assert katex("x", symbol_styles={"x": SymbolStyle.OPERATORNAME}) == "\\operatorname{x}"
44
+
45
+
46
+ # ---------------------------------------------------------------------------
47
+ # Unary operators
48
+ # ---------------------------------------------------------------------------
49
+
50
+ class TestUnaryOps:
51
+ def test_unary_minus(self):
52
+ assert katex("-a") == "-a"
53
+
54
+ def test_unary_minus_complex(self):
55
+ assert katex("-(a+b)") == "-\\left(a+b\\right)"
56
+
57
+ def test_unary_plus_omitted(self):
58
+ # Unary plus carries no mathematical meaning and is omitted.
59
+ assert katex("+a") == "a"
60
+
61
+
62
+ # ---------------------------------------------------------------------------
63
+ # Binary operators
64
+ # ---------------------------------------------------------------------------
65
+
66
+ class TestBinOps:
67
+ def test_addition(self):
68
+ assert katex("a + b") == "a+b"
69
+
70
+ def test_subtraction(self):
71
+ assert katex("a - b") == "a-b"
72
+
73
+ def test_multiplication(self):
74
+ assert katex("a * b") == "a\\times b"
75
+
76
+ def test_floor_division(self):
77
+ assert katex("a // b") == "\\lfloor\\frac{a}{b}\\rfloor"
78
+
79
+ def test_modulo(self):
80
+ assert katex("a % b") == "a\\bmod b"
81
+
82
+ def test_power(self):
83
+ assert katex("a ** 2") == "a^{2}"
84
+
85
+ def test_power_expression_exponent(self):
86
+ assert katex("a ** (b + 1)") == "a^{\\left(b+1\\right)}"
87
+
88
+
89
+ # ---------------------------------------------------------------------------
90
+ # Division styles
91
+ # ---------------------------------------------------------------------------
92
+
93
+ class TestDivStyle:
94
+ def test_div_frac_default(self):
95
+ assert katex("a / b") == "\\frac{a}{b}"
96
+
97
+ def test_div_frac_explicit(self):
98
+ assert katex("a / b", div_style="frac") == "\\frac{a}{b}"
99
+
100
+ def test_div_symbol(self):
101
+ assert katex("a / b", div_style="div") == "a \\div b"
102
+
103
+
104
+ # ---------------------------------------------------------------------------
105
+ # Parentheses (precedence reconstruction)
106
+ # ---------------------------------------------------------------------------
107
+
108
+ class TestPrecedence:
109
+ def test_no_parens_same_precedence(self):
110
+ assert katex("a + b + c") == "a+b+c"
111
+
112
+ def test_parens_added_for_lower_precedence(self):
113
+ # (a + b) * c — the addition node is wrapped in parens.
114
+ assert katex("(a + b) * c") == "\\left(a+b\\right)\\times c"
115
+
116
+ def test_no_parens_higher_precedence_child(self):
117
+ # a + b * c — multiplication is higher precedence, no parens needed.
118
+ assert katex("a + b * c") == "a+b\\times c"
119
+
120
+ def test_nested_parens(self):
121
+ assert katex("(a + b) * (c + d)") == "\\left(a+b\\right)\\times \\left(c+d\\right)"
122
+
123
+ def test_parens_in_function_arg(self):
124
+ assert katex("sin((a + b) * c)") == "\\sin\\left(\\left(a+b\\right)\\times c\\right)"
125
+
126
+
127
+ # ---------------------------------------------------------------------------
128
+ # Functions
129
+ # ---------------------------------------------------------------------------
130
+
131
+ class TestFunctions:
132
+ def test_katex_native_sin(self):
133
+ assert katex("sin(a)") == "\\sin\\left(a\\right)"
134
+
135
+ def test_katex_native_cos(self):
136
+ assert katex("cos(a)") == "\\cos\\left(a\\right)"
137
+
138
+ def test_katex_native_log(self):
139
+ assert katex("log(a)") == "\\log\\left(a\\right)"
140
+
141
+ def test_katex_native_ln(self):
142
+ assert katex("ln(a)") == "\\ln\\left(a\\right)"
143
+
144
+ def test_katex_native_exp(self):
145
+ assert katex("exp(a)") == "\\exp\\left(a\\right)"
146
+
147
+ def test_sqrt_single_arg(self):
148
+ assert katex("sqrt(x)") == "\\sqrt{x}"
149
+
150
+ def test_sqrt_nth_root(self):
151
+ assert katex("sqrt(x, 3)") == "\\sqrt[3]{x}"
152
+
153
+ def test_unknown_function_operatorname(self):
154
+ assert katex("foo(a)") == "\\operatorname{foo}\\left(a\\right)"
155
+
156
+ def test_function_style_override(self):
157
+ assert katex("sin(a)", symbol_styles={"sin": SymbolStyle.TEXT}) == "\\text{sin}\\left(a\\right)"
158
+
159
+ def test_nested_function_calls(self):
160
+ assert katex("sin(cos(x))") == "\\sin\\left(\\cos\\left(x\\right)\\right)"
161
+
162
+ def test_function_with_expression_arg(self):
163
+ assert katex("sin(a + 1)") == "\\sin\\left(a+1\\right)"
164
+
165
+ def test_function_multiple_args(self):
166
+ assert katex("foo(a, b)") == "\\operatorname{foo}\\left(a, b\\right)"
167
+
168
+ def test_nested_function_with_binop(self):
169
+ assert katex("sin(cos(x) + 1)") == "\\sin\\left(\\cos\\left(x\\right)+1\\right)"
170
+
171
+
172
+ # ---------------------------------------------------------------------------
173
+ # Compound expressions
174
+ # ---------------------------------------------------------------------------
175
+
176
+ class TestCompoundExpressions:
177
+ def test_sin_plus_product(self):
178
+ assert katex("sin(a) + b * c") == "\\sin\\left(a\\right)+b\\times c"
179
+
180
+ def test_power_sum(self):
181
+ assert katex("a ** 2 + b ** 2") == "a^{2}+b^{2}"
182
+
183
+ def test_frac_plus_var(self):
184
+ assert katex("a / b + c") == "\\frac{a}{b}+c"
185
+
186
+ def test_unary_minus_in_expression(self):
187
+ assert katex("-a + b * c") == "-a+b\\times c"
188
+
189
+
190
+ # ---------------------------------------------------------------------------
191
+ # SyntaxError handling
192
+ # ---------------------------------------------------------------------------
193
+
194
+ class TestSyntaxError:
195
+ def test_unclosed_paren_returns_err_msg(self):
196
+ _, e = expr2katex("sin(a+(c+3*b")
197
+ assert e is not None
198
+ assert "was never closed" in e
199
+
200
+ def test_unclosed_paren_katex_contains_highlight(self):
201
+ result, _ = expr2katex("sin(a+(c+3*b")
202
+ assert "\\textcolor{red}" in result
203
+ assert "\\underbrace" in result
204
+
205
+ def test_error_style_underline(self):
206
+ result, _ = expr2katex("sin(a+(c+3*b", error_style="underline")
207
+ assert "\\underline" in result
208
+ assert "\\underbrace" not in result
209
+
210
+ def test_error_style_underbrace_default(self):
211
+ result, _ = expr2katex("sin(a+(c+3*b", error_style="underbrace")
212
+ assert "\\underbrace" in result
213
+
214
+ def test_raise_on_error_true(self):
215
+ with pytest.raises(SyntaxError):
216
+ expr2katex("sin(a+(c+3*b", raise_on_error=True)
217
+
218
+ def test_raise_on_error_false_no_exception(self):
219
+ # Should not raise; error is returned as second tuple element.
220
+ result, e = expr2katex("sin(a+(c+3*b", raise_on_error=False)
221
+ assert e is not None
222
+
223
+ def test_valid_expr_err_is_none(self):
224
+ assert err("sin(a) + b") is None
225
+
226
+ def test_non_syntax_error_always_raised(self):
227
+ # Deliberately trigger a non-SyntaxError during traversal.
228
+ # An unsupported node type returns "" without raising, so we verify
229
+ # that a valid expression always returns err=None.
230
+ _, e = expr2katex("1 + 2")
231
+ assert e is None