math-delimiters icon indicating copy to clipboard operation
math-delimiters copied to clipboard

Insert math delimiters in TeX, LaTeX and Org buffers

  • math-delimiters

This package provides the command =math-delimiters-insert= to insert TeX/LaTeX math delimiters. This command is meant to be bound to $ in buffers where the user wants to use it. The author, for example, binds it in =LaTeX-mode-map= and in =org-mode-map=.

Note that the excellent =cdlatex= includes a =cdlatex-dollar= command that it binds to $. Users of both packages will probably want to unbind $ in =cdlatex-mode-map=.

The =math-delimiters-insert= command behaves as follows: if the region is active, it surrounds it with inline math delimiters and leaves the point after. Without an active region it inserts inline math delimiters and places point inside them. It toggles between display and inline if called from inside empty math delimiters, or just after math delimeters. (As a consequence, if called repeatedly it toggles between inline and display math.) A tiny bit of practice makes this very natural.

The delimiters used for inline math and display math by =math-delimiters-insert= are customizable, defaulting to =(...)= and =[...]= respectively. A command =math-delimiters-toggle= is provided to quickly toggle between =$...$= and =(...)= for inline math and between =$$...$$= and =[...]= for display math.

By default, inline math is translated into display math with no additional line breaks. This can be modified by setting =math-delimiters-compressed-display-math= to =nil=. For example, expression of the form =(...)= will then be translated to =\n[\n...\n]\n=.

When translating inline math to display math, often it is desirable to include punctuation into the display math and exclude it from of the inline math environment. For that purpose, the variable =math-delimiters-include-characters= is available; it is a string consisting of the punctuation characters to act upon. The default value is ",.;". So, for example, toggling to display math on the expression =(\eta).= would turn it into =[\eta.]=.

Finally, a command =math-delimiters-no-dollars= is provided to replace all =$...$= and =$$...$$= delimiters with =(...)= and =[...]=, respectively. Sadly, this package offers no help convincing coauthors to use =(...)= over =$...$=.

  • Sample configuration

Put =math-delimiters.el= somewhere in your =load-path= and use something like this:

#+begin_src emacs-lisp (autoload 'math-delimiters-insert "math-delimiters")

(with-eval-after-load 'org (define-key org-mode-map "$" #'math-delimiters-insert))

(with-eval-after-load 'tex ; for AUCTeX (define-key TeX-mode-map "$" #'math-delimiters-insert))

(with-eval-after-load 'tex-mode ; for the built-in TeX/LaTeX modes (define-key tex-mode-map "$" #'math-delimiters-insert))

(with-eval-after-load 'cdlatex (define-key cdlatex-mode-map "$" nil)) #+end_src

(Most people only use one out of AUCTeX and the built-in TeX/LaTeX modes, so you probably only need one of those forms.)

The above configuration will setup the $ key to insert math delimiters in both TeX and LaTeX buffers (because the LaTeX mode maps inherit from the TeX mode ones), if you only want to use it in LaTeX buffers you can use this instead:

#+begin_src emacs-lisp (with-eval-after-load 'latex ; for AUCTeX (define-key LaTeX-mode-map "$" #'math-delimiters-insert))

(with-eval-after-load 'tex-mode ; for the built-in LaTeX mode (define-key latex-mode-map "$" #'math-delimiters-insert)) #+end_src

Notice that for the built-in LaTeX mode the feature is still =tex-mode=, not =latex-mode=.