Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
1  
Thanks for answers; still I would rather accept something Emacs independent. –  mbq Jul 26 '10 at 21:56
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 9 '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 9 '11 at 18:30
1  
Caveat: Compiling a document with LaTeX and processing it with Scientific Workplace (SWP) can create quite different results... –  Stephen Jan 6 '12 at 19:19
show 4 more comments

5 Answers

up vote 28 down vote accepted

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 my website: 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 here.

share|improve this answer
    
Fantastic, this looks really useful. –  Neil Olver Aug 10 '10 at 15:57
    
It messes up the code we have in custom minted environments. Are you working on any updates? –  recluze Jan 18 '12 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 '13 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. –  Andrew Stacey Aug 19 '13 at 12:35
    
Thanks! I wish I could test this, but there's a bug with pdftk for mac... tinyurl.com/k8aq8s4 –  Dror Aug 19 '13 at 13:19
show 3 more comments

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?

share|improve this answer
    
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. –  mbq Jul 26 '10 at 21:54
add comment

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.

share|improve this answer
5  
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
3  
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
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.