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

3.1 Font Locking

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 (emacs)Font Lock section ‘Font Lock Mode’ in GNU Emacs Manual.

User Option: TeX-install-font-lock

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.


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

3.1.1 Fontification of macros

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:

  1. Commands of the form ‘\foo[bar]{baz}’ which consist of the macro itself, optional arguments in square brackets and mandatory arguments in curly braces. For the command itself the face 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.
  2. Declaration macros of the form ‘{\foo text}’ which consist of the macro which may be enclosed in a TeX group together with text to be affected by the macro. In case a TeX group is present, the macro will get the face 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.
  3. Simple macros of the form ‘\foo’ which do not have any arguments or groupings. The respective 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).

General macro classes

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

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.

User Option: 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.

Commands for changing fonts

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

Deactivating defaults of built-in keyword classes

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", "reference", "function" , "sectioning-0", "sectioning-1", "sectioning-2", "sectioning-3", "sectioning-4", "sectioning-5", "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 "footnote" 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.

User-defined keyword classes

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.

User Option: 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 font specifications made by you. (The latter option is not available on XEmacs.)

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.


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

3.1.2 Fontification of quotes

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.

User Option: font-latex-quotes

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.


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

3.1.3 Fontification of mathematical constructs

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.

User Option: font-latex-fontify-script

If non-nil, fontify subscript and superscript strings.

Note that this feature is not available on XEmacs, for which it is disabled per default. In GNU Emacs raising and lowering is not enabled for versions 21.3 and before due to it working not properly.

User Option: font-latex-script-display

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 (elisp)Other Display Specs section ‘Other Display Specifications’ in GNU Emacs Lisp Reference Manual.


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

3.1.4 Verbatim macros and environments

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.


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

3.1.5 Faces used by font-latex

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.


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

3.1.6 Known fontification problems

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 section Verbatim macros and environments. If this is not possible, you can insert a commented dollar sign (‘%$’) at the next suitable end of line as a quick workaround.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated on October 17, 2018 using texi2html 1.82.