73

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.

doncherry
  • 54,637
  • 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 '10 at 15:30
  • @Aditya -- Have you tried ggVG=? – Richard Herron Apr 09 '11 at 16:24
  • 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 09 '11 at 18:30
  • 1
    @Aditya -- +1 thanks for the education. So much to learn about Vim! – Richard Herron Apr 10 '11 at 20:47
  • To make this question clearer for people who don't know indent, could you elaborate a bit on what kind of a tool you were looking for? – doncherry Oct 14 '11 at 14:57
  • Maybe this is a helpful answer for you: http://tex.stackexchange.com/a/1550/9666 – schmendrich Jan 06 '12 at 12:41
  • 1
    Caveat: Compiling a document with LaTeX and processing it with Scientific Workplace (SWP) can create quite different results... – Stephen Jan 06 '12 at 19:19
  • Have a look at latexindent, available as part of your tex distribution since late 2013 :) – cmhughes Jun 08 '15 at 18:20

5 Answers5

36

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.

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
19

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.

vanden
  • 30,891
  • 23
  • 67
  • 87
  • 6
    Can you run emacs in batch-mode for this, to avoid actually having to use the editor? – Lucas Jones Aug 10 '10 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)' – Ivan Andrus Jun 10 '12 at 13:38
10

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?

KeithB
  • 3,368
  • 2
  • 21
  • 16
  • 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. –  Jul 26 '10 at 21:54
6

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

Bundles->Latex->Tidy  

...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.

lockstep
  • 250,273
brainray
  • 163
5

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.

Mephisto
  • 1,306