Next: Processing, Previous: Editing, Up: top [Contents][Index]
It is often desirable to get visual help of what markup code in a text actually does without having to decipher it explicitly. For this purpose Emacs and AUCTeX provide font locking (also known as syntax highlighting) which visually sets off markup code like macros or environments by using different colors or fonts. For example text to be typeset in italics can be displayed with an italic font in the editor as well, or labels and references get their own distinct color.
While font locking helps you grasp the purpose of markup code and separate markup from content, the markup code can still be distracting. AUCTeX lets you hide those parts and show them again at request with its built-in support for hiding macros and environments which we call folding here.
Besides folding of macros and environments, AUCTeX provides support for Emacs’ outline mode which lets you narrow the buffer content to certain sections of your text by hiding the parts not belonging to these sections.
Moreover, you can focus in a specific portion of the code by narrowing the buffer to the desired region. AUCTeX provides also functions to narrow the buffer to the current group and to LaTeX environments.
AUCTeX also provides some WYSIWYG features.
First, you can customize font-latex-fontify-script
to enable
special formatting of ^
superscripts and _
subscripts
(see Font Locking).
Secondly, AUCTeX with GNU Emacs 25 or later can display certain math
macros using Unicode characters, e.g., \alpha
as α. This is
called prettification and is lightweight and reasonable robust
(see Prettifying).
A more accurate approach is provided by preview-latex, a subsystem of AUCTeX, see Introduction in The preview-latex Manual. This system uses LaTeX to generate images that are then displayed in your buffer. It is extremely accurate but can be fragile with some packages (like older pgf versions).
Please note that you can use prettification and preview-latex together.
• Font Locking | Font Locking | |
• Folding | Folding Macros and Environments | |
• Outline | Outlining the Document | |
• Narrowing | Restricting display and editing to a portion of the buffer | |
• Prettifying | Displaying Greek and math macros as Unicode characters |
Font locking is supposed to improve readability of the source code by highlighting certain keywords with different colors or fonts. It thereby lets you recognize the function of markup code to a certain extent without having to read the markup command. For general information on controlling font locking with Emacs’ Font Lock mode, see Font Lock Mode in GNU Emacs Manual.
Once font locking is enabled globally or for the major modes provided by
AUCTeX, the font locking patterns and functionality of font-latex
are activated by default. You can switch to a different font locking
scheme or disable font locking in AUCTeX by customizing the variable
TeX-install-font-lock
.
Besides font-latex AUCTeX ships with a scheme which is derived
from Emacs’ default LaTeX mode and activated by choosing
tex-font-setup
. Be aware that this scheme is not coupled with
AUCTeX’s style system and not the focus of development. Therefore
and due to font-latex being much more feature-rich the following
explanations will only cover font-latex.
In case you want to hook in your own fontification scheme, you can
choose other
and insert the name of the function which sets up
your font locking patterns. If you want to disable fontification in
AUCTeX completely, choose ignore
.
font-latex provides many options for customization which are accessible with M-x customize-group RET font-latex RET. For this description the various options are explained in conceptional groups.
• Fontification of macros | Fontification of macros | |
• Fontification of quotes | Fontification of quotes | |
• Fontification of math | Fontification of math constructs | |
• Verbatim content | Verbatim macros and environments | |
• Faces | Faces used by font-latex | |
• Known problems | Known fontification problems |
Next: Fontification of quotes, Up: Font Locking [Contents][Index]
Highlighting of macros can be customized by adapting keyword lists which
can be found in the customization group font-latex-keywords
.
Three types of macros can be handled differently with respect to fontification:
font-lock-keyword-face
will be used and for the optional
arguments the face font-lock-variable-name-face
. The face
applied to the mandatory argument depends on the macro class represented
by the respective built-in variables.
font-lock-keyword-face
and the text will get
the face configured for the respective macro class. If no TeX group
is present, the latter face will be applied to the macro itself.
Customization variables for ‘\foo[bar]{baz}’ type macros allow both the macro name and the sequence of arguments to be specified. The latter is done with a string which can contain the characters
indicating the existence of a starred variant for the macro,
for optional arguments in brackets,
for mandatory arguments in braces,
for mandatory arguments consisting of a single macro and
as a prefix indicating that two alternatives are following.
For example the specifier for ‘\documentclass’ would be ‘[{’ because the macro has one optional followed by one mandatory argument. The specifier for ‘\newcommand’ would be ‘*|{\[[{’ because there is a starred variant, the mandatory argument following the macro name can be a macro or a TeX group which can be followed by two optional arguments and the last token is a mandatory argument in braces.
Customization variables for the ‘{\foo text}’ and ‘\foo’ types are simple lists of strings where each entry is a macro name (without the leading backslash).
font-latex provides keyword lists for different macro classes which are described in the following table:
font-latex-match-function-keywords
Keywords for macros defining or related to functions, like
‘\newcommand’.
Type: ‘\macro[...]{...}’
Face: font-lock-function-name-face
font-latex-match-reference-keywords
Keywords for macros defining or related to references, like
‘\ref’.
Type: ‘\macro[...]{...}’
Face: font-lock-constant-face
font-latex-match-textual-keywords
Keywords for macros specifying textual content, like ‘\caption’.
Type: ‘\macro[...]{...}’
Face: font-lock-type-face
font-latex-match-variable-keywords
Keywords for macros defining or related to variables, like
‘\setlength’.
Type: ‘\macro[...]{...}’
Face: font-lock-variable-name-face
font-latex-match-warning-keywords
Keywords for important macros, e.g. affecting line or page break, like
‘\clearpage’.
Type: ‘\macro’
Face: font-latex-warning-face
Sectioning commands are macros like ‘\chapter’ or ‘\section’.
For these commands there are two fontification schemes which may be
selected by customizing the variable font-latex-fontify-sectioning
.
Per default sectioning commands will be shown in a larger, proportional
font, which corresponds to a number for this variable. The font size
varies with the sectioning level, e.g. ‘\part’
(font-latex-sectioning-0-face
) has a larger font than
‘\paragraph’ (font-latex-sectioning-5-face
). Typically,
values from 1.05 to 1.3 for font-latex-fontify-sectioning
give
best results, depending on your font setup. If you rather like to use
the base font and a different color, set the variable to the symbol
‘color’. In this case the face font-lock-type-face
will be
used to fontify the argument of the sectioning commands.
You can make font-latex aware of your own sectioning commands be
adding them to the keyword lists:
font-latex-match-sectioning-0-keywords
(font-latex-sectioning-0-face
) …
font-latex-match-sectioning-5-keywords
(font-latex-sectioning-5-face
).
Related to sectioning there is special support for slide titles which
may be fontified with the face font-latex-slide-title-face
. You
can add macros which should appear in this face by customizing the
variable font-latex-match-slide-title-keywords
.
LaTeX provides various macros for changing fonts or font attributes. For example, you can select an italic font with ‘\textit{...}’ or bold with ‘\textbf{...}’. An alternative way to specify these fonts is to use special macros in TeX groups, like ‘{\itshape ...}’ for italics and ‘{\bfseries ...}’ for bold. As mentioned above, we call the former variants commands and the latter declarations.
Besides the macros for changing fonts provided by LaTeX there is an infinite number of other macros—either defined by yourself for logical markup or defined by macro packages—which affect the font in the typeset text. While LaTeX’s built-in macros and macros of packages known by AUCTeX are already handled by font-latex, different keyword lists per type style and macro type are provided for entering your own macros which are listed in the table below.
font-latex-match-bold-command-keywords
Keywords for commands specifying a bold type style.
Face: font-latex-bold-face
font-latex-match-italic-command-keywords
Keywords for commands specifying an italic font.
Face: font-latex-italic-face
font-latex-match-math-command-keywords
Keywords for commands specifying a math font.
Face: font-latex-math-face
font-latex-match-type-command-keywords
Keywords for commands specifying a typewriter font.
Face: font-lock-type-face
font-latex-match-bold-declaration-keywords
Keywords for declarations specifying a bold type style.
Face: font-latex-bold-face
font-latex-match-italic-declaration-keywords
Keywords for declarations specifying an italic font.
Face: font-latex-italic-face
font-latex-match-type-declaration-keywords
Keywords for declarations specifying a typewriter font.
Face: font-latex-type-face
font-latex ships with predefined lists of keywords for the classes
described above. You can disable these defaults per class by
customizing the variable font-latex-deactivated-keyword-classes
.
This is a list of strings for keyword classes to be deactivated. Valid
entries are "warning", "variable", "biblatexnoarg", "biblatex",
"reference", "function" , "sectioning-0", "sectioning-1",
"sectioning-2", "sectioning-3", "sectioning-4", "sectioning-5",
"slide-title", "textual", "bold-command", "italic-command",
"math-command", "type-command", "bold-declaration",
"italic-declaration", "type-declaration".
You can also get rid of certain keywords only. For example if you want to remove highlighting of footnotes as references you can put the following stanza into your init file:
(eval-after-load "font-latex" '(setq-default font-latex-match-reference-keywords-local (remove (assoc-string "footnote" font-latex-match-reference-keywords-local) font-latex-match-reference-keywords-local)))
But note that this means fiddling with font-latex’s internals and is not guaranteed to work in future versions of font-latex.
In case the customization options explained above do not suffice for
your needs, you can specify your own keyword classes by customizing the
variable font-latex-user-keyword-classes
.
Every keyword class consists of four parts, a name, a list of keywords, a face and a specifier for the type of macros to be highlighted.
When adding new entries, you have to use unique values for the class names, i.e. they must not clash with names of the built-in keyword classes or other names given by you. Additionally the names must not contain spaces.
The list of keywords defines which commands and declarations should be covered by the keyword class. A keyword can either be a simple command name omitting the leading backslash or a list consisting of the command name and a string specifying the sequence of arguments for the command.
The face argument can either be an existing face or face attributes made by you.
There are three alternatives for the type of keywords—“Command with arguments”, “Declaration inside TeX group” and “Command without arguments”—which correspond with the macro types explained above.
Next: Fontification of math, Previous: Fontification of macros, Up: Font Locking [Contents][Index]
Text in quotation marks is displayed with the face
font-latex-string-face
. Besides the various forms of opening and
closing double and single quotation marks, so-called guillemets (<<, >>)
can be used for quoting. Because there are two styles of using
them—French style: << text >>; German style: >>text<<—you can
customize the variable font-latex-quotes
to tell font-latex
which type you are using if the correct value cannot be derived from
document properties.
The default value of font-latex-quotes
is ‘auto’ which means
that font-latex will try to derive the correct type of quotation mark
matching from document properties like the language option supplied to
the babel LaTeX package.
If the automatic detection fails for you and you mostly use one specific style you can set it to a specific language-dependent value as well. Set the value to ‘german’ if you are using >>German quotes<< and to ‘french’ if you are using << French quotes >>. font-latex will recognize the different ways these quotes can be given in your source code, i.e. (‘"<’, ‘">’), (‘<<’, ‘>>’) and the respective 8-bit variants.
If you set font-latex-quotes
to nil, quoted content will not be
fontified.
Next: Verbatim content, Previous: Fontification of quotes, Up: Font Locking [Contents][Index]
In LaTeX mathematics can be indicated by a variety of different
methods: toggles (like dollar signs), macros and environments. Math
constructs known by font-latex are displayed with the face
font-latex-math-face
. Support for dollar signs and shorthands
like ‘\(...\)’ or ‘\[...\]’ is built-in and not customizable.
Support for other math macros and environments can be adapted by
customizing the variables font-latex-match-math-command-keywords
and font-latex-math-environments
respectively.
In order to make math constructs more readable, font-latex displays
subscript and superscript parts in a smaller font and raised or lowered
respectively. This fontification feature can be controlled with the
variables font-latex-fontify-script
and
font-latex-script-display
.
If non-nil, fontify subscript and superscript strings. Concretely, this means that the scripts are raised or lowered.
Another possiblity is setting this variable to the symbol
multi-level
. In this case, in a formula x^{y^z}, y is
raised above and smaller than x, and z is raised above and
smaller than y. With many script levels, the text might become too
small to be readable. (See font-latex-fontify-script-max-level
below.)
Lastly, you can set this variable to invisible
whose behavior is
like multi-level
, and in addition the super-/subscript characters
^ and _ are not displayed.
Maximum scriptification level for which script faces are applied.
The faces font-latex-superscript-face
and
font-latex-subscript-face
define custom :height
values <
1.0. Therefore, scripts are displayed with a slightly smaller font than
normal math text. If font-latex-fontify-script
is
multi-level
or invisible
, the font size becomes too small
to be readable after a few levels. This option allows to specify the
maximum level after which the size of the script text won’t be shrunken
anymore.
For example, in the expression x^{y^{z^a_b}}, x has scriptification level 0, y has level 1, z has level 2, and both a and b have scriptification level 3.
If font-latex-fontify-script-max-level
was 2, then z, a,
and b would have the same font size. If it was 3 or more, then
a and b were smaller than z just in the same way as z is
smaller than y and y is smaller than x.
The script characters ‘^’ and ‘_’ themselves are also
fontified with an own face named font-latex-script-char-face
.
Display specification for subscript and superscript content. The car is used for subscript, the cdr is used for superscript. The feature is implemented using so-called display properties. For information on what exactly to specify for the values, see Other Display Specifications in GNU Emacs Lisp Reference Manual.
Next: Faces, Previous: Fontification of math, Up: Font Locking [Contents][Index]
Usually it is not desirable to have content to be typeset verbatim
highlighted according to LaTeX syntax. Therefore this content will
be fontified uniformly with the face font-latex-verbatim-face
.
font-latex differentiates three different types of verbatim
constructs for fontification. Macros with special characters like | as
delimiters, macros with braces, and environments. Which macros and
environments are recognized is controlled by the variables
LaTeX-verbatim-macros-with-delims
,
LaTeX-verbatim-macros-with-braces
, and
LaTeX-verbatim-environments
respectively.
Next: Known problems, Previous: Verbatim content, Up: Font Locking [Contents][Index]
In case you want to change the colors and fonts used by font-latex please refer to the faces mentioned in the explanations above and use M-x customize-face RET <face> RET. All faces defined by font-latex are accessible through a customization group by typing M-x customize-group RET font-latex-highlighting-faces RET.
Previous: Faces, Up: Font Locking [Contents][Index]
In certain cases the fontification machinery fails to interpret buffer contents correctly. This can lead to color bleed, i.e. large parts of a buffer get fontified with an inappropriate face. A typical situation for this to happen is the use of a dollar sign (‘$’) in a verbatim macro or environment. If font-latex is not aware of the verbatim construct, it assumes the dollar sign to be a toggle for mathematics and fontifies the following buffer content with the respective face until it finds a closing dollar sign or till the end of the buffer.
As a remedy you can make the verbatim construct known to font-latex, see Verbatim content. If this is not possible, you can insert a commented dollar sign (‘%$’) at the next suitable end of line as a quick workaround.
Next: Outline, Previous: Font Locking, Up: Display [Contents][Index]
A popular complaint about markup languages like TeX and LaTeX is that there is too much clutter in the source text and that one cannot focus well on the content. There are macros where you are only interested in the content they are enclosing, like font specifiers where the content might already be fontified in a special way by font locking. Or macros the content of which you only want to see when actually editing it, like footnotes or citations. Similarly you might find certain environments or comments distracting when trying to concentrate on the body of your document.
With AUCTeX’s folding functionality you can collapse those items and replace them by a fixed string, the content of one of their arguments, or a mixture of both. If you want to make the original text visible again in order to view or edit it, move point sideways onto the placeholder (also called display string) or left-click with the mouse pointer on it. (The latter is currently only supported on Emacs.) The macro or environment will unfold automatically, stay open as long as point is inside of it and collapse again once you move point out of it. (Note that folding of environments currently does not work in every AUCTeX mode.)
In order to use this feature, you have to activate TeX-fold-mode
which will activate the auto-reveal feature and the necessary commands
to hide and show macros and environments. You can activate the mode in
a certain buffer by typing the command M-x TeX-fold-mode RET or
using the keyboard shortcut C-c C-o C-f. If you want to use it
every time you edit a LaTeX document, add it to a hook:
(add-hook 'LaTeX-mode-hook (lambda () (TeX-fold-mode 1)))
If it should be activated in all AUCTeX modes, use
TeX-mode-hook
instead of LaTeX-mode-hook
.
Once the mode is active there are several commands available to hide and show macros, environments and comments:
(C-c C-o C-b) Hide all foldable items in the current buffer
according to the setting of TeX-fold-type-list
.
If you want to have this done automatically every time you open a file, add it to a hook and make sure the function is called after font locking is set up for the buffer. The following code should accomplish this:
(add-hook 'find-file-hook 'TeX-fold-buffer t)
The command can be used any time to refresh the whole buffer and fold any new macros and environments which were inserted after the last invocation of the command.
List of symbols determining the item classes to consider for folding. This can be macros, environments and comments. Per default only macros and environments are folded.
In order for all folded content to get the right faces, the whole buffer
has to be fontified before folding is carried out.
TeX-fold-buffer
therefore will force fontification of unfontified
regions. As this will prolong the time folding takes, you can prevent
forced fontification by customizing the variable
TeX-fold-force-fontify
.
By default, a macro inserted with TeX-insert-macro
(C-c
C-m) will not be folded. Set this variable to a non-nil value to
aumatically fold macros as soon as they are inserted.
By default items found in comments will be folded. If your comments often contain unfinished code this might lead to problems. Give this variable a non-nil value and foldable items in your comments will be left alone.
When this variable is non-nil and there is an active regione, text around the mark will be kept unfolded.
(C-c C-o C-r) Hide all configured macros in the marked region.
(C-c C-o C-p) Hide all configured macros in the paragraph containing point.
(C-c C-o C-m) Hide the macro on which point currently is located.
If the name of the macro is found in TeX-fold-macro-spec-list
,
the respective display string will be shown instead. If it is not
found, the name of the macro in sqare brackets or the default string for
unspecified macros (TeX-fold-unspec-macro-display-string
) will be
shown, depending on the value of the variable
TeX-fold-unspec-use-name
.
(C-c C-o C-e) Hide the environment on which point currently is
located. The behavior regarding the display string is analogous to
TeX-fold-macro
and determined by the variables
TeX-fold-env-spec-list
and
TeX-fold-unspec-env-display-string
respectively.
Hide the math macro on which point currently is located. If the name of
the macro is found in TeX-fold-math-spec-list
, the respective
display string will be shown instead. If it is not found, the name of
the macro in sqare brackets or the default string for unspecified macros
(TeX-fold-unspec-macro-display-string
) will be shown, depending
on the value of the variable TeX-fold-unspec-use-name
.
(C-c C-o C-c) Hide the comment point is located on.
(C-c C-o b) Permanently unfold all macros and environments in the current buffer.
(C-c C-o r) Permanently unfold all macros and environments in the marked region.
(C-c C-o p) Permanently unfold all macros and environments in the paragraph containing point.
(C-c C-o i) Permanently show the macro or environment on which point currently is located. In contrast to temporarily opening the macro when point is moved sideways onto it, the macro will be permanently unfolded and will not collapse again once point is leaving it.
(C-c C-o C-o) Hide or show items according to the current context. If there is folded content, unfold it. If there is a marked region, fold all configured content in this region. If there is no folded content but a macro or environment, fold it.
In case you want to use a different prefix than C-c C-o for these
commands you can customize the variable TeX-fold-command-prefix
.
(Note that this will not change the key binding for activating the
mode.)
The commands above will only take macros or environments into
consideration which are specified in the variables
TeX-fold-macro-spec-list
or TeX-fold-env-spec-list
respectively.
List of replacement specifiers and macros to fold. The specifier can be a string, an integer or a function symbol.
If you specify a string, it will be used as a display replacement for the whole macro. Numbers in braces, brackets, parens or angle brackets will be replaced by the respective macro argument. For example ‘{1}’ will be replaced by the first mandatory argument of the macro. One can also define alternatives within the specifier which are used if an argument is not found. Alternatives are separated by ‘||’. They are most useful with optional arguments. As an example, the default specifier for ‘\item’ is ‘[1]:||*’ which means that if there is an optional argument, its value is shown followed by a colon. If there is no optional argument, only an asterisk is used as the display string.
If you specify a number as the first element, the content of the respective mandatory argument of a LaTeX macro will be used as the placeholder.
If the first element is a function symbol, the function will be called with all mandatory arguments of the macro and the result of the function call will be used as a replacement for the macro.
The placeholder is made by copying the text from the buffer together with
its properties, i.e. its face as well. If fontification has not
happened when this is done (e.g. because of lazy font locking) the
intended fontification will not show up. As a workaround you can leave
Emacs idle a few seconds and wait for stealth font locking to finish
before you fold the buffer. Or you just re-fold the buffer with
TeX-fold-buffer
when you notice a wrong fontification.
List of display strings or argument numbers and environments to fold. Argument numbers refer to the ‘\begin’ statement. That means if you have e.g. ‘\begin{tabularx}{\linewidth}{XXX} ... \end{tabularx}’ and specify 3 as the argument number, the resulting display string will be “XXX”.
List of display strings and math macros to fold.
The variables TeX-fold-macro-spec-list
,
TeX-fold-env-spec-list
, and TeX-fold-math-spec-list
apply
to any AUCTeX mode. If you want to make settings which are only
applied to LaTeX mode, you can use the mode-specific variables
LaTeX-fold-macro-spec-list
, LaTeX-fold-env-spec-list
, and
LaTeX-fold-math-spec-list
Default display string for macros which are not specified in
TeX-fold-macro-spec-list
.
Default display string for environments which are not specified in
TeX-fold-env-spec-list
.
If non-nil the name of the macro or environment surrounded by square
brackets is used as display string, otherwise the defaults specified in
TeX-fold-unspec-macro-display-string
or
TeX-fold-unspec-env-display-string
respectively.
When you hover with the mouse pointer over folded content, its original
text will be shown in a tooltip or the echo area depending on Tooltip
mode being activate. In order to avoid exorbitantly big tooltips and to
cater for the limited space in the echo area the content will be cropped
after a certain amount of characters defined by the variable
TeX-fold-help-echo-max-length
.
Maximum length of original text displayed in a tooltip or the echo area for folded content. Set it to zero in order to disable this feature.
AUCTeX supports the standard outline minor mode using LaTeX/ConTeXt sectioning commands as header lines. See Outline Mode in GNU Emacs Manual.
You can add your own headings by setting the 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
or ConTeXt-INTERFACE-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.
Next: Prettifying, Previous: Outline, Up: Display [Contents][Index]
Sometimes you want to focus your attention to a limited region of the code. You can do that by restricting the text addressable by editing commands and hiding the rest of the buffer with the narrowing functions, see Narrowing in GNU Emacs Manual. In addition, AUCTeX provides a couple of other commands to narrow the buffer to a group, i.e. a region enclosed in a pair of curly braces, and to LaTeX environments.
(C-x n g) Make text outside current group invisible.
(C-x n e) Make text outside current environment invisible. With optional argument count keep visible that number of enclosing environmens.
Like other standard narrowing functions, the above commands are disabled. Attempting to use them asks for confirmation and gives you the option of enabling them; if you enable the commands, confirmation will no longer be required for them.
Emacs 25 is able to prettify symbols in programming language buffers,
see Misc for Programs in GNU Emacs Manual. The canonical
example is to display (lambda () ...)
as (λ () ...)
in
Lisp buffers.
AUCTeX can use this feature in order to display certain math macros
and greek letters using their Unicode representation, too. For example,
the TeX code \alpha \times \beta
will be displayed as α
× β
. When point is on one of the characters, it’ll be unprettified
automatically, meaning you see the verbatim text again. For this
behaviour however you need to set
prettify-symbols-unprettify-at-point
to t or right-edge
which will unprettify the symbol when point moves into or near it.
To enable prettification in AUCTeX, simply add
prettify-symbols-mode
to TeX-mode-hook
. If you enabled
prettification globally with global-prettify-symbols-mode
, then
it’s automatically enabled in AUCTeX, too.
You can also add custom symbol unicode-character pairs for
prettification by adding to tex--prettify-symbols-alist
. Note
that this variable is part of Emacs’ stock tex-mode.el
and used
by that and AUCTeX.