math2tex 1.0.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.
@@ -0,0 +1,195 @@
1
+ """
2
+ math2tex.tests_legacy — Inline test suite for baseline verification.
3
+ """
4
+
5
+ from . import math2tex, math2tex_multiline, parse_macro_def
6
+
7
+
8
+ def run_tests():
9
+ tests = [
10
+ # --- Precedence & Parentheses ---
11
+ ('(a+b)*c', r'\left(a+b\right)c'),
12
+ ('a*(b+c)', r'a\left(b+c\right)'),
13
+ ('a/(b+c)', r'\frac{a}{b+c}'),
14
+ ('(a+b)/(c+d)', r'\frac{a+b}{c+d}'),
15
+ ('((a+b)/(c+d))+e', r'\frac{a+b}{c+d}+e'),
16
+ ('a+((b+c)/(d+e))', r'a+\frac{b+c}{d+e}'),
17
+ ('x^(a^(b+c))', r'x^{a^{b+c}}'),
18
+ ('e^a^b', r'e^{a^{b}}'),
19
+
20
+ # --- Implicit Multiplication ---
21
+ ('2x', r'2x'),
22
+ ('.5x', r'.5x'),
23
+ ('3sin(x)', r'3\sin(x)'),
24
+ ('x sin(x)', r'x\sin(x)'),
25
+ ('xy', r'xy'),
26
+ ('(x+1)(x-1)', r'\left(x+1\right)\left(x-1\right)'),
27
+
28
+ # --- Functions, Roots & Trig ---
29
+ ('sin(x)', r'\sin(x)'),
30
+ ('sin^2(x)', r'\sin^{2}(x)'),
31
+ ('cos^3(x)', r'\cos^{3}(x)'),
32
+ ('arctan(x)', r'\arctan(x)'),
33
+ ('log_2(x)', r'\log_{2}(x)'),
34
+ ('exp(x)', r'e^{x}'),
35
+ ('sin((x+1)/(x-1))', r'\sin\left(\frac{x+1}{x-1}\right)'),
36
+ ('sqrt(x^3+y^3)', r'\sqrt{x^{3}+y^{3}}'),
37
+ ('sqrt((a+b)/(c+d))', r'\sqrt{\frac{a+b}{c+d}}'),
38
+ ('root(3, x)', r'\sqrt[3]{x}'),
39
+
40
+ # --- Calculus (Derivatives & Integrals) ---
41
+ ('d/dx f(x)', r'\dv{f(x)}{x}'),
42
+ ('d / d x f(x)', r'\dv{f(x)}{x}'),
43
+ ('d / d theta f(theta)', r'\dv{f(\theta)}{\theta}'),
44
+ ('d^2/dx^2 f(x)', r'\dv[2]{f(x)}{x}'),
45
+ ('partial f / partial x', r'\pdv{f}{x}'),
46
+ ('partial^2 f / partial x^2', r'\pdv[2]{f}{x}'),
47
+ ('partial^2 f / partial x partial y', r'\pdv{f}{x}{y}'),
48
+ ('integral x^2 dx', r'\int x^{2}\dd{x}'),
49
+ ('integral from 0 to 1 x^2 dx', r'\int_{0}^{1} x^{2}\dd{x}'),
50
+ ('integral from -pi/2 to pi/2 sin(x) dx',
51
+ r'\int_{\frac{-\pi}{2}}^{\frac{\pi}{2}} \sin(x)\dd{x}'),
52
+ ('iint f(x,y) dx dy', r'\iint f(x,y)\dd{x}\dd{y}'),
53
+ ('iiint f(x,y,z) dx dy dz', r'\iiint f(x,y,z)\dd{x}\dd{y}\dd{z}'),
54
+ ('oint_C f(z) dz', r'\oint_{C} f(z)\dd{z}'),
55
+ ('integral_0^1 x^2 dx', r'\int_{0}^{1} x^{2}\dd{x}'),
56
+
57
+ # --- Sums, Products & Limits ---
58
+ ('sum_(n=1)^inf 1/n^2', r'\sum_{n=1}^{\infty}\frac{1}{n^{2}}'),
59
+ ('prod_(k=1)^n k', r'\prod_{k=1}^{n} k'),
60
+ ('lim_(x->0) sin(x)/x', r'\lim_{x\to 0}\frac{\sin(x)}{x}'),
61
+ ('lim_(n->inf) a_n', r'\lim_{n\to \infty} a_{n}'),
62
+
63
+ # --- Sets, Abs, Norms (physics package) ---
64
+ ('{x | x > 0}', r'\left\{x\mid x>0\right\}'),
65
+ ('|x|', r'\abs{x}'),
66
+ ('|x+y|', r'\abs{x+y}'),
67
+ ('||x||', r'\norm{x}'),
68
+ ('floor((x+1)/2)', r'\left\lfloor \frac{x+1}{2}\right\rfloor'),
69
+ ('ceil(x)', r'\left\lceil x\right\rceil'),
70
+ ('<a,b>', r'\langle a,b\rangle'),
71
+ ('det(A)', r'\det(A)'),
72
+ ('n choose k', r'\binom{n}{k}'),
73
+
74
+ # --- Matrices & Piecewise ---
75
+ ('[[1,2],[3,4]]', '\\begin{pmatrix}\n1 & 2 \\\\\n3 & 4\n\\end{pmatrix}'),
76
+ ('{ x^2 if x>=0, -x if x<0 }',
77
+ '\\begin{cases}\nx^{2}, & x\\ge 0 \\\\\n-x, & x<0\n\\end{cases}'),
78
+ ('{ 1 if x>0, 0 otherwise }',
79
+ '\\begin{cases}\n1, & x>0 \\\\\n0, & \\text{otherwise}\n\\end{cases}'),
80
+
81
+ # --- Constants & Greek Letters ---
82
+ ('alpha + beta = Gamma', r'\alpha+\beta=\Gamma'),
83
+ ('Re(z) + Im(z)', r'\Re(z)+\Im(z)'),
84
+ ('infinity', r'\infty'),
85
+
86
+ # --- Factorial ---
87
+ ('n!', r'n!'),
88
+ ('(n+1)!', r'\left(n+1\right)!'),
89
+ ('n!/(k!(n-k)!)',
90
+ r'\frac{n!}{k!\left(n-k\right)!}'),
91
+
92
+ # --- Prime Notation ---
93
+ ("f'(x)", r"f'(x)"),
94
+ ("g''(t)", r"g''(t)"),
95
+
96
+ # --- Relational Word Operators ---
97
+ ('x in A', r'x \in A'),
98
+ ('A subset B', r'A \subset B'),
99
+ ('x approx y', r'x \approx y'),
100
+ ('x != y', r'x\neq y'),
101
+ ('p implies q', r'p \implies q'),
102
+ ('p iff q', r'p \iff q'),
103
+
104
+ # --- Multiplicative Word Operators ---
105
+ ('a times b', r'a \times b'),
106
+ ('a cdot b', r'a \cdot b'),
107
+ ('A cup B', r'A \cup B'),
108
+ ('A cap B', r'A \cap B'),
109
+
110
+ # --- Plus-Minus ---
111
+ ('a +- b', r'a \pm b'),
112
+
113
+ # --- Accent Functions ---
114
+ ('hat(x)', r'\hat{x}'),
115
+ ('vec(v)', r'\vec{v}'),
116
+ ('bar(z)', r'\bar{z}'),
117
+ ('dot(q)', r'\dot{q}'),
118
+ ('tilde(x)', r'\tilde{x}'),
119
+
120
+ # --- Font Functions ---
121
+ ('mathbb(R)', r'\mathbb{R}'),
122
+ ('mathcal(L)', r'\mathcal{L}'),
123
+
124
+ # --- Special Constants ---
125
+ ('nabla f', r'\nabla f'),
126
+ ('forall x', r'\forall x'),
127
+ ('hbar', r'\hbar'),
128
+
129
+ # --- Semicolon Matrix ---
130
+ ('[1,2; 3,4]', '\\begin{pmatrix}\n1 & 2 \\\\\n3 & 4\n\\end{pmatrix}'),
131
+
132
+ # --- Function abs/norm ---
133
+ ('abs(x+y)', r'\abs{x+y}'),
134
+ ('norm(v)', r'\norm{v}'),
135
+
136
+ # --- Binom function ---
137
+ ('binom(n, k)', r'\binom{n}{k}'),
138
+
139
+ # --- operatorname fallback ---
140
+ ('sgn(x)', r'\operatorname{sgn}(x)'),
141
+
142
+ # --- Boxed ---
143
+ ('boxed(answer)', r'\boxed{answer}'),
144
+
145
+ # --- Ellipsis ---
146
+ ('a_1 + a_2 + ... + a_n', r'a_{1}+a_{2}+\ldots+a_{n}'),
147
+
148
+ # --- Advanced NL Parsing ---
149
+ ('integrate x^2 from 0 to 1', r'\int_{0}^{1} x^{2}\dd{x}'),
150
+ ('integral of x^2', r'\int x^{2}\dd{x}'),
151
+ ('sum of n^2, n=1, inf', r'\sum_{n=1}^{\infty} n^{2}'),
152
+ ('sum n^2 from 1 to inf', r'\sum_{1}^{\infty} n^{2}'),
153
+ ('limit of sin(x)/x as x approaches 0', r'\lim_{x\to 0}\frac{\sin(x)}{x}'),
154
+ ('square root of x over y', r'\sqrt{\frac{x}{y}}'),
155
+
156
+ # --- Expected Errors ---
157
+ ('a + ()', "% Parse Error: Empty parentheses '()' are not allowed outside of function calls."),
158
+
159
+ # --- Macro Engine ---
160
+ ('MACRO_TEST_1', r'x^{2}+y^{2}'),
161
+ ('MACRO_TEST_2', r'\left(a+b\right)^{2}+c^{2}'),
162
+ ]
163
+
164
+ test_macro_env = {}
165
+ name, macro = parse_macro_def("define f(x, y) = x^2 + y^2")
166
+ test_macro_env[name] = macro
167
+
168
+ print(f'{"INPUT":<50} | {"OUTPUT":<68} | PASS')
169
+ print('-' * 130)
170
+
171
+ passed_count = 0
172
+ total = 0
173
+ for raw, expected in tests:
174
+ total += 1
175
+ if raw == 'MACRO_TEST_1':
176
+ result = math2tex('f(x, y)', macro_env=test_macro_env)
177
+ raw = 'f(x, y)'
178
+ elif raw == 'MACRO_TEST_2':
179
+ result = math2tex('f(a+b, c)', macro_env=test_macro_env)
180
+ raw = 'f(a+b, c)'
181
+ else:
182
+ result = math2tex(raw)
183
+ passed = 'YES' if result.strip() == expected.strip() else 'NO'
184
+ if passed == 'YES':
185
+ passed_count += 1
186
+
187
+ display = result.replace('\n', ' ')
188
+ print(f'{raw:<50} | {display:<68} | {passed}')
189
+
190
+ if passed == 'NO':
191
+ print(f'\n Expected: {expected}\n Got: {result}\n')
192
+
193
+ print('-' * 130)
194
+ print(f'TESTS PASSED: {passed_count} / {total}')
195
+ return passed_count == total
@@ -0,0 +1,135 @@
1
+ Metadata-Version: 2.4
2
+ Name: math2tex
3
+ Version: 1.0.0
4
+ Summary: A high-performance compiler for translating plaintext mathematics into LaTeX.
5
+ Author: NicknamedTwice
6
+ License: MIT License
7
+ License-File: LICENSE
8
+ Requires-Python: >=3.8
9
+ Description-Content-Type: text/markdown
10
+
11
+ # math2tex
12
+
13
+ A compiler that converts plaintext mathematical notation into LaTeX.
14
+
15
+ `math2tex` is a recursive-descent parser written in Python. It translates mathematical expressions written in plain text into valid LaTeX while preserving operator precedence and mathematical structure. It supports everything from basic algebra to calculus, complex analysis, matrices, piecewise functions, and user-defined macros.
16
+
17
+ **Note:** The generated LaTeX uses commands provided by the `physics` package (for example `\dd{x}` and `\abs{x}`). Add the following to your document preamble:
18
+
19
+ ```latex
20
+ \usepackage{physics}
21
+ ```
22
+
23
+ ## Features
24
+
25
+ - Correct handling of operator precedence and associativity.
26
+ - User-defined macros.
27
+ - Support for fractions, powers, roots, sums, products, limits, derivatives, and integrals.
28
+ - Support for matrices and piecewise functions.
29
+ - Recursive-descent parser with an Abstract Syntax Tree (AST).
30
+ - Comprehensive automated test suite.
31
+
32
+ ## Installation
33
+
34
+ Install from a local clone:
35
+
36
+ ```bash
37
+ pip install .
38
+ ```
39
+
40
+ Or install from PyPI:
41
+
42
+ ```bash
43
+ pip install math2tex
44
+ ```
45
+
46
+ ## Usage
47
+
48
+ Start the interactive command-line interface:
49
+
50
+ ```bash
51
+ math2tex
52
+ ```
53
+
54
+ You can then enter mathematical expressions and receive the corresponding LaTeX output.
55
+
56
+ ## Examples
57
+
58
+ ### Abel–Plana Formula
59
+
60
+ **Input**
61
+
62
+ ```text
63
+ sum_(n=0)^inf f(n) = int_0^inf f(x) dx + 1/2 f(0) + i int_0^inf (f(i y) - f(-i y)) / (e^(2 pi y) - 1) dy
64
+ ```
65
+
66
+ **Output**
67
+
68
+ ```latex
69
+ \sum_{n=0}^{\infty} f(n)=\int_{0}^{\infty} f(x)\dd{x}+\frac{1}{2}f(0)+i\int_{0}^{\infty}\frac{f(iy)-f(-iy)}{e^{2\pi y}-1}\dd{y}
70
+ ```
71
+
72
+ ### Contour Integral
73
+
74
+ **Input**
75
+
76
+ ```text
77
+ oint_C f(z) dz = 2 pi i sum Res(f, a_k)
78
+ ```
79
+
80
+ **Output**
81
+
82
+ ```latex
83
+ \oint_{C}f(z)\dd{z}=2\pi i\sum \operatorname{Res}(f,a_{k})
84
+ ```
85
+
86
+ ### Nested Fractions
87
+
88
+ **Input**
89
+
90
+ ```text
91
+ 1 / (1 + 1 / (1 + x))
92
+ ```
93
+
94
+ **Output**
95
+
96
+ ```latex
97
+ \frac{1}{1+\frac{1}{1+x}}
98
+ ```
99
+
100
+ ### Matrix
101
+
102
+ **Input**
103
+
104
+ ```text
105
+ [[1, 2], [3, 4]]
106
+ ```
107
+
108
+ **Output**
109
+
110
+ ```latex
111
+ \begin{pmatrix}
112
+ 1 & 2 \\
113
+ 3 & 4
114
+ \end{pmatrix}
115
+ ```
116
+
117
+ ## Project Structure
118
+
119
+ - **lexer.py** – Converts input text into a stream of tokens.
120
+ - **parser.py** – Builds an Abstract Syntax Tree (AST) from the token stream.
121
+ - **ast_nodes.py** – Defines the AST node types.
122
+ - **macros.py** – Handles user-defined macro expansion.
123
+ - **formatter.py** – Converts the AST into LaTeX.
124
+
125
+ ## Testing
126
+
127
+ Run the test suite with:
128
+
129
+ ```bash
130
+ pytest
131
+ ```
132
+
133
+ ## License
134
+
135
+ This project is licensed under the MIT License. See `LICENSE` for details.
@@ -0,0 +1,14 @@
1
+ math2tex/__init__.py,sha256=Ysn0FjeCZo6hLETvz0qdTGuRes01wyAofR4ZYhqM0nI,2619
2
+ math2tex/ast_nodes.py,sha256=Bjn7CVKgPyUuucAABe-v-h7vIb9858tbR81n_XyRUYo,4758
3
+ math2tex/cli.py,sha256=GtKLiMZNMeO8pYPVUhLRsAV4JtwTz9fZWMzjVmKHcjs,1789
4
+ math2tex/config.py,sha256=yxL3-pHsMRK0QC4uPCg6kpU7meMODSBQB1Ur8AtUA2A,8244
5
+ math2tex/formatter.py,sha256=rG9bZrIHhXH0xk6Cn0n3YgG-m3nmA3IJr4awNkZwyqs,20633
6
+ math2tex/lexer.py,sha256=nE65rSsrKMb3MybNKxn0b1oaPPdbJ48I9iPlIZO7hmU,5154
7
+ math2tex/macros.py,sha256=e4wNNFJlYOCDEDcWn3jDwi34dLAsWCuHJ1Q6gFJePCA,15626
8
+ math2tex/parser.py,sha256=nQZ1mF97LRK24QfR7L9xA08Ijy4kjujI7j5TYo98mek,36616
9
+ math2tex/tests_legacy.py,sha256=pnDk1BPAlcYp-uMr3Wteeex6ixjG5HCcHYF9gAzgJKI,8446
10
+ math2tex-1.0.0.dist-info/METADATA,sha256=eBpCg2rEATotsnVO0T5ir_rDYSghT_TacCXWIFl-E2c,2727
11
+ math2tex-1.0.0.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
12
+ math2tex-1.0.0.dist-info/entry_points.txt,sha256=VmZ9RKDWvFMLqSMvbomKhRSqdH2I4InIwKWleoYTmrk,47
13
+ math2tex-1.0.0.dist-info/licenses/LICENSE,sha256=JS5wsPpbs3i-kJpm4RrqVt3q6otmpboRhL01JKiJQ8M,1071
14
+ math2tex-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.31.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ math2tex = math2tex.cli:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 NicknamedTwice
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.