Metadata-Version: 2.4
Name: gmpl-tex
Version: 0.1.1
Summary: Convert GMPL (GNU MathProg) models into LaTeX.
Author: Philipp Molnar
License: MIT License
        
        Copyright (c) 2026 philippmoln
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.10
Requires-Dist: lark>=1.1
Requires-Dist: nicegui>=2.0
Description-Content-Type: text/markdown

# gmpl-tex

Convert **GMPL** (GNU MathProg) optimization models into **LaTeX**, so the
sets, parameters, variables, constraints and objectives you wrote for a solver
can go straight into a paper with readable, renamed symbols.

Either way you use it, the tool works in two phases so you stay in control of
the notation:

1. **Lookup table** - every name in the model is collected into a table, where
   each raw name maps to a **label you can edit**.
2. **LaTeX generation** - the model is rendered to LaTeX using your edited labels.

There are two front-ends:

- **`gmpl-tex-web`** - a local web app: paste or drop a model, rename labels, and
  watch the LaTeX render live. Best for interactive work.
- **`gmpl-tex`** - a command-line tool: a scriptable two-step (JSON table → LaTeX)
  for batch use.

## Requirements

- **Python 3.10 or newer.** Check what you have:
  - Windows: `py --version`
  - macOS / Linux: `python3 --version`
  - If it's missing, install from [python.org](https://www.python.org/downloads/).
    On Windows, tick **"Add python.exe to PATH"** in the installer.
- [`lark`](https://github.com/lark-parser/lark) and [`nicegui`](https://nicegui.io)
  - installed automatically.

## Install

```bash
pip install gmpl-tex
```

One install gives you **both** front-ends: the `gmpl-tex` command line and the
`gmpl-tex-web` UI.

> **Windows:** use `py -m pip install gmpl-tex` if `pip` isn't on your PATH.

---

## Web UI

```bash
gmpl-tex-web          # opens http://localhost:8080 in your browser
gmpl-tex-web 9000     # ... on a different port
```

The app runs **only on your machine** - nothing is uploaded or reachable from the
network. Closing the terminal stops it.

**How to use it:**

1. **Add your model** - paste GMPL into the editor on the left, or drop / click to
   upload a `.mod` file.
2. **Rename labels** - in the middle **lookup table**, each row maps a raw name to
   the label you want in the output. Edits apply instantly. The section tabs
   (Sets, Parameters, Variables, Constraints, Objectives) filter the view.
3. **Take the LaTeX** - on the right, **List** shows each statement rendered;
   expand a card to edit or copy that single line. **Tex** shows the full document
   to **copy** or **download**. Both follow the active section tab.

<!--
Screenshots: create a  docs/screenshots/  folder and drop PNGs in it, then keep
the entries below whose files exist (delete the rest). Capture just the browser
window, not the whole desktop. A window ~1200px wide reads well on GitHub.
-->

![Overview](docs/screenshots/overview.png)
*The three panes: model editor (left), editable lookup table (middle), live preview (right).*

![Renaming a label](docs/screenshots/rename.png)
*Editing a label in the lookup table updates the rendered statement immediately.*

![Generated LaTeX](docs/screenshots/tex-tab.png)
*The **Tex** tab shows the full LaTeX document, ready to copy or download.*

<!-- Optional extra: a dark-mode shot.
![Dark mode](docs/screenshots/dark.png)
*The theme toggle (top right) switches light / dark and is remembered next time.*
-->

---

## Command line

```text
gmpl-tex model.mod [lookup.json] --json
gmpl-tex model.mod [lookup.json] [output.tex] --latex
```

### 1. Generate the editable lookup table (writes `model.json`)

```bash
gmpl-tex model.mod --json
```

For a model like

```
set I;
param d{i in I} >= 0;
var x{i in I} >= 0;
s.t. demand{i in I}: x[i] >= d[i];
minimize cost: sum{i in I} x[i];
```

`model.json` is:

```json
{
    "sets":        { "I": "I" },
    "parameters":  { "d": "d" },
    "variables":   { "x": "x" },
    "constraints": { "demand": "demand" },
    "objectives":  { "cost": "cost" }
}
```

### 2. Edit the labels you want to rename

```json
// Example: rename "cost" to "c"
{
    ...
    "objectives":  { "cost": "c" }
}
```

### 3. Render LaTeX using your edited labels (writes `model.tex`)

```bash
gmpl-tex model.mod model.json --latex
```

> **Note:** If you skip step 1 and run `gmpl-tex model.mod --latex` directly, a
> default table is created automatically (labels equal to the raw names) and used.
> An existing `model.json` next to the model is reused as-is and never overwritten.

An example model is included under [`examples/model.mod`](examples/model.mod).

*(Not sure which front-end you have? Run `gmpl-tex --help` or `gmpl-tex-web --help` - each points you to the other.)*

## Troubleshooting

- **Double subscript on LaTeX compile:** LaTeX rejects multiple `_` in a symbol.
  The converter will happily emit them, but LaTeX throws a `Double subscript`
  error. Remove the extra `_` from the label (in the lookup table or the JSON)
  and re-render.
- **A symbol shows up in red:** that name has no label - it's missing from the
  lookup table. This mainly happens with a hand-edited `model.json` (CLI); add the
  missing entry and re-run.

## License

MIT - see [LICENSE](LICENSE).