Next: , Previous: , Up: top   [Contents][Index]

2 Editing the Document Source

The most commonly used commands/macros of AUCTeX are those which simply insert templates for often used TeX, LaTeX, or ConTeXt constructs, like font changes, handling of environments, etc. These features are very simple, and easy to learn, and help you avoid mistakes like mismatched braces, or ‘\begin{}’-‘\end{}’ pairs.

Apart from that this chapter contains a description of some features for entering more specialized sorts of text, for formatting the source by indenting and filling and for navigating through the document.


Next: , Up: Editing   [Contents][Index]

2.1 Insertion of Quotes, Dollars, and Braces

Quotation Marks

In TeX, literal double quotes ‘"like this"’ are seldom used, instead two single quotes are used ‘``like this''’. To help you insert these efficiently, AUCTeX allows you to continue to press " to insert two single quotes. To get a literal double quote, press " twice.

Command: TeX-insert-quote count

(") Insert the appropriate quote marks for TeX.

Inserts the value of TeX-open-quote (normally ‘``’) or TeX-close-quote (normally ‘''’) depending on the context. With prefix argument, always inserts ‘"’ characters.

User Option: TeX-open-quote

String inserted by typing " to open a quotation. (See European, for language-specific quotation mark insertion.)

User Option: TeX-close-quote

String inserted by typing " to close a quotation. (See European, for language-specific quotation mark insertion.)

User Option: TeX-quote-after-quote

Determines the behavior of ". If it is non-nil, typing " will insert a literal double quote. The respective values of TeX-open-quote and TeX-close-quote will be inserted after typing " once again.

The ‘babel’ package provides special support for the requirements of typesetting quotation marks in many different languages. If you use this package, either directly or by loading a language-specific style file, you should also use the special commands for quote insertion instead of the standard quotes shown above. AUCTeX is able to recognize several of these languages and will change quote insertion accordingly. See European, for details about this feature and how to control it.

In case you are using the ‘csquotes’ package, you should customize LaTeX-csquotes-open-quote, LaTeX-csquotes-close-quote and LaTeX-csquotes-quote-after-quote. The quotation characters will only be used if both variables—LaTeX-csquotes-open-quote and LaTeX-csquotes-close-quote—are non-empty strings. But then the ‘csquotes’-related values will take precedence over the language-specific ones.

Dollar Signs

In AUCTeX, dollar signs should match like they do in TeX. This has been partially implemented, we assume dollar signs always match within a paragraph. By default, the first ‘$’ you insert in a paragraph will do nothing special. The second ‘$’ will match the first. This will be indicated by moving the cursor temporarily over the first dollar sign.

Command: TeX-insert-dollar arg

($) Insert dollar sign.

Show matching dollar sign if this dollar sign end the TeX math mode.

With optional arg, insert that many dollar signs.

TeX and LaTeX users often look for a way to insert inline equations like ‘$...$’ or ‘\(...\)’ simply typing $. AUCTeX helps them through the customizable variable TeX-electric-math.

User Option: TeX-electric-math

If the variable is non-nil and you type $ outside math mode, AUCTeX will automatically insert the opening and closing symbols for an inline equation and put the point between them. The opening symbol will blink when blink-matching-paren is non-nil. If TeX-electric-math is nil, typing $ simply inserts ‘$’ at point, this is the default.

Besides nil, possible values for this variable are (cons "$" "$") for TeX inline equations ‘$...$’, and (cons "\\(" "\\)") for LaTeX inline equations ‘\(...\)’.

If the variable is non-nil and point is inside math mode right between a couple of single dollars, pressing $ will insert another pair of dollar signs and leave the point between them. Thus, if TeX-electric-math is set to (cons "$" "$") you can easily obtain a TeX display equation ‘$$...$$’ by pressing $ twice in a row. (Note that you should not use double dollar signs in LaTeX because this practice can lead to wrong spacing in typeset documents.)

In addition, when the variable is non-nil and there is an active region outside math mode, typing $ will put around the active region symbols for opening and closing inline equation and keep the region active, leaving point after the closing symbol. By pressing repeatedly $ while the region is active you can toggle between an inline equation, a display equation, and no equation. To be precise, ‘$...$’ is replaced by ‘$$...$$’, whereas ‘\(...\)’ is replaced by ‘\[...\]’.

If you want to automatically insert ‘$...$’ in plain TeX files, and ‘\(...\)’ in LaTeX files by pressing $, add the following to your init file

(add-hook 'plain-TeX-mode-hook
	  (lambda () (set (make-local-variable 'TeX-electric-math)
			  (cons "$" "$"))))
(add-hook 'LaTeX-mode-hook
	  (lambda () (set (make-local-variable 'TeX-electric-math)
			  (cons "\\(" "\\)"))))

Braces

To avoid unbalanced braces, it is useful to insert them pairwise. You can do this by typing C-c {.

Command: TeX-insert-braces

(C-c {) Make a pair of braces and position the cursor to type inside of them. If there is an active region, put braces around it and leave point after the closing brace.

When writing complex math formulas in LaTeX documents, you sometimes need to adjust the size of braces with pairs of macros like ‘\left’-‘\right’, ‘\bigl’-‘\bigr’ and so on. You can avoid unbalanced pairs with the help of TeX-insert-macro, bound to C-c C-m or C-c RET (see Completion). If you insert left size adjusting macros such as ‘\left’, ‘\bigl’ etc. with TeX-insert-macro, it asks for left brace to use and supplies automatically right size adjusting macros such as ‘\right’, ‘\bigr’ etc. and corresponding right brace in addtion to the intended left macro and left brace.

The completion by TeX-insert-macro also applies when entering macros such as ‘\langle’, ‘\lfloor’ and ‘\lceil’, which produce the left part of the paired braces. For example, inserting ‘\lfloor’ by C-c C-m is immediately followed by the insertion of ‘\rfloor’. In addition, if the point was located just after ‘\left’ or its friends, the corresponding ‘\right’ etc. will be inserted in front of ‘\rfloor’. In both cases, active region is honored.

As a side effect, when LaTeX-math-mode (see Mathematics) is on, just typing `( inserts not only ‘\langle’, but also ‘\rangle’.

If you do not like such auto completion at all, it can be disabled by a user option.

User Option: TeX-arg-right-insert-p

If this option is turned off, the automatic supply of the right macros and braces is suppressed.

When you edit LaTeX documents, you can enable automatic brace pairing when typing (, { and [.

User Option: LaTeX-electric-left-right-brace

If this option is on, just typing (, { or [ immediately adds the corresponding right brace ‘)’, ‘}’ or ‘]’. The point is left after the opening brace. If there is an active region, braces are put around it.

They recognize the preceding backslash or size adjusting macros such as ‘\left’, ‘\bigl’ etc., so the following completions will occur:

This auto completion feature may be a bit annoying when editing an already existing LaTeX document. In that case, use C-u 1 or C-q before typing (, { or [. Then no completion is done and just a single left brace is inserted. In fact, with optional prefix arg, just that many open braces are inserted without any completion.


Next: , Previous: , Up: Editing   [Contents][Index]

2.2 Inserting Font Specifiers

Perhaps the most used keyboard commands of AUCTeX are the short-cuts available for easy insertion of font changing macros.

If you give an argument (that is, type C-u) to the font command, the innermost font will be replaced, i.e. the font in the TeX group around point will be changed. The following table shows the available commands, with indicating the position where the text will be inserted.

C-c C-f C-b

Insert bold face\textbf{∗}’ text.

C-c C-f C-i

Insert italics\textit{∗}’ text.

C-c C-f C-e

Insert emphasized\emph{∗}’ text.

C-c C-f C-s

Insert slanted\textsl{∗}’ text.

C-c C-f C-r

Insert roman \textrm{∗} text.

C-c C-f C-f

Insert sans serif\textsf{∗}’ text.

C-c C-f C-t

Insert typewriter\texttt{∗}’ text.

C-c C-f C-c

Insert SMALL CAPS\textsc{∗}’ text.

C-c C-f C-d

Delete the innermost font specification containing point.

Command: TeX-font replace what

(C-c C-f) Insert template for font change command.

If replace is not nil, replace current font. what determines the font to use, as specified by TeX-font-list.

User Option: TeX-font-list

List of fonts used by TeX-font.

Each entry is a list with three elements. The first element is the key to activate the font. The second element is the string to insert before point, and the third element is the string to insert after point. An optional fourth element means always replace if not nil.

User Option: LaTeX-font-list

List of fonts used by TeX-font in LaTeX mode. It has the same structure as TeX-font-list.


Next: , Previous: , Up: Editing   [Contents][Index]

2.3 Inserting chapters, sections, etc.

Insertion of sectioning macros, that is ‘\chapter’, ‘\section’, ‘\subsection’, etc. and accompanying ‘\label’’s may be eased by using C-c C-s. This command is highly customizable, the following describes the default behavior.

When invoking you will be asked for a section macro to insert. An appropriate default is automatically selected by AUCTeX, that is either: at the top of the document; the top level sectioning for that document style, and any other place: The same as the last occurring sectioning command.

Next, you will be asked for the actual name of that section, and last you will be asked for a label to be associated with that section. The label will be prefixed by the value specified in LaTeX-section-hook.

Command: LaTeX-section arg

(C-c C-s) Insert a sectioning command.

Determine the type of section to be inserted, by the argument arg.

The following variables can be set to customize the function.

LaTeX-section-hook

Hooks to be run when inserting a section.

LaTeX-section-label

Prefix to all section references.

The precise behavior of LaTeX-section is defined by the contents of LaTeX-section-hook.

User Option: LaTeX-section-hook

List of hooks to run when a new section is inserted.

The following variables are set before the hooks are run

level

Numeric section level, default set by prefix arg to LaTeX-section.

name

Name of the sectioning command, derived from level.

title

The title of the section, default to an empty string.

toc

Entry for the table of contents list, default nil.

done-mark

Position of point afterwards, default nil meaning after the inserted text.

A number of hooks are already defined. Most likely, you will be able to get the desired functionality by choosing from these hooks.

LaTeX-section-heading

Query the user about the name of the sectioning command. Modifies level and name.

LaTeX-section-title

Query the user about the title of the section. Modifies title.

LaTeX-section-toc

Query the user for the toc entry. Modifies toc.

LaTeX-section-section

Insert LaTeX section command according to name, title, and toc. If toc is nil, no toc entry is inserted. If toc or title are empty strings, done-mark will be placed at the point they should be inserted.

LaTeX-section-label

Insert a label after the section command. Controlled by the variable LaTeX-section-label.

To get a full featured LaTeX-section command, insert

(setq LaTeX-section-hook
      '(LaTeX-section-heading
	LaTeX-section-title
	LaTeX-section-toc
	LaTeX-section-section
	LaTeX-section-label))

in your .emacs file.

The behavior of LaTeX-section-label is determined by the variable LaTeX-section-label.

User Option: LaTeX-section-label

Default prefix when asking for a label.

If it is a string, it is used unchanged for all kinds of sections. If it is nil, no label is inserted. If it is a list, the list is searched for a member whose car is equal to the name of the sectioning command being inserted. The cdr is then used as the prefix. If the name is not found, or if the cdr is nil, no label is inserted.

By default, chapters have a prefix of ‘cha:’ while sections and subsections have a prefix of ‘sec:’. Labels are not automatically inserted for other types of sections.


Next: , Previous: , Up: Editing   [Contents][Index]

2.4 Inserting Environment Templates

A large apparatus is available that supports insertions of environments, that is ‘\begin{}’ — ‘\end{}’ pairs.

AUCTeX is aware of most of the actual environments available in a specific document. This is achieved by examining your ‘\documentclass’ command, and consulting a precompiled list of environments available in a large number of styles.

Most of these are described further in the following sections, and you may easily specify more. See Customizing Environments.

You insert an environment with C-c C-e, and select an environment type. Depending on the environment, AUCTeX may ask more questions about the optional parts of the selected environment type. With C-u C-c C-e you will change the current environment.

Command: LaTeX-environment arg

(C-c C-e) AUCTeX will prompt you for an environment to insert. At this prompt, you may press TAB or SPC to complete a partially written name, and/or to get a list of available environments. After selection of a specific environment AUCTeX may prompt you for further specifications.

If the optional argument arg is not-nil (i.e. you have given a prefix argument), the current environment is modified and no new environment is inserted.

AUCTeX helps you adding labels to environments which use them, such as ‘equation’, ‘figure’, ‘table’, etc… When you insert one of the supported environments with C-c C-e, you will be automatically prompted for a label. You can select the prefix to be used for such environments with the LaTeX-label-alist variable.

User Option: LaTeX-label-alist

List the prefixes to be used for the label of each supported environment.

This is an alist whose car is the environment name, and the cdr either the prefix or a symbol referring to one.

If the name is not found, or if the cdr is nil, no label is automatically inserted for that environment.

If you want to automatically insert a label for a environment but with an empty prefix, use the empty string "" as the cdr of the corresponding entry.

As a default selection, AUCTeX will suggest the environment last inserted or, as the first choice the value of the variable LaTeX-default-environment.

User Option: LaTeX-default-environment

Default environment to insert when invoking ‘LaTeX-environment’ first time. When the current environment is ‘document’, it is overriden by LaTeX-default-document-environment.

Variable: LaTeX-default-document-environment

Default environment when invoking ‘LaTeX-environment’ and the current environment is ‘document’. It is intended to be used in LaTeX class style files. For example, in beamer.el it is set to frame, in letter.el to letter, and in slides.el to slide.

If the document is empty, or the cursor is placed at the top of the document, AUCTeX will default to insert a ‘document’ environment prompting also for the insertion of ‘\documentclass’ and ‘\usepackage’ macros. You will be prompted for a new package until you enter nothing. If you do not want to insert any ‘\usepackage’ at all, just press RET at the first ‘Packages’ prompt.

AUCTeX distinguishes normal and expert environments. By default, it will offer completion only for normal environments. This behavior is controlled by the user option TeX-complete-expert-commands.

User Option: TeX-complete-expert-commands

Complete macros and environments marked as expert commands.

Possible values are nil, t, or a list of style names.

nil

Don’t complete expert commands (default).

t

Always complete expert commands.

(STYLES …)

Only complete expert commands of STYLES.

You can close the current environment with C-c ], but we suggest that you use C-c C-e to insert complete environments instead.

Command: LaTeX-close-environment

(C-c ]) Insert an ‘\end’ that matches the current environment.

AUCTeX offers keyboard shortcuts for moving point to the beginning and to the end of the current environment.

Command: LaTeX-find-matching-begin

(C-M-a) Move point to the ‘\begin’ of the current environment.

If this command is called inside a comment and LaTeX-syntactic-comments is enabled, try to find the environment in commented regions with the same comment prefix.

Command: LaTeX-find-matching-end

(C-M-e) Move point to the ‘\end’ of the current environment.

If this command is called inside a comment and LaTeX-syntactic-comments is enabled, try to find the environment in commented regions with the same comment prefix.


Next: , Up: Environments   [Contents][Index]

2.4.1 Equations

When inserting equation-like environments, the ‘\label’ will have a default prefix, which is controlled by the following variables:

User Option: LaTeX-equation-label

Prefix to use for ‘equation’ labels.

User Option: LaTeX-eqnarray-label

Prefix to use for ‘eqnarray’ labels.

User Option: LaTeX-amsmath-label

Prefix to use for amsmath equation labels. Amsmath equations include ‘align’, ‘alignat’, ‘xalignat’, ‘aligned’, ‘flalign’ and ‘gather’.


Next: , Previous: , Up: Environments   [Contents][Index]

2.4.2 Floats

Figures and tables (i.e., floats) may also be inserted using AUCTeX. After choosing either ‘figure’ or ‘table’ in the environment list described above, you will be prompted for a number of additional things.

float position

This is the optional argument of float environments that controls how they are placed in the final document. In LaTeX this is a sequence of the letters ‘htbp’ as described in the LaTeX manual. The value will default to the value of LaTeX-float.

caption

This is the caption of the float. The default is to insert the caption at the bottom of the float. You can specify floats where the caption should be placed at the top with LaTeX-top-caption-list.

short caption

If the specified caption is greater than a specific length, then a short caption is prompted for and it is inserted as an optional argument to the ‘\caption’ macro. The length that a caption needs to be before prompting for a short version is controlled by LaTeX-short-caption-prompt-length.

label

The label of this float. The label will have a default prefix, which is controlled by the variables LaTeX-figure-label and LaTeX-table-label.

Moreover, you will be asked if you want the contents of the float environment to be horizontally centered. Upon a positive answer a ‘\centering’ macro will be inserted at the beginning of the float environment.

User Option: LaTeX-float

Default placement for floats.

User Option: LaTeX-figure-label

Prefix to use for figure labels.

User Option: LaTeX-table-label

Prefix to use for table labels.

User Option: LaTeX-top-caption-list

List of float environments with top caption.

User Option: LaTeX-short-caption-prompt-length

Number of chars a caption should be before prompting for a short caption.


Next: , Previous: , Up: Environments   [Contents][Index]

2.4.3 Itemize-like Environments

In an itemize-like environment, nodes (i.e., ‘\item’s) may be inserted using C-c LFD.

Command: LaTeX-insert-item

(C-c LFD) Close the current item, move to the next line and insert an appropriate ‘\item’ for the current environment. That is, ‘itemize’ and ‘enumerate’ will have ‘\item ’ inserted, while ‘description’ will have ‘\item[]’ inserted.

User Option: TeX-arg-item-label-p

If non-nil, you will always be asked for optional label in items. Otherwise, you will be asked only in description environments.


Next: , Previous: , Up: Environments   [Contents][Index]

2.4.4 Tabular-like Environments

When inserting Tabular-like environments, that is, ‘tabular’ ‘array’ etc., you will be prompted for a template for that environment. Related variables:

User Option: LaTeX-default-format

Default format string for array and tabular environments.

User Option: LaTeX-default-width

Default width for minipage and tabular* environments.

User Option: LaTeX-default-position

Default position string for array and tabular environments. If nil, act like the empty string is given, but don’t prompt for a position.

AUCTeX calculates the number of columns from the format string and inserts the suitable number of ampersands.

You can use C-c LFD (LaTeX-insert-item) to terminate rows in these environments. It supplies line break macro ‘\\’ and inserts the suitable number of ampersands on the next line. AUCTeX also supports the ‘*{num}{cols}’ notation (which may contain another ‘*’-expression) in the format string when calculating the number of ampersands. Please note that ‘num’ and ‘cols’ must be enclosed in braces; expressions like ‘*2l’ are not recognized correctly by the algorithm.

Command: LaTeX-insert-item

(C-c LFD) Close the current row with ‘\\’, move to the next line and insert an appropriate number of ampersands for the current environment.

Similar supports are provided for various amsmath environments such as ‘align’, ‘gather’, ‘alignat’, ‘matrix’ etc. Try typing C-c LFD in these environments. It recognizes the current environment and does the appropriate job depending on the context.


Previous: , Up: Environments   [Contents][Index]

2.4.5 Customizing Environments

See Adding Environments, for how to customize the list of known environments.


Next: , Previous: , Up: Editing   [Contents][Index]

2.5 Entering Mathematics

TeX is written by a mathematician, and has always contained good support for formatting mathematical text. AUCTeX supports this tradition, by offering a special minor mode for entering text with many mathematical symbols. You can enter this mode by typing C-c ~.

Command: LaTeX-math-mode

(C-c ~) Toggle LaTeX Math mode. This is a minor mode rebinding the key LaTeX-math-abbrev-prefix to allow easy typing of mathematical symbols. ` will read a character from the keyboard, and insert the symbol as specified in LaTeX-math-default and LaTeX-math-list. If given a prefix argument, the symbol will be surrounded by dollar signs.

You can use another prefix key (instead of `) by setting the variable LaTeX-math-abbrev-prefix.

To enable LaTeX Math mode by default, add the following in your .emacs file:

(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)
User Option: LaTeX-math-abbrev-prefix

A string containing the prefix of LaTeX-math-mode commands; This value defaults to `.

The string has to be a key or key sequence in a format understood by the kbd macro. This corresponds to the syntax usually used in the manuals for Emacs Emacs Lisp.

The variable LaTeX-math-list allows you to add your own mappings.

User Option: LaTeX-math-list

A list containing user-defined keys and commands to be used in LaTeX Math mode. Each entry should be a list of two to four elements.

First, the key to be used after LaTeX-math-abbrev-prefix for macro insertion. If it is nil, the symbol has no associated keystroke (it is available in the menu, though).

Second, a string representing the name of the macro (without a leading backslash.)

Third, a string representing the name of a submenu the command should be added to. Use a list of strings in case of nested menus.

Fourth, the position of a Unicode character to be displayed in the menu alongside the macro name. This is an integer value.

User Option: LaTeX-math-menu-unicode

Whether the LaTeX menu should try using Unicode for effect. Your Emacs built must be able to display include Unicode characters in menus for this feature.

AUCTeX’s reference card tex-ref.tex includes a list of all math mode commands.

AUCTeX can help you write subscripts and superscripts in math constructs by automatically inserting a pair of braces after typing _ or ^ respectively and putting point between the braces. In order to enable this feature, set the variable TeX-electric-sub-and-superscript to a non-nil value.

User Option: TeX-electric-sub-and-superscript

If non-nil, insert braces after typing ^ and _ in math mode.

You can automatically turn off input methods, used to input non-ascii characters, when you begin to enter math constructs.

User Option: TeX-math-input-method-off-regexp

Input method matching this regular expression is turned off when $ is typed to begin math mode or a math environment is inserted by C-c C-e (LaTeX-environment).


Next: , Previous: , Up: Editing   [Contents][Index]

2.6 Completion

Emacs lisp programmers probably know the lisp-complete-symbol command which was bound to M-TAB until completion-at-point became the new standard completion facility (see below). Users of the wonderful ispell mode know and love the ispell-complete-word command from that package. Similarly, AUCTeX has a TeX-complete-symbol command, by default bound to M-TAB which is equivalent to M-C-i. Using TeX-complete-symbol makes it easier to type and remember the names of long LaTeX macros.

In order to use TeX-complete-symbol, you should write a backslash and the start of the macro. Typing M-TAB will now complete as much of the macro, as it unambiguously can. For example, if you type ‘‘\renewc’’ and then M-TAB, it will expand to ‘‘\renewcommand’’. But there’s more: if point is just after ‘\begin{’, then TeX-complete-symbol will complete LaTeX environments, etc. This is controlled by TeX-complete-list.

Command: TeX-complete-symbol

(M-TAB) Complete TeX symbol before point.

Variable: TeX-complete-list

List of ways to complete the preceding text.

Each entry is a list with the following elements:

  1. Regexp matching the preceding text or a predicate of arity 0 which returns non-nil and sets ‘match-data’ appropriately if it is applicable.
  2. A number indicating the subgroup in the regexp containing the text.
  3. A function returning an alist of possible completions.
  4. Text to append after a succesful completion.

Or alternatively:

  1. Regexp matching the preceding text.
  2. Function to do the actual completion.

More recent Emacs versions have a new completion mechanism. Modes may define and register custom completion-at-point functions and when the user invokes completion-at-point (usually bound to M-TAB), all such registered functions are consulted for checking for possible completions. Modern completion UIs like company-mode support this completion-at-point facility.

Function: TeX--completion-at-point

AUCTeX’s completion-at-point function which is automatically added to completion-at-point-functions in TeX and LaTeX buffers.

It offers the same completion candidates as would TeX-complete-symbol (and is also controlled by TeX-complete-list) except that it doesn’t fall back on ispell-complete-word which would be awkward with completion UIs like company-mode.

A more direct way to insert a macro is with TeX-insert-macro, bound to C-c C-m which is equivalent to C-c RET. It has the advantage over completion that it knows about the argument of most standard LaTeX macros, and will prompt for them. It also knows about the type of the arguments, so it will for example give completion for the argument to ‘\include’. Some examples are listed below.

Command: TeX-insert-macro

(C-c C-m or C-c RET) Prompt (with completion) for the name of a TeX macro, and if AUCTeX knows the macro, prompt for each argument.

As a default selection, AUCTeX will suggest the macro last inserted or, as the first choice the value of the variable TeX-default-macro.

User Option: TeX-insert-macro-default-style

Specifies whether TeX-insert-macro will ask for all optional arguments.

If set to the symbol show-optional-args, TeX-insert-macro asks for optional arguments of TeX marcos, unless the previous optional argument has been rejected. If set to show-all-optional-args, TeX-insert-macro asks for all optional arguments. mandatory-args-only, TeX-insert-macro asks only for mandatory arguments. When TeX-insert-macro is called with prefix argument (C-u), it’s the other way round.

Note that for some macros, there are special mechanisms, e.g. LaTeX-includegraphics-options-alist and TeX-arg-cite-note-p.

User Option: TeX-default-macro

Default macro to insert when invoking TeX-insert-macro first time.

A faster alternative is to enable the option TeX-electric-escape.

User Option: TeX-electric-escape

If this is non-nil, typing the TeX escape character \ will invoke the command TeX-electric-macro.

In Texinfo mode, the command is invoked by @ instead.

The difference between TeX-insert-macro and TeX-electric-macro is that space will complete and exit from the minibuffer in TeX-electric-macro. Use TAB if you merely want to complete.

Command: TeX-electric-macro

Prompt (with completion) for the name of a TeX macro, and if AUCTeX knows the macro, prompt for each argument. Space will complete and exit.

By default AUCTeX will put an empty set braces ‘{}’ after a macro with no arguments to stop it from eating the next whitespace. This is suppressed inside math mode and can be disabled totally by setting TeX-insert-braces to nil.

User Option: TeX-insert-braces

If non-nil, append a empty pair of braces after inserting a macro with no arguments.

User Option: TeX-insert-braces-alist

Control the insertion of a pair of braces after a macro on a per macro basis.

This variable is an alist. Each element is a cons cell, whose car is the macro name, and the cdr is non-nil or nil, depending on whether a pair of braces should be, respectively, appended or not to the macro.

If a macro has an element in this variable, TeX-parse-macro will use its value to decide what to do, whatever the value of the variable TeX-insert-braces.

Completions work because AUCTeX can analyze TeX files, and store symbols in Emacs Lisp files for later retrieval. See Automatic, for more information.

AUCTeX distinguishes normal and expert macros. By default, it will offer completion only for normal commands. This behavior can be controlled using the user option TeX-complete-expert-commands.

User Option: TeX-complete-expert-commands

Complete macros and environments marked as expert commands.

Possible values are nil, t, or a list of style names.

nil

Don’t complete expert commands (default).

t

Always complete expert commands.

(STYLES …)

Only complete expert commands of STYLES.

AUCTeX will also make completion for many macro arguments, for example existing labels when you enter a ‘\ref’ macro with TeX-insert-macro or TeX-electric-macro, and BibTeX entries when you enter a ‘\cite’ macro. For this kind of completion to work, parsing must be enabled as described in see Parsing Files. For ‘\cite’ you must also make sure that the BibTeX files have been saved at least once after you enabled automatic parsing on save, and that the basename of the BibTeX file does not conflict with the basename of one of TeX files.


Next: , Previous: , Up: Editing   [Contents][Index]

2.7 Marking Environments, Sections, or Texinfo Nodes

You can mark the current environment by typing C-c ., or the current section by typing C-c *.

In Texinfo documents you can type M-C-h to mark the current node.

When the region is set, the point is moved to its beginning and the mark to its end.


Next: , Up: Marking   [Contents][Index]

2.7.1 LaTeX Commands for Marking Environments and Sections

Command: LaTeX-mark-section

(C-c *) Set mark at end of current logical section, and point at top.

With a non-nil prefix argument, mark only the region from the current section start to the next sectioning command. Thereby subsections are not being marked. Otherwise, any included subsections are also marked along with current section.

Command: LaTeX-mark-environment

(C-c .) Set mark to the end of the current environment and point to the matching beginning.

If a prefix argument is given, mark the respective number of enclosing environments. The command will not work properly if there are unbalanced begin-end pairs in comments and verbatim environments.


Previous: , Up: Marking   [Contents][Index]

2.7.2 Texinfo Commands for Marking Environments and Sections

Command: Texinfo-mark-section

(C-c *) Mark the current section, with inclusion of any containing node.

The current section is detected as starting by any of the structuring commands matched by the regular expression in the variable outline-regexp which in turn is a regular expression matching any element of the variable texinfo-section-list.

With a non-nil prefix argument, mark only the region from the current section start to the next sectioning command. Thereby subsections are not being marked. Otherwise, any included subsections are also marked

Note that when the current section is starting immediately after a node command, then the node command is also marked as part of the section.

Command: Texinfo-mark-environment

(C-c .) Set mark to the end of the current environment and point to the matching beginning.

If a prefix argument is given, mark the respective number of enclosing environments. The command will not work properly if there are unbalanced begin-end pairs in comments and verbatim environments.

Command: Texinfo-mark-node

(M-C-h) Mark the current node. This is the node in which point is located. It is starting at the previous occurrence of the keyword @node and ending at next occurrence of the keywords @node or @bye.


Next: , Previous: , Up: Editing   [Contents][Index]

2.8 Commenting

It is often necessary to comment out temporarily a region of TeX or LaTeX code. This can be done with the commands C-c ; and C-c %. C-c ; will comment out all lines in the current region, while C-c % will comment out the current paragraph. Type C-c ; again to uncomment all lines of a commented region, or C-c % again to uncomment all comment lines around point. These commands will insert or remove a single ‘%’ respectively.

Command: TeX-comment-or-uncomment-region

(C-c ;) Add or remove ‘%’ from the beginning of each line in the current region. Uncommenting works only if the region encloses solely commented lines. If AUCTeX should not try to guess if the region should be commented or uncommented the commands TeX-comment-region and TeX-uncomment-region can be used to explicitly comment or uncomment the region in concern.

Command: TeX-comment-or-uncomment-paragraph

(C-c %) Add or remove ‘%’ from the beginning of each line in the current paragraph. When removing ‘%’ characters the paragraph is considered to consist of all preceding and succeeding lines starting with a ‘%’, until the first non-comment line.


Next: , Previous: , Up: Editing   [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:

TAB

LaTeX-indent-line will indent the current line.

LFD

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.

C-j

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.


Previous: , Up: Editing   [Contents][Index]

2.10 Filling

Filling deals with the insertion of line breaks to prevent lines from becoming wider than what is specified in fill-column. The linebreaks will be inserted automatically if auto-fill-mode is enabled. In this case the source is not only filled but also indented automatically as you write it.

auto-fill-mode can be enabled for AUCTeX by calling turn-on-auto-fill in one of the hooks AUCTeX is running. See Modes and Hooks. As an example, if you want to enable auto-fill-mode in LaTeX-mode, put the following into your init file:

(add-hook 'LaTeX-mode-hook 'turn-on-auto-fill)

You can manually fill explicitely marked regions, paragraphs, environments, complete sections, or the whole buffer. (Note that manual filling in AUCTeX will indent the start of the region to be filled in contrast to many other Emacs modes.)

There are some syntactical constructs which are handled specially with regard to filling. These are so-called code comments and paragraph commands.

Code comments are comments preceded by code or text in the same line. Upon filling a region, code comments themselves will not get filled. Filling is done from the start of the region to the line with the code comment and continues after it. In order to prevent overfull lines in the source code, a linebreak will be inserted before the last non-comment word by default. This can be changed by customizing LaTeX-fill-break-before-code-comments. If you have overfull lines with code comments you can fill those explicitely by calling LaTeX-fill-paragraph or pressing M-q with the cursor positioned on them. This will add linebreaks in the comment and indent subsequent comment lines to the column of the comment in the first line of the code comment. In this special case M-q only acts on the current line and not on the whole paragraph.

Lines with ‘\par’ are treated similarly to code comments, i.e. ‘\par’ will be treated as paragraph boundary which should not be followed by other code or text. But it is not treated as a real paragraph boundary like an empty line where filling a paragraph would stop.

Paragraph commands like ‘\section’ or ‘\noindent’ (the list of commands is defined by LaTeX-paragraph-commands) are often to be placed in their own line(s). This means they should not be consecuted with any preceding or following adjacent lines of text. AUCTeX will prevent this from happening if you do not put any text except another macro after the end of the last brace of the respective macro. If there is other text after the macro, AUCTeX regards this as a sign that the macro is part of the following paragraph.

Here are some examples:

\begin{quote}
  text text text text
\begin{quote}\label{foo}
  text text text text

If you press M-q on the first line in both examples, nothing will change. But if you write

\begin{quote} text
  text text text text

and press M-q, you will get

\begin{quote} text text text text text

Besides code comments and paragraph commands, another speciality of filling in AUCTeX involves commented lines. You should be aware that these comments are treated as islands in the rest of the LaTeX code if syntactic filling is enabled. This means, for example, if you try to fill an environment with LaTeX-fill-environment and have the cursor placed on a commented line which does not have a surrounding environment inside the comment, AUCTeX will report an error.

The relevant commands and variables with regard to filling are:

C-c C-q C-p

LaTeX-fill-paragraph will fill and indent the current paragraph.

M-q

Alias for C-c C-q C-p

C-c C-q C-e

LaTeX-fill-environment will fill and indent the current environment. This may e.g. be the ‘document’ environment, in which case the entire document will be formatted.

C-c C-q C-s

LaTeX-fill-section will fill and indent the current logical sectional unit.

C-c C-q C-r

LaTeX-fill-region will fill and indent the current region.

User Option: LaTeX-fill-break-at-separators

List of separators before or after which respectively linebreaks will be inserted if they do not fit into one line. The separators can be curly braces, brackets, switches for inline math (‘$’, ‘\(’, ‘\)’) and switches for display math (‘\[’, ‘\]’). Such formatting can be useful to make macros and math more visible or to prevent overfull lines in the LaTeX source in case a package for displaying formatted TeX output inside the Emacs buffer, like preview-latex, is used.

User Option: LaTeX-fill-break-before-code-comments

Code comments are comments preceded by some other text in the same line. When a paragraph containing such a comment is to be filled, the comment start will be seen as a border after which no line breaks will be inserted in the same line. If the option LaTeX-fill-break-before-code-comments is enabled (which is the default) and the comment does not fit into the line, a line break will be inserted before the last non-comment word to minimize the chance that the line becomes overfull.

User Option: LaTeX-fill-excluded-macros

A list of macro names (without leading backslash) for whose arguments filling should be disabled. Typically, you will want to add macros here which have long, multi-line arguments. An example is \pgfplotstabletypeset from the pgfplotstable package which is used as shown in the following listing:

\pgfplotstabletypeset[skip first n=4]{%
  XYZ Format,
  Version 1.234
  Date 2010-09-01
  @author Mustermann
  A B C
  1 2 3
  4 5 6
}

Previous: , Up: Editing   [Contents][Index]