# Latex formatting with gq

5 messages
Open this post in threaded view
|

## Latex formatting with gq

 I use vim to edit latex files and often use 'gq' to format the text. It usually looks fine, but sometimes I want to have particular strings start the beginning of a line.  For example, I want my text to be formatted like this: \begin{enumerate}     \item first item     \item second item     \item third item \end{enumerate} (the \item lines are preceeded by 4 spaces - i'm not sure if they'll come out in the email) But 'gq' will change it to: \begin{enumerate} \item first item \item second item \end{enumerate} which is really ugly.  Essentially, I would like 'gq' to begin a new line whenever it hits one of: \begin{ \item \end{ (and possibly others) Also, I'd like it to automatically put a newline AFTER the following: \part{.*} \chapter{.*} \section{.*} \subsection{.*} (and so on...) For example, 'gq' produces: \section{the section} This is a sentence. but I'd like it to do: \section{the section} This is a sentence. Does anyone know if this is possible?  I know you can set formatprg to an external program, but I don't know what one to use.  I looked at 'fmt' and 'par', but I don't think either of them can do what I want. I find it hard to imagine I'm the only one who has come across this problem.  Any ideas out there? Thanks a lot, Mark
Open this post in threaded view
|

## Re: Latex formatting with gq

 On 8/8/05, Mark Lodato <[hidden email]> wrote: > I use vim to edit latex files and often use 'gq' to format the text. > It usually looks fine, but sometimes I want to have particular strings > start the beginning of a line.  For example, I want my text to be > formatted like this: > > \begin{enumerate} >     \item first item >     \item second item >     \item third item > \end{enumerate} > > (the \item lines are preceeded by 4 spaces - i'm not sure if they'll > come out in the email) > But 'gq' will change it to: > > \begin{enumerate} \item first item \item second item \end{enumerate} > > which is really ugly.  Essentially, I would like 'gq' to begin a new > line whenever it hits one of: > \begin{ > \item > \end{ > (and possibly others) > [snip] This isn't QUITE what you asked for, but if you add the 'w' option to formatoptions (see :help 'fo' and :help fo-table), gq will only reformat when there's a blank space at the end of the line and therefore the \begin...\end blocks will be preserved, as will a newline after a \section or whatever (as long as you don't put a space on the end of the line).  It doesn't, however, automatically add a newline after the \chapter lines etc. Hope that helps, Al P.S. In case you're interested, I use "set fo=tcqwal" and "set tw=78" for editing LaTeX documents and it works like a charm for me.
Open this post in threaded view
|

## Re: Latex formatting with gq

 In reply to this post by Mark Lodato Dnia poniedzia?ek 08 sierpie? 2005 22:46, Mark Lodato napisa?: > I use vim to edit latex files and often use 'gq' to format the text. > It usually looks fine, but sometimes I want to have particular strings > start the beginning of a line.  For example, I want my text to be > formatted like this: > > \begin{enumerate} >     \item first item >     \item second item >     \item third item > \end{enumerate} > > (the \item lines are preceeded by 4 spaces - i'm not sure if they'll > come out in the email) > But 'gq' will change it to: > > \begin{enumerate} \item first item \item second item \end{enumerate} > > which is really ugly.  Essentially, I would like 'gq' to begin a new > line whenever it hits one of: > \begin{ > \item > \end{ > (and possibly others) This is done in Vim7 with 'formatlistpat' option where you can define regexp for item leads. > Also, I'd like it to automatically put a newline AFTER the following: > \part{.*} > \chapter{.*} > \section{.*} > \subsection{.*} > (and so on...) > > For example, 'gq' produces: > > \section{the section} This is a sentence. Don't think it is possible but simple regexp can fix this: %s/\\section{.\{-}}/&\r/g m.