Latex formatting with gq

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Latex formatting with gq

Mark Lodato
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
Reply | Threaded
Open this post in threaded view
|

Re: Latex formatting with gq

A. S. Budden
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.
Reply | Threaded
Open this post in threaded view
|

Re: Latex formatting with gq

Mikołaj Machowski
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.

Reply | Threaded
Open this post in threaded view
|

Re: Latex formatting with gq

Mark Lodato
On 8/9/05, Mikolaj Machowski <[hidden email]> wrote:
> This is done in Vim7 with 'formatlistpat' option where you can define
> regexp for item leads.
>

Ooh, I'll be looking forward to Vim7 then!


On 8/9/05, A. S. Budden <[hidden email]> wrote:

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

I saw that, in the help.  I would have liked an automatic thing, but I
tried it out and it works surprisingly well.  I set my "fo=tcqwa" and
"tw=78" and it seems to do me fine.

Thanks for the help!

Makr
Reply | Threaded
Open this post in threaded view
|

Re: Latex formatting with gq

A.J.Mechelynck
----- Original Message -----
From: "Mark Lodato" <[hidden email]>
To: <[hidden email]>
Sent: Wednesday, August 10, 2005 9:56 PM
Subject: Re: Latex formatting with gq
[...]
> Ooh, I'll be looking forward to Vim7 then!
[...]

The alpha version is already available. It is still under development:
features are added between one day and the next. You can get the source (for
all platforms and languages) in zipped form from
http://ftp.vim.org/pub/vim/unstable/snapshot/ and, for Windows, you can get
a ready-to-use distribution from a link at
http://users.skynet.be/antoine.mechelynck/vim/#vim7 . Don't remove your Vim
6 installation (so you'll have a fallback if a bug bites you), and read the
blurb before downloading.

Best regards,
Tony.