Metadata-Version: 2.4
Name: math2tex
Version: 1.0.1
Summary: A high-performance compiler for translating plaintext mathematics into LaTeX.
Author: NicknamedTwice
License: MIT License
License-File: LICENSE
Requires-Python: >=3.8
Description-Content-Type: text/markdown

# math2tex

A compiler that converts plaintext mathematical notation into LaTeX.

`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.

**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:

```latex
\usepackage{physics}
```

## Features

- Correct handling of operator precedence and associativity.
- User-defined macros.
- Support for fractions, powers, roots, sums, products, limits, derivatives, and integrals.
- Support for matrices and piecewise functions.
- Recursive-descent parser with an Abstract Syntax Tree (AST).
- Comprehensive automated test suite.

## Installation

Install from a local clone:

```bash
pip install .
```

Or install from PyPI:

```bash
pip install math2tex
```

## Usage

Start the interactive command-line interface:

```bash
math2tex
```

You can then enter mathematical expressions and receive the corresponding LaTeX output.

## Examples

### Abel–Plana Formula

**Input**

```text
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
```

**Output**

```latex
\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}
```

### Contour Integral

**Input**

```text
oint_C f(z) dz = 2 pi i sum Res(f, a_k)
```

**Output**

```latex
\oint_{C}f(z)\dd{z}=2\pi i\sum \operatorname{Res}(f,a_{k})
```

### Nested Fractions

**Input**

```text
1 / (1 + 1 / (1 + x))
```

**Output**

```latex
\frac{1}{1+\frac{1}{1+x}}
```

### Matrix

**Input**

```text
[[1, 2], [3, 4]]
```

**Output**

```latex
\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}
```

## Project Structure

- **lexer.py** – Converts input text into a stream of tokens.
- **parser.py** – Builds an Abstract Syntax Tree (AST) from the token stream.
- **ast_nodes.py** – Defines the AST node types.
- **macros.py** – Handles user-defined macro expansion.
- **formatter.py** – Converts the AST into LaTeX.

## Testing

An extremely rigorous, automated testing framework is included in the `test_suite/` directory. Instead of relying solely on standard unit tests, the test suite (`gen.py`) dynamically constructs an Abstract Syntax Tree (AST) to generate deeply nested and mathematically complex edge cases. 

It tests every single supported command, operator, and keyword mapping in the configuration, randomizes spacing to catch implicit multiplication breaks, and evaluates structural boundaries up to multiple recursion depths. 

The generated `tests.txt` file contains 2,000 unique, exhaustive test cases covering both single-line expressions and multi-line equation blocks. The parser currently passes all 2,000 cases with a 100% success rate, ensuring that all operator precedence and lexical ambiguities are correctly resolved.

To run the full suite locally:

```bash
cd test_suite
python gen.py
python run_tests.py
```

## License

This project is licensed under the MIT License. See `LICENSE` for details.