[Tex/LaTex] emacs/aquamacs + latex/auctex: how to define one’s own environments or commands

aquamacsauctexemacs

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

Best Answer

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