Go to the first, previous, next, last section, table of contents.

Advanced Editing Features

The previous chapter described how to write the main body of the text easily and with a minimum of errors. In this chapter we will describe some features for entering more specialized sorts of text, and for indenting and navigating through the document.

Entering Mathematics

TeX is written by a mathematician, and has always contained good support for formatting mathematical text. AUC TeX supports this tradition, by offering a special minor mode for entering text with many mathematic 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-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.

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

The variable LaTeX-math-list holds the actual mapping.

User Option: LaTeX-math-list
A list containing key command mappings to use in LaTeX-math-mode. The car of each element is the key and the cdr is the macro name.

The AUC TeX distributions includes a reference card for LaTeX-math-mode with a list of all math mode commands.

Completion

Emacs lisp programmers probably know the lisp-complete-symbol command, usually bound to M-TAB. Users of the wonderful ispell mode know and love the ispell-complete-word command from that package. Similarly, AUC TeX has a TeX-complete-symbol command, usually bound to M-TAB. Using LaTeX-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''.

Command: TeX-complete-symbol
(M-TAB) Complete TeX symbol before point.

A more direct way to insert a macro is with TeX-insert-macro, bound to C-c C-m. 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) Prompt (with completion) for the name of a TeX macro, and if AUC TeX knows the macro, prompt for each argument.

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

User Option: TeX-default-macro
Default macro to insert when invoking TeX-insert-macro first time.

A faster alternative is to bind the function TeX-electric-macro to `\'. This can be done by setting the variable TeX-electric-escape

User Option: TeX-electric-escape
If this is non-nil when AUC TeX is loaded, the TeX escape character `\' will be bound to TeX-electric-macro

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 AUC TeX knows the macro, prompt for each argument. Space will complete and exit.

By default AUC TeX will put an empty set braces `{}' after a macro with no arguments to stop it from eating the next whitespace. This can be stopped by entering LaTeX-math-mode, see section Entering Mathematics, or 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.

Completions work because AUC TeX can analyze TeX files, and store symbols in emacs lisp files for later retrieval. See section Automatic Customization, for more information.

AUC TeX 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 section Automatic Parsing of TeX 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.

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. To uncomment, simply type C-u - C-c ; to uncomment all lines in the region, or C-u - C-c % uncomment all comment lines around point.

By default, these commands will insert or remove a single `%'. To insert more than one, give an argument. C-u 5 C-c % will add five `%' to each line, while C-u - 5 C-c % will remove up to 5 `%' from each line.

Command: TeX-comment-region count
(C-c ;) Add or remove `%' from the beginning of each line in the current region, as specified by count.

Command: TeX-comment-paragraph count
(C-c %) Add or remove `%' from the beginning of each line in the current paragraph, as specified by count. When removing `%''s the paragraph is considered to consist of all preceding and succeeding lines starting with a `%', until the first non-comment line.

Marking, Formatting and Indenting

AUC TeX contains very advanced handling of indentation and reformatting of the LaTeX source. If you have already tried AUC TeX with auto-fill-mode enabled, you may have noted that the source is automatically indented and formatted as you write it. More over, AUC TeX is able to format sections of text on demand.

It is important to realize, that AUC TeX comes with `formatting' in two fashions. Either letting TeX format the file, or letting AUC TeX make the ASCII document look better.

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. This indentation makes it easier to see the structure of the document, and to catch errors such as a missing close brace. Thus, the indentation is done for precisely the same reasons that you would indent ordinary computer programs.

The following is a short sample of an itemize environment indented by AUC TeX. If more environment are nested, they are indented `accumulated' just like most programming languages usually are seen indented in nested constructs.

\begin{itemize}
\item Insertion of templates for logical-structural compositions such as
  environments and sections.
\item Hot-keys for easy access to certain often used constructs, e.g.,
  font changes, accented letters, and mathematical symbols.
\item Running application programs (such as \TeX), and then parsing
  the output so that errors in the document may be located 
  easily.
\item Support for multi-file documents.
\item Online help for \AllTeX\ error messages.
\item Outlining\Dash i.e., manipulating the document as a composition
  of nested/sequential logical constructs.
\item Instant formatting and indentation of the \ascii-document in
  order to make it easier to read.
\item `Completion' (and thereby spell-checking) of partially written
  control sequences.
\end{itemize}

You can format and indent single lines, paragraphs, environments, or sections.

TAB
LaTeX-indent-line will indent the current line.
LFD
reindent-then-newline-and-indent indents the current line, and then inserts a new line (much like RET) and move the cursor to an appropriate position by the left margin.
M-q
Alias for C-c C-q C-p
C-c C-q C-p
LaTeX-fill-paragraph will reformat or `fill' the current paragraph.
C-c C-q C-e
LaTeX-fill-environment will reformat or `fill' 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 reformat or `fill' the current logical sectional unit.
M-g
Alias for C-c C-q C-r
C-c C-q C-r
LaTeX-fill-region will format or `fill' the current region.

Warning: The formatting cannot handle tabular-like environments. Those will be completely messed-up if you try to format them.

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 `}'.

Outlining the Document

GNU Emacs earlier than version 19.19 does not have a useful outline mode. If you want to use outlines with old versions of emacs, please get the file `outln-18.el' from `sunsite.auc.dk:/packages/auctex/outln-18.el'. It is a port of the Emacs 19.19 outline mode to Emacs 18 and Lucid Emacs.

AUC TeX supports the standard outline minor mode using LaTeX sectioning commands as header lines. See section `Outline Mode' in GNU Emacs Manual. By default outline-minor-mode will use the prefix key C-c which is also used by AUC TeX, so it is suggested that you choose another prefix key by inserting

  (setq outline-minor-mode-prefix "\C-c\C-o") ; Or whatever...

in your `.emacs' file.

You can add your own headings by setting the variable TeX-outline-extra.

Variable: TeX-outline-extra
List of extra TeX outline levels.

Each element is a list with two entries. The first entry is the regular expression matching a header, and the second is the level of the header. A `^' is automatically prepended to the regular expressions in the list, so they must match text at the beginning of the line.

See LaTeX-section-list for existing header levels.

The following example add `\item' and `\bibliography' headers, with `\bibliography' at the same outline level as `\section', and `\item' being below `\subparagraph'.

(setq TeX-outline-extra
      '(("[ \t]*\\\\\\(bib\\)?item\\b" 7)
        ("\\\\bibliography\\b" 2)))

You may want to check out the unbundled `out-xtra' package for even better outline support. It is available from your favorite emacs lisp archive.


Go to the first, previous, next, last section, table of contents.