# How can I fix the auto indentation in LaTeX?

Vim has some fancy pants indentation stuff. When it works right it's a pleasure to have, but sometimes it just drives me nuts. One that I've never been able to solve has to do with LaTeX syntax.

Lets say you have this file:

This is a test paragraph. Most of it is innocuous, but the \textit{inline markup code happens to fall over a line break} which is a problem.

% vim: autoindent textwidth=79 ft=tex


If you put your cursor in the first line and hit gqip to reformat the paragraph, it will format it like this:

This is a test paragraph. Most of it is innocuous, but the \textit{inline
markup code happens to fall over a line break} which is a problem.


But what I would like is this:

This is a test paragraph. Most of it is innocuous, but the \textit{inline
markup code happens to fall over a line break} which is a problem.


Of course I still do want it to auto-indent when I hit enter on a line ending in { (or {%. I also want to follow the indent of the previous line, so if I reformat:

    This is a test paragraph. Most of it is innocuous, but the \textit{inline markup code happens to fall over a line break} which is a problem.


I would like:

    This is a test paragraph. Most of it is innocuous, but the \textit{inline
markup code happens to fall over a line break} which is a problem.


Is there a way I can correct only the use case behavior of formatting paragraphs that have inline markup without affecting other usage scenarios?

• The autoindent in the modeline is not necessary, since it is already set by the indentation plugin. – Karl Yngve Lervåg Apr 14 '15 at 6:58

The documentation for the indentation plugin for LaTeX is unfortunately not available in the help files, but it is written in the source code in VIMRUNTIME/indent/tex.vim. The following variables are available for customization: g:tex_indent_brace: Toggle smartindent-like style for {} and []. g:tex_indent_items: Indent continuation lines after \items. g:tex_items: Tokens to be recognized as \items. g:tex_itemize_env: Environments that allow item indentation. g:tex_noindent_env: Environments that should not be indented. g:tex_indent_and: Whether to align the line with the first &, i.e. in tables and align environments. By setting the above variables, you may get a more customized indentation experience. There does exist alternative indentation plugins. I'm developing vimtex, which provides a function that IMHO is slightly better, although it is not as customizible (I do accept both pull requests and suggestions for improvements). LaTeX-Box also provides an indentation plugin. So does Automatic LaTeX plugin for Vim, and probably/possibly others. The indentation rules for LaTeX are defined in VIMRUNTIME/indent/tex.vim. There are a few configurable variables in there, such as g:tex_indent_brace, which is set by default. You could unset it, but that disables all indenting on braces, which is not what you want.

Getting the behavior that you want probably means digging into the GetTexIndent() function in that file and fixing the way it handles braces, unless you can find somebody that has already done that.