# emacs/aquamacs + latex/auctex: how to define one's own environments or commands?

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]
\item
\item
\end{enumerate}


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 (?)

-

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

-

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] \item1
\item $2 \end{enumerate}$0

3. put the following code in Preferences.el:

(add-to-list 'load-path "~/.emacs.d/plugins/yasnippet-0.6.1c")
(require 'yasnippet)
(yas/initialize)

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.org/emacs/yasnippet_templates_howto.html - add comment 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" (interactive) (progn (insert "\\begin{enumerate}[label=(\arabic*)\\ ,leftmargin=*,align=left,topsep=\\mytopsep,itemsep=\\myitemsep] \\item ") (save-excursion (insert " \\end{enumerate}")) ) ) (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. - add comment 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] \item0
\item
\end{enumerate}


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 '11 at 7:10 Why is there only a limited number of characters that can be posted? hmmm... – Marius Sep 16 '11 at 7:11 oh, yes, and I chose a simpler example environment to start with: \begin{align*}0 &=\ \$1 &= \end{align*} –  Marius Sep 16 '11 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 '11 at 22:47
You want to use skeletons optionally with abbrev.