Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Vim, isn't there a more efficient way to format LaTeX paragraphs according to this best practice?

The best practice mentioned in the title is the one suggested by Uri:

When writing paragraphs, start each sentence at the beginning of a line, and if it spills over, each subsequent line is tabbed.

I use gVim with Vim-LaTeX, which comes with an indent/tex.vim file, to edit LaTeX files. The way I currently implement the practice mentioned above is as follows:

  1. I :set textwidth=79 to automatically break lines before they become too long.
  2. I manually hit Enter after I finish inserting each sentence.
  3. If I'm done with revising and editing a sentence, I manually shift any spillovers using >>, prefixing it with a count if necessary.

Occasionally, that last step will make one or more spillovers go over the maximum line width. In this case, I

  1. gqq the faulty line.
  2. J my way through to the end of the sentence.
  3. repeat steps 1 and 2 as necessary.

As you can imagine, this can become tedious. Isn't there a more efficient way to achieve the same result? Ultimately, I want to be able to write the sentences without worrying about their format, and then use gqap, or gqip, to automatically produce the result that I currently produce manually.

To do that, I suspect that I will need to write a formatexpr of my own, but I'm not sure how to proceed. I have found a number of plugins, Latex Text Formatter and Text (Especially LaTeX) Formatter, and a tip, but none of them seem to suit my needs, and I'm not sure how to modify them to do so.

like image 920
Yaser Sulaiman Avatar asked Apr 18 '11 17:04

Yaser Sulaiman


People also ask

Can I use Vim for LaTeX?

Vim is undoubtedly one of the best editors ever made. LaTeX is an extremely powerful, intelligent typesetter. Vim-LaTeX aims at bringing together the best of both these worlds. We attempt to provide a comprehensive set of tools to view, edit and compile LaTeX documents without needing to ever quit Vim.


1 Answers

I may well be oversimplifying the problem, but does this mapping do what you want?

nnoremap \z (j>>gq)

So pressing \z in normal mode will do the following: From the cursor position, jump to the start of the sentence. Then go to the next line and indent it. Then reformat from this line to the end of the sentence. Reformatting sentence-wise is the way to go, rather than reformatting each line individually, as your method seems to do.

Of course you can use an insert-mode mapping if you prefer, or even try redefining the behaviour of the Enter key to do this automatically (although I don't know if this will have unintended consequences...).

like image 93
Prince Goulash Avatar answered Nov 03 '22 02:11

Prince Goulash