Tool for cleaning LaTeX code

When I switched to using a VCS then I had a load of documents that I was writing that were not in suitable format for putting in to a VCS. In particular, the one-sentence-per-line rule was not in force. So I wrote one. I now use it for similar circumstances as you describe: when collaborators send me something and I want to import it in to my VCS. It's available from github: direct link and the relevant page about my experiences switching to a VCS. It's a perl script and the documentation is embedded: perldoc fmtlatex should provide it.

When using this program, I LaTeX the document first and then dvips to produce a postscript file. Then I run fmtlatex on it, and run latex+dvips again (being sure not to overwrite the original one). Doing diff original.ps new.ps will tell me if there's any functional difference between the two documents - it should just produce something about timestamps. If you have a document that does produce a significant difference, please send it to me (if possible) as that's evidence of a bug. (All this is in the documentation.)

I also have a script for swapping dollars for their LaTeX equivalents which is available also on github.


The wonderful AUCTeX provides a command LaTeX-fill-environment that will (mostly) fix up the indentation of regions of or the entirety of a LaTeX source file. AUCTeX is an emacs package though. So, while a must have for the emacs using LaTeX'er, it isn't much help if you don't use emacs.


I use M-q in emacs. It does a good enough job of indenting and splitting long lines.

Be careful if you are using a source code repository to collaborate on your documents (you should be). If you reformat the document each time you edit it, and Scientific Workplace does the same, it will become impossible to see what has changed between versions, or to merge changes.

Edit: Also see the answers to this question: How can you use latex to write a document with other people?