Skip to content

FishboneChiang/latexformat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

latexformat

A minimal Latex formatter written in Nim

While working on a PhD paper with LaTeX source files recently, I noticed that formatting my paper became unbearably slow as the project grew.

This inspired the creation of latexformat, a dead‑simple yet fast LaTeX formatter written in around 70 lines of code using the Nim programming language. It does not aim to cover all edge cases and instead prioritizes cache locality and predictability. It reads through the LaTeX source code, line by line, on a single pass and determines the indentation based on keywords such as {}, \begin{...}, \end{...}, and %.

While it is far from a full LaTeX parser, it helped me format my recently published paper on arXiv and has been tested to handle medium- to large-size JHEP papers (around 50 to 100 pages) with ease. On my personal computer, such files are typically formatted in around 5 ~ 10 ms, while the feature-rich latexindent often takes several seconds.

Installation

To compile the source code, simply run

nim c -d:release latexformat.nim

Integrating with conform.nvim is easy: please ensure that the compiled executable is in your PATH, then add the following lines to your configuration.

return {
    "stevearc/conform.nvim",
    opts = {
        formatters_by_ft = {
            tex = { "latexformat" },
        },
        formatters = {
            ["latexformat"] = {
                command = "latexformat",
                stdin = true,
            },
        },
    },
}

Usage

cat file.tex | latexformat > formatted_file.tex

Future work

Although straightforward to implement, I will leave these to future works:

  • Extra arguments to specify the indentation
  • Support for special cases such as verbatim environments
  • A config file to include a "whitelist" of keywords ignored by the program

Any feedback would be greatly appreciated. Happy TeXing!

About

A (super) simple Latex formatter written in Nim

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Contributors

Languages