[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.9 Indenting

Indentation means the addition of whitespace at the beginning of lines to reflect special syntactical constructs. This makes it easier to see the structure of the document, and to catch errors such as a missing closing brace. Thus, the indentation is done for precisely the same reasons that you would indent ordinary computer programs.

Indentation is done by LaTeX environments and by TeX groups, that is the body of an environment is indented by the value of LaTeX-indent-level (default 2). Also, items of an ‘itemize-like’ environment are indented by the value of LaTeX-item-indent, default -2. (Items are identified with the help of LaTeX-item-regexp.) If more environments are nested, they are indented ‘accumulated’ just like most programming languages usually are seen indented in nested constructs.

You can explicitely indent single lines, usually by pressing <TAB>, or marked regions by calling indent-region on it. If you have auto-fill-mode enabled and a line is broken while you type it, Emacs automatically cares about the indentation in the following line. If you want to have a similar behavior upon typing <RET>, you can customize the variable TeX-newline-function and change the default of newline which does no indentation to newline-and-indent which indents the new line or reindent-then-newline-and-indent which indents both the current and the new line.

There are certain LaTeX environments which should be indented in a special way, like ‘tabular’ or ‘verbatim’. Those environments may be specified in the variable LaTeX-indent-environment-list together with their special indentation functions. Taking the ‘verbatim’ environment as an example you can see that current-indentation is used as the indentation function. This will stop AUCTeX from doing any indentation in the environment if you hit <TAB> for example.

There are environments in LaTeX-indent-environment-list which do not bring a special indentation function with them. This is due to the fact that first the respective functions are not implemented yet and second that filling will be disabled for the specified environments. This shall prevent the source code from being messed up by accidently filling those environments with the standard filling routine. If you think that providing special filling routines for such environments would be an appropriate and challenging task for you, you are invited to contribute. (See Filling, for further information about the filling functionality)

The check for the indentation function may be enabled or disabled by customizing the variable LaTeX-indent-environment-check.

As a side note with regard to formatting special environments: Newer Emacsen include ‘align.el’ and therefore provide some support for formatting ‘tabular’ and ‘tabbing’ environments with the function align-current which will nicely align columns in the source code.

AUCTeX is able to format commented parts of your code just as any other part. This means LaTeX environments and TeX groups in comments will be indented syntactically correct if the variable LaTeX-syntactic-comments is set to t. If you disable it, comments will be filled like normal text and no syntactic indentation will be done.

Following you will find a list of most commands and variables related to indenting with a small summary in each case:


LaTeX-indent-line will indent the current line.


newline-and-indent inserts a new line (much like <RET>) and moves the cursor to an appropriate position by the left margin.

Most keyboards nowadays lack a linefeed key and C-j may be tedious to type. Therefore you can customize AUCTeX to perform indentation upon typing <RET> as well. The respective option is called TeX-newline-function.


Alias for <LFD>

User Option: LaTeX-indent-environment-list

List of environments with special indentation. The second element in each entry is the function to calculate the indentation level in columns.

The filling code currently cannot handle tabular-like environments which will be completely messed-up if you try to format them. This is why most of these environments are included in this customization option without a special indentation function. This will prevent that they get filled.

User Option: LaTeX-indent-level

Number of spaces to add to the indentation for each ‘\begin’ not matched by a ‘\end’.

User Option: LaTeX-item-indent

Number of spaces to add to the indentation for ‘\item’’s in list environments.

User Option: TeX-brace-indent-level

Number of spaces to add to the indentation for each ‘{’ not matched by a ‘}’.

User Option: LaTeX-syntactic-comments

If non-nil comments will be filled and indented according to LaTeX syntax. Otherwise they will be filled like normal text.

User Option: TeX-newline-function

Used to specify the function which is called when <RET> is pressed. This will normally be newline which simply inserts a new line. In case you want to have AUCTeX do indentation as well when you press <RET>, use the built-in functions newline-and-indent or reindent-then-newline-and-indent. The former inserts a new line and indents the following line, i.e. it moves the cursor to the right position and therefore acts as if you pressed <LFD>. The latter function additionally indents the current line. If you choose ‘Other’, you can specify your own fancy function to be called when <RET> is pressed.

AUCTeX treats by default ‘\[...\]’ math mode as a regular environment and indents it accordingly. If you do not like such behavior you only need to remove \|\[ and \|\] from LaTeX-begin-regexp and LaTeX-end-regexp variables respectively.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on October 18, 2020 using texi2html 1.82.