This package provides completion features that allow for the rapid entry of various LaTeX constructs (environments, macros, symbols, subscripts, …). It is similar in spirit to what is offered by the popular packages CDLaTeX and YASnippet, and also to the built-in Skeleton language. I suspect that this package is objectively inferior to these alternatives in most respects, but it has suited my needs well.
This package requires AUCTeX (to carry out the macro folding), so install that first.
Download this repository, install using M-x package-install-file (or package-vc-install, straight, elpaca, …), and add something like the following to your init file (after whatever is needed to load AUCTeX):
(use-package dynexp
:demand ; but after auctex
:bind
(:map LaTeX-mode-map
("SPC" . dynexp-space)
("TAB" . dynexp-next))
:config
(with-eval-after-load 'latex
(quietly-read-abbrev-file "~/whatever/dynexp/lisp/dynexp-abbrev.el")))Replace the path with whatever the path is to the dynexp-abbrev.el file that comes with this package (and feel free to put that file somewhere else and modify it to your liking; see below for details on the format).
This package makes use of Emacs’s built-in abbrevs feature, which expands words, called abbrevs, into some different text, called the expansion of the abbrev. Quoting from this most recent link:
For example, you might define
fooas an abbrev expanding tofind outer otter. Then you could insertfind outer otterinto the buffer by typingf o o SPC.
To each abbrev, one can attach a hook function, which is called, with no arguments, after the abbrev is replaced by its expansion; see this part of the elisp manual for details.
The main export of this package is a certain abbrev hook function dynexp that postprocesses the expansion. We illustrate with three examples.
- I use “bthm” as an abbrev for theorem environments. When I type “bthm” in a LaTeX buffer followed by
SPC(bound todynexp-space), it expands to the following abbrev:<+START+><+TAB+>\begin{theorem} <+TAB+><+++> <+TAB+>\end{theorem}<++><+END+>"The function
dynexppostprocesses this expansion, transforming it into the following:\begin{theorem} _ \end{theorem}<++>"The underscore _ is not actually present – I have used it just to indicate where the cursor is positioned. When I’m done writing the contents of the theorem, I hit
TAB(bound todynexp-next), which positions the cursor just beyond the theorem environment, like so:\begin{theorem} (Contents of my cool theorem.) \end{theorem}_ - The abbrev “zx” expands to
<+START+>$<+++>$<++><+END+>which
dynexpthen postprocesses to$_$<++>I then type some inline math. When I’m done, I hit
TABto position the cursor just beyond the second dollar delimiter:$x=y$_The point here is that with my keyboard layout (US), zx is very easy to type.
- The abbrev “mx3p” expands to
<+START+><+TAB+>\begin{pmatrix} <+TAB+><+++> & <++> & <++> \\ <+TAB+><++> & <++> & <++> \\ <+TAB+><++> & <++> & <++> \\ <+TAB+>\end{pmatrix}<++><+END+>which
dynexppostprocesses to\begin{pmatrix} _ & <++> & <++> \\ <++> & <++> & <++> \\ <++> & <++> & <++> \\ \end{pmatrix}<++>I can then fill in the matrix one entry at a time, using
TABto navigate.
The recipe is likely clear:
<+START+>and<+END+>specify the start and the end of the expansion,<+TAB+>indicates where we should apply suitable indentation,- <+++> is where the cursor should be positioned after the expansion, and
- <++> is left intact by the expansion, but used to denote “positions of interest” that are “eaten up” by
dynexp-next.
There are a few variants of the hook function dynexp, e.g., dynexp-fold, which expands the abbreviation template and applies TeX-fold-region to it.
The package file lisp/abbrevs.el contains a few thousand abbrevs that I’ve accumulated over the years. As a base, I started with FasTeX (http://www.cds.caltech.edu/~fastex/fastex.html), specifically, the FasTeX shortcuts for Emacs released by Ari Stern.
You can create new expansion templates by modifying lisp/abbrevs.el. It’s often convenient to mimic an existing expansion template.