[ < ] [ > ]   [ << ] [ 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 explicitly 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.

For tabular-like environments, AUCTeX has a built-in function to indent according to preceding ‘&’ signs and assigns it to all known tabular-like environments in the default value of LaTeX-indent-environment-list.

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:

<TAB>

LaTeX-indent-line will indent the current line.

<LFD>
C-j

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.

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.

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.

A closely related topic is indenting of text enclosed in square brackets, parentheses and other pairs. AUCTeX offers two variables which control if indentation happens inside these pairs.

User Option: TeX-indent-open-delimiters

This variable contains additional opening delimiters which increase indentation. For example add [ to this variable to get text after a square bracket indented.

User Option: TeX-indent-close-delimiters

This is the accompanying variable to TeX-indent-open-delimiters decreasing the indentation again. This variable should contain ] if TeX-indent-open-delimiters is set like described above.

Note that this is an opt-in feature, both variables are initially set to an empty string. That is because it introduces non-trivial side effects to include [ and ] in TeX-indent-open-delimiters and TeX-indent-close-delimiters; if you only have an opening square bracket in your text without closing it, wrong indentation persists in the following text. For example, in math expression, half-open intervals are frequently written as ‘[0,10)’ or ‘[0,10[’. In such cases, you can put the closing part as a comment in the same line in order to have correct indentation after that:

 
$[0,10)$ % ]
$[0,10[$ % ]]

Another example is ‘\left’-‘\right’ pair in equations. Similar workarounds are available:

 
\begin{equation}
  \left[ % ]
    xyz
  \right] % [
  abc
\end{equation}

You can include parens ‘()’ also in TeX-indent-open-delimiters and TeX-indent-close-delimiters to enable indent inside them. Be prepared for similar side effects when you do.

Note that commented curly braces { and } aren’t counted when AUCTeX computes indentation.

In docTeX-mode, TeX code is enclosed in ‘macrocode’ environment like this:

 
%    \begin{macrocode}
\def\foo#1{%
  $#1$%
}
%    \end{macrocode}

Sometimes, the code is long and one wants to insert comments inside the TeX code like this:

 
%    \begin{macrocode}
\def\foo#1{%
%    \end{macrocode}
%   Comment the next line of code
%    \begin{macrocode}
  $#1$%
}
%    \end{macrocode}

Usually, the comment inside the code interrupts the indentation. This behavior can be controlled by setting the variable docTeX-indent-across-comments.

User Option: docTeX-indent-across-comments

If non-nil, indentation in docTeX is done across comments. This option is disabled by default.


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

This document was generated on January 17, 2024 using texi2html 1.82.