Skip to content

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 init file such as init.el or .emacs:

emacs-lisp
(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 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. The key can be a character (e.g. ‘?o’) for a single stroke or a string (e.g. ‘"o a"’) for a multi-stroke binding. 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 Math 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).

Modifying Math Delimiters and Environments

AUCTeX offers the command LaTeX-modify-math to convert the mathematical construct at point—whether it is inline math such as ‘$...$’ or ‘\(...\)’, a display construct such as ‘$$...$$’ or ‘\[...\]’, or an environment such as ‘\begin{equation} ... \end{equation}’—into a different kind of delimiter or environment.

Command: LaTeX-modify-math

Interactively, prompt for the target delimiter or environment. The completion table contains the inline delimiters ‘$’ and ‘\(’, the display delimiters ‘$$’ and ‘\[’, and every math environment known to texmathp, such as ‘equation’, ‘align*’, or anything in the user option texmathp-tex-commands. The current construct is then rewritten using the chosen form, taking care to

  • keep any trailing punctuation outside inline math,
  • put display constructs on their own lines, and
  • strip any \label{} commands when converting to inline math.

When called from Lisp, new-type may be a string naming a delimiter or environment, or a cons ((open . close) . inline) specifying custom delimiters, where inline is non-nil for inline math.

This command does not understand macro-based math wrappers such as \ensuremath. It may also fail in docTeX buffers.

A related command, invoked with a prefix argument, is C-u C-c C-e (LaTeX-environment) (see Inserting Environment Templates). This modifies the current LaTeX environment, while LaTeX-modify-math also handles inline/display constructs.

You can define commands that convert to a particular form, e.g. by adding the following to your init file::

emacs-lisp
(defun my-LaTeX-make-brackets ()
  "Convert math construct at point to \"\\=\\[..\\]\"."
  (interactive)
  (LaTeX-modify-math "\\["))
emacs-lisp
(defun my-LaTeX-make-equation* ()
  "Convert math construct at point to \"equation*\"."
  (interactive)
  (LaTeX-modify-math "equation*"))

You can use LaTeX-modify-math to build higher‑level toggles. The following modifies any math construct to an ‘equation*’ environment, then toggles the numbered status:

emacs-lisp
(defun my-LaTeX-toggle-numbered ()
  "Convert math construct at point to \"equation*\".
If the math construct is already \"equation*\", then toggle with the
numbered variant \"equation\"."
  (interactive)
  (unless (texmathp) (user-error "Not inside math"))
  (let ((current (car texmathp-why)))
    (LaTeX-modify-math
     (pcase current
       ("equation*" "equation")
       ("equation" "equation*")
       (_ "equation*")))))

A further example toggles between ‘equation’, ‘align’ and their starred forms:

emacs-lisp
(defun my-LaTeX-toggle-align ()
  "Toggle math environment at point between \"equation\" and \"align\"."
  (interactive)
  (unless (texmathp) (user-error "Not inside math"))
  (let ((current (car texmathp-why)))
    (LaTeX-modify-math
     (pcase current
       ("align*" "equation*")
       ("equation*" "align*")
       ("align" "equation")
       ("equation" "align")
       (_ "align*")))))

Such helper commands can be bound in LaTeX-mode-map as you see fit, e.g. by adding the following to your init file:

emacs-lisp
(keymap-set LaTeX-mode-map "C-c e" #'my-LaTeX-make-equation*)

See LaTeX-make-inline, for a built-in convenience wrapper that converts display constructs to inline math.

Repeating recent math

Sometimes you want the next equation to be a slight modification of a previous one. The command LaTeX-repeat-recent-math looks backward for a recent top-level display math construct and inserts a copy.

Command: LaTeX-repeat-recent-math n

Search backward for the nth most recent outer-level math construct (‘\[...\]’, ‘$$...$$’, or any math environment recognized by texmathp, such as equation, align*, …). Copy that construct to the current line, indent it, and strip any labels.

With no prefix argument, n defaults to 1 (the most recent construct).

We can bind this command to a key, as follows:

emacs-lisp
(keymap-set LaTeX-mode-map "C-c r" #'LaTeX-repeat-recent-math)

Then C-c r duplicates the last equation, C-2 C-c r duplicates the second-to-last equation, and so on.