Is there something like indent for LaTeX? I have a collaborator that is using Scientific Workplace and the code it produces is totally messed up; I know I shouldn't expect miracles, but any machine support would be useful.

EDIT: This question is quite undefined without my comment to KeithB's answer; the idea is to save VC-friendly one sentence/one paragraph per text file line structure which SWT "fixes" by wrapping everything into fixed column width format.

  • 3
    Finding something as smart as AucTeX will be difficult. I am not an emacs fan, in fact use vim for almost all the time, but when I get a really messed up latex file, I open it in emacs, do indent file, save the file, and then start editing in vim. I am pretty sure that calling the format function can be wrapped in command line option in emacs.
    – Aditya
    Aug 10, 2010 at 15:30
  • 2
    @richardh: ggVG= (or the slightly more efficient gg=G) only re-indents but does not reflow the text. I prefer breaking paragraphs into lines (usually tw=80), but some of the automatic generators create one line for each paragraph. gg=G does not handle that. Emacs reflow region command does it perfectly.
    – Aditya
    Apr 9, 2011 at 18:30
  • 1
    @Aditya -- +1 thanks for the education. So much to learn about Vim! Apr 10, 2011 at 20:47
  • 1
    Caveat: Compiling a document with LaTeX and processing it with Scientific Workplace (SWP) can create quite different results...
    – Stephen
    Jan 6, 2012 at 19:19
  • 1
    Have a look at latexindent, available as part of your tex distribution since late 2013 :)
    – cmhughes
    Jun 8, 2015 at 18:20

5 Answers 5


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.

  • Fantastic, this looks really useful.
    – Neil Olver
    Aug 10, 2010 at 15:57
  • It messes up the code we have in custom minted environments. Are you working on any updates?
    – recluze
    Jan 18, 2012 at 13:53
  • How would you suggest to test original vs. new if one is using pdflatex? I tried to convert pdf2ps but then I get many differences.
    – Dror
    Aug 19, 2013 at 12:15
  • 1
    @Dror I would use pdftk to uncompress the PDFs and then diff the uncompressed versions. Use as pdftk input.pdf output output.pdf uncompress. Aug 19, 2013 at 12:35
  • 1
    @AndrewSwann Fixed. Thanks for pointing it out. Feb 5, 2017 at 22:06

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.

  • 6
    Can you run emacs in batch-mode for this, to avoid actually having to use the editor? Aug 10, 2010 at 17:41
  • 4
    The following works for me (I installed AUCTeX via ELPA): emacs --batch -L ~/.emacs.d/elpa/auctex-11.86/ -l auctex-autoloads ~/TeX/test.tex --execute '(LaTeX-fill-region (point-min) (point-max)) (save-buffer)' Jun 10, 2012 at 13:38

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?

  • That's exactly my problem! I find it useless to use repository when there is no one-sentence-per-line principle applied. And here goes SWP folding the lines.
    – user7
    Jul 26, 2010 at 21:54

For those who have a Mac and don't like to work with emac or vin, but with an application that has a GUI: try TextMate . Open the .tex file to clean, select the text and go to


...and formatting and indentation of the LaTeX code is looking pretty. I'm glad I found this, as command line editors are not my preference.

If I got it right, TextMate 2 is now open source, so it's free.


What exactly is the problem? The way I use SWP, it produces very clean code. Have you asked your collaborator to save his files as "Portable Latex"? SWP breaks the code up into lines of roughly the same length. When it breaks math, it puts % sign in. This is correct, but if the line breaks have been lost, it causes problems.