sponsor Vim development Vim logo Vim Book Ad

Latex Text Formatter : This scripts makes it easier to format latex text

 script karma  Rating 22/13, Downloaded by 3302  Comments, bugs, improvements  Vim wiki

created by
Sandro Bosio
 
script type
utility
 
description
The script allows to format a piece of latex text by splitting paragraphs according to latex keywords.

The main function, FormatLatexPar, formats the latex text to which the cursor belong, stopping above and below when a keyword is found, or when the vim paragraph ends. Then it moves at the end of the formatted section. If the current line is a comment, does the same thing, but searching for neighboring comments. The function is mapped to CTRL-j.

Note: have a look also at a similar scrip (http://www.vim.org/scripts/script.php?script_id=2307).

Todolist: merge multiple empty line into one.

If you have better solutions, improvements or suggestions, email me!

Here is an example of text, and the resulting formatted text obtained by repeated calls of the function:

<EXAMPLE BEGIN>
This is an example of the latex formatting script: the paragraphs are
formatted
by taking into account special latex ending keywords, as any
\begin{something}
    or as,
    for example
\end{something}

Also
formulas
$$
    like this
    one
$$
are kept separated

Other separators are a line beginning
with
{
    as this one
}  
but others can be easily added

Also,
comments
% like this
% or this
% are merged

but not % if the comment is not at the beginning of the line
% as in this case.

\begin{itemize}
    \item Some commands, as item, are delimiters,
    but one may want
    to format them with the text.
    \item These are treated differently
    in the script
\end{itemize}

You can easily suit this script to your needs
changing SingleLineLatexParEndings and
MultiLineLatexParEndings functions.
<EXAMPLE END>

is formatted, by subsequent calls, into

<FORMATTED EXAMPLE BEGIN>
This is an example of the latex formatting script: the paragraphs
are formatted by taking into account special latex ending keywords,
as any
\begin{something}
    or as, for example
\end{something}

Also formulas
$$
    like this one
$$  
are kept separated

Other separators are a line beginning with
{
    as this one
}  
but others can be easily added

Also, comments
% like this or this are merged

but not % if the comment is not at the beginning of the line
% as in this case.

\begin{itemize}
    \item Some commands, as item, are delimiters, but one may want
    to format them with the text.
    \item These are treated differently in the script
\end{itemize}

You can easily suit this script to your needs changing
SingleLineLatexParEndings and MultiLineLatexParEndings functions.
<FORMATTED EXAMPLE END>
 
install details
Copy in your .vim/plugin directory. If CTRL-j is already mapped, change the mapping for FormatLatexPar.
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
LatexParFormat.vim 2.5 2011-11-17 7.0 Sandro Bosio Corrected a bug closing the open tab when formatting a comment
LatexParFormat.vim 2.4 2011-11-17 7.0 Sandro Bosio Corrected a bug merging comments with standard text
LatexParFormat.vim 2.3 2011-11-17 7.0 Sandro Bosio In this version the comments are formatted even when the comment character is not at the beginning of a line, that is, indented multi-line comments get properly formatted as well.

The comment string is parametrized according to the filetype, so that e.g. formatting works also for c/c++ comments (very few languages supported). If the filetype is not recognized, the comment is set as the tex comment %.

The script also contains a simple but useful Comment command that toggles the comments on a range (again, for few languages only).

Remove or personalize the maps to FormatLatexPar and ToggleComment as desired.
LatexParFormat.vim 2.2 2008-11-11 7.0 Sandro Bosio Corrected a bug possibly splitting a comment at the end of a line, making it normal text.
LatexParFormat.vim 2.1 2008-09-25 7.0 Sandro Bosio Resolved problem with undos jumping at the beginning of the file. Better implementation (i.e., more readable) of some functions.
LatexParFormat.vim 2.0 2008-09-22 7.0 Sandro Bosio Basic improvements and bug removals: 1) It distinguish between paragraph separating commands (that do not have to be formatted with the text) and paragraph beginning commands (that will be formatted together with the paragraph they begin). 2) Works also on commented text, for any level of comments. The functions are now much more readable, too.
LatexParFormat.vim 1.3 2008-09-09 6.0 Sandro Bosio Added some paragraph delimiters, learned some vim scripting, improved readability for some functions. Now it works also for \item, and can be extended to any paragraph begin that one wants to be inlined with the text.
LatexParFormat.vim 1.2 2008-04-09 6.0 Sandro Bosio Some changes to the definition of paragraph endings (see the comments).
LatexParFormat.vim 1.0 2008-03-12 6.0 Sandro Bosio Initial upload
ip used for rating: 18.226.169.94

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github