Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/WGUNDERWOOD/tex-fmt

LaTeX formatter written in Rust
https://github.com/WGUNDERWOOD/tex-fmt

formatter indenter latex rust tex

Last synced: 3 months ago
JSON representation

LaTeX formatter written in Rust

Awesome Lists containing this project

README

        

# tex-fmt tex-fmt

[![CI](
https://github.com/wgunderwood/tex-fmt/actions/workflows/ci.yml/badge.svg)](
https://github.com/wgunderwood/tex-fmt/actions/workflows/ci.yml)
[![crates.io](
https://img.shields.io/crates/v/tex-fmt?logo=rust)](
https://crates.io/crates/tex-fmt)
[![Packaging status](
https://repology.org/badge/tiny-repos/tex-fmt.svg)](
https://repology.org/project/tex-fmt/versions)
[![license: MIT](
https://shields.io/badge/license-MIT-blue.svg)](
https://mit-license.org/)

An extremely fast LaTeX formatter written in Rust.

Input

Output

``` tex
\documentclass{article}

\begin{document}

\begin{itemize}
\item Lists with items
over multiple lines
\end{itemize}

\begin{equation}
E = m c^2
\end{equation}

\end{document}
```

``` tex
\documentclass{article}

\begin{document}

\begin{itemize}
\item Lists with items
over multiple lines
\end{itemize}

\begin{equation}
E = m c^2
\end{equation}

\end{document}
```

- โšกย  Extremely fast run-time performance
- ๐Ÿ”งย  Minimal configuration required
- ๐Ÿ“Ÿย  Command-line interface
- ๐Ÿ“œย  Handles LaTeX file types `.tex`, `.bib`, `.cls`, and `.sty`
- ๐Ÿฆ€ย  Written entirely in safe Rust

## Installation

### Cargo

Install the [stable release](https://crates.io/crates/tex-fmt) with

``` shell
cargo install tex-fmt
```

Install from [GitHub](https://github.com/WGUNDERWOOD/tex-fmt) with

```shell
cargo install --git "https://github.com/wgunderwood/tex-fmt"
```

### Nix

Install from
[nixpkgs](
https://search.nixos.org/packages?channel=unstable&query=tex-fmt)
into a temporary shell with

``` shell
nix-shell -p tex-fmt
```

Build from source using flakes with

``` shell
nix build "github:wgunderwood/tex-fmt"
```

Add to your NixOS installation with

```nix
environment.systemPackages = [
pkgs.tex-fmt
];
```

### Arch Linux

Install from the
[Arch User Repository](https://aur.archlinux.org/packages/tex-fmt).
For example, using the [yay](https://github.com/Jguer/yay) AUR helper:

``` shell
yay -S tex-fmt
```

### Homebrew

Install using
[Homebrew](https://formulae.brew.sh/formula/tex-fmt) with

```shell
brew install tex-fmt
```

### Binary download

Binaries for various platforms are available on the GitHub
[releases](https://github.com/WGUNDERWOOD/tex-fmt/releases) page.

### Visual Studio Code
Integration with VS Code is provided by the
[LaTeX Workshop](https://github.com/James-Yu/LaTeX-Workshop)
extension. You will need to first install tex-fmt
through one of the above methods.

## Usage
``` shell
tex-fmt file.tex # format file.tex and overwrite
tex-fmt --check file.tex # check if file.tex is correctly formatted
tex-fmt --print file.tex # format file.tex and print to STDOUT
tex-fmt --keep file.tex # do not wrap long lines
tex-fmt --stdin # read from STDIN and print to STDOUT
tex-fmt --help # view help information
```

### Disabling the formatter

Ending a source line with `% tex-fmt: skip` disables formatting for that line.
To disable the formatter for a block, use `% tex-fmt: off` and `% tex-fmt: on`.

``` tex
\documentclass{article}

\begin{document}

This line is skipped % tex-fmt: skip

% tex-fmt: off
These lines are also
not formatted or wrapped
% tex-fmt: on

\end{document}
```

Verbatim environments including `verbatim`, `Verbatim`, `lstlisting`
and `minted` are automatically skipped.

## Performance

When formatting all of the test cases,
tex-fmt is over two thousand times faster than latexindent.

| **Files** | **Lines** | **Size** | **tex-fmt** | **latexindent** | **latexindent -m** |
| --- | --- | --- | --- | --- | --- |
| 49 | 94k | 3.5M | **0.047s** | 106s [x2208] | 127s [x2661] |

## Contribution

Please feel free to open an issue or submit a pull request,
including as much information as you can. Documentation of internals
can be accessed by cloning this repository and running `cargo doc`.

Alternatively, you can
[Buy Me a Coffee](https://buymeacoffee.com/wgunderwood)!

## Limitations

- Semantic parsing of LaTeX code not conducted
- No linting or correction of syntax errors
- Customization via configuration files not supported
- Compliance with existing formatting guidelines not guaranteed
- No spelling or grammar checking

## Existing tools

- [latexindent](https://github.com/cmhughes/latexindent.pl).
Perl script, many configuration options, slow on large files

- [LaTeXTidy](http://bfc.sfsu.edu/cgi-bin/hsu.pl?LaTeX_Tidy).
Perl script, download links seem to be broken

- [latex-pretty](https://c.albert-thompson.com/latex-pretty/).
Browser-based, uses latexindent as the backend

- [latexformat.com](https://latexformat.com/).
Browser-based

- [texpretty](http://ftp.math.utah.edu/pub/texpretty/).
C program which works sometimes and appears to be fast

- [latex-editor](https://latex-editor.pages.dev/formatter/).
Browser-based

- [LaTeXFmt](https://github.com/engeljh/vim-latexfmt).
Vim plugin, does not apply indentation

- [latex-formatter](https://github.com/nfode/latex-formatter).
Visual Studio plugin, uses latexindent as the backend

## Options

The following command-line options are offered by tex-fmt.

| Option | Alias | Default | Description |
| ----------- | ----- | ------- | --- |
| `--check` | `-c` | | Check formatting, do not modify files. |
| `--print` | `-p` | | Print to STDOUT, do not modify files. |
| `--keep` | `-k` | | Keep lines, do not wrap. |
| `--verbose` | `-v` | | Show info log messages. |
| `--quiet` | `-q` | | Hide warning messages. |
| `--trace` | `-t` | | Show trace log messages. |
| `--stdin` | `-s` | | Process STDIN as a single file, output formatted text to STDOUT. |
| `--tab` | | `2` | Number of spaces to use as tab size. |
| `--usetabs` | | | Use tabs instead of spaces for indentation. |
| `--wrap` | | `80` | Line length for wrapping. |
| `--help` | `-h` | | Print help. |
| `--version` | `-V` | | Print version. |