I work with Aquamacs (emacs on Mac OS X; latest version) to compile .tex documents. Although AUCTeX provides environments such as enumerate etc., I would like to define a shortcut/macro to get my own environment in the .tex file. Ideally, it should be:

I hit a certain keystroke

The following code is entered in the .tex file at the current cursor position:

\begin{enumerate}[label=(\arabic*)\ ,leftmargin=*,align=left,topsep=\mytopsep,itemsep=\myitemsep] 

The cursor should jump to the first \item [since that is where one wants to continue writing].

I should be possible to do these things, but I have no glue how since I'm only an emacs user for about a week [really like it so far :-)]. I would guess it could be done in Preferences.el (the .emacs for Aquamacs) with a hook to LaTeX mode (?)

  • 2
    We'd like to keep answers separate from questions, so you should write a separate answer instead of editing your answer into the question. Self-answers are perfectly admissibe, and a well-written answer may earn you additional reputation.
    – lockstep
    Sep 18, 2011 at 9:19

5 Answers 5


yasnippet does just what you're looking for. Check out the documentation in the link, it's relatively painless, and easier to set up than things like skeleton, in my experience.

The snippet for your particular need would be something like this:

\begin{enumerate}[label=(\arabic*) ,leftmargin=*,align=left,topsep=\mytopsep,itemsep=\myitemsep] 
    \item $0 

That's fairly intuitive, I think. Once you've got it installed, the rest is easy!

BTW, once you've entered your first item, C-c C-j will automatically add the next one for you, as a built-in feature of Auctex, even without yasnippet.C-c C-e prompts you for a new environment to add, which is more general than using a snippet, but requires you to fill in the options by hand.

  • Dear Tyler, thanks for the hint. I did the following: (1) successfully installed yasnippet (2) opened a .tex file (3) M-x yas/new-snippet (4) put in the following: # -- mode: snippet -- # name: align* # binding: (LaTeX-mode-map . "M-1") # expand-env: ((some-var some-value)) # -- \begin{align*} $0 &=\ $1 &= \end{align*} (5) saved [normal command + s] There are two remaining problems: (1) the keystroke is not working [it does nothing] (2) I can insert the snippet via M-x yas/insert-snippet but it writes it "as is" including $0 instead of jumping there Do you know a solution?
    – Marius
    Sep 16, 2011 at 7:10
  • Why is there only a limited number of characters that can be posted? hmmm...
    – Marius
    Sep 16, 2011 at 7:11
  • oh, yes, and I chose a simpler example environment to start with: \begin{align*} $0 &=\ $1 &= \end{align*}
    – Marius
    Sep 16, 2011 at 7:12
  • The snippet has to be loaded before it will work. You can do this with M-x yas\load-snippet-buffer, selecting latex-mode as where you want it loaded. To have it loaded automatically, it should be saved in a file in your mysnippets/latex-mode directory. I didn't bother using the expand-env, or binding lines, they aren't necessary.
    – Tyler
    Sep 16, 2011 at 22:47

The questioner's answer:

  1. the goal is not to get a certain keybinding to work, the goal is to define a yasnippet; this is then inserted just by typing the corresponding key and hitting <TAB>

  2. $0 does not refer to the first point to jump to (via <TAB>), but rather to the exit point (the last one)

  3. For the minimal example above, one can do the following:

    1. create a file named enuma.yasnippet in ~/.emacs.d/plugins/yasnippet-0.6.1c/snippets/text-mode/latex-mode

    2. put the following code in this file:

       \# name: enumerate arabic
       \# key: enuma
       \# --
       \begin{enumerate}[label=(\arabic*)\ ,leftmargin=*,align=left,topsep=\mytopsep,itemsep=\myitemsep] 
           \item $1
           \item $2
    3. put the following code in Preferences.el:

       (add-to-list 'load-path "~/.emacs.d/plugins/yasnippet-0.6.1c")
       (require 'yasnippet) 
       (yas/load-directory "~/.emacs.d/plugins/yasnippet-0.6.1c/snippets")
    4. open the .tex file and do YASnippet -> Reload everything

    5. then just type in enuma and hit <TAB>; you can switch through the $-places via <TAB> hits.

This worked for me (Aquamacs; yasnippet installed in ~/.emacs.d/plugins as suggested on the yasnippet website)

I hope this helps, there are more examples on http://xahlee.info/emacs/emacs/yasnippet_templates_howto.html


You want to use skeletons optionally with abbrev.

  • Dear Ross, thanks for your reply. I visited emacswiki.org/emacs/SkeletonMode and tried the following code, but it is not working: ;; align* (define-skeleton latex-skeleton "Insert a special enumerate environment" "\\begin{align*}\n" " &=@ -\\\\\n" " &=\n" "\\end{align*}\n") (define-key LaTeX-mode-map (kbd "M-1") 'latex-skeleton)
    – Marius
    Sep 15, 2011 at 21:27
  • This sounds like a new question. Either way, your comment isn't clear enough. When asking others for help it's pretty much never enough to say "it is not working".
    – Ross Patterson
    Sep 15, 2011 at 22:18
  • Dear Ross, thanks. I should have said "it does nothing", so the keystroke is not working (instead of just "it is not working"), sorry.
    – Marius
    Sep 16, 2011 at 7:11

You can do this by putting into your init file the command to insert it all. (I use xemacs, so my init file is .xemacs/init.el). Try putting in the following code:

(defun my-enum ()
 "We insert an enum environment with lots of options"
   (insert "\\begin{enumerate}[label=(\arabic*)\\ ,leftmargin=*,align=left,topsep=\\mytopsep,itemsep=\\myitemsep]
    \\item ")
   (save-excursion (insert "
(global-set-key "\ee"     'my-enum)

The defun defines a function that will insert what you want and put the cursor where you want. That last global-set-key command ties the command to Alt-E (or Escape-E); if you'd prefer, e.g., tying it to Alt-g, then change that "\ee" to "\eg". Note also that each insert command contains the string to be inserted between double quote characters, and each of them contains a carriage return followed by the number of spaces by which you want to indent the following line.


The problem, I believe, that the OP was having with creating the snippet in yasnippet was that they had the following line (which yasnippet inserts automatically in new snippets):

# expand-env: ((some-var some-value))

When this is run on expansion, it causes an emacs lisp error, which kills the expansion. To deal with this, either delete the line from the snippet, or double the # mark to make it a snippet-mode comment, thus:

## expand-env: ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .