Metadata-Version: 2.4
Name: math2tex
Version: 2.0.0
Summary: A high-performance compiler for translating plaintext mathematics into LaTeX.
Author: NicknamedTwice
Project-URL: Homepage, https://github.com/LostSyntax21/math2tex
Project-URL: Bug Tracker, https://github.com/LostSyntax21/math2tex/issues
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# 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 via PyPI:

```bash
pip install math2tex
```

Alternatively, install from a local clone for development:

```bash
git clone https://github.com/LostSyntax21/math2tex.git
cd math2tex
pip install .
```

## Usage

`math2tex` operates via a unified Command-Line Interface (CLI). You can run it globally using the `math2tex` command (if installed via pip) or directly as a Python module:

```bash
python -m math2tex <command>
```

### Available Commands

#### 1. Interactive REPL
Start the interactive terminal to enter plaintext math and instantly receive the corresponding LaTeX output.
```bash
python -m math2tex repl
```

#### 2. Live File Watching
Watch an input text file for changes and automatically compile the equations into an output file on every save.
```bash
python -m math2tex watch input.txt output.tex
```

#### 3. Test Pipeline
Run the compiler's internal test suite and evaluator.
```bash
# Run standard tests
python -m math2tex test

# Run full rigorous test suite (including the fuzzer)
python -m math2tex test --full
# or
python -m math2tex test -f
```

## 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}\left(f(x)\dd{x}+\frac{1}{2} f(0)+i \int_{0}^{\infty}\frac{f(i y)-f(-i y)}{e^{2\pi y}-1}\dd{y}\right)$$
```

### 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 \Re s(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.
- **config.py** – Configuration flags and central lexer vocabularies.
- **macros.py** – Handles user-defined macro expansion.
- **formatter.py** – Converts the AST into LaTeX.
- **cli.py** – The command line interface.

## Testing

An extremely rigorous, automated testing framework is included in the `tests/` directory using `pytest`. 

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

The parser currently passes all 285 grammatical edge-case tests with a 100% success rate, ensuring that all operator precedence and lexical ambiguities are correctly resolved.

To run the full suite locally:

```bash
pip install pytest
pytest tests/test_compiler.py -v
```

## License

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