# How to get AucTex to prompt me for a name, when inserting an environment using Ctrl-C, Ctrl-E?

I'm using emacs auctex. I've defined an environment template, as follows.

\documentclass{article}

\newtheorem{definition}{Definition}[section]

\begin{document}
\end{document}


I insert instances of this environment into the document using the Ctrl-C, Ctrl-E keyboard shortcut. Upon doing so, I'd like to be prompted for a name, which will be included in square brackets thus:

\begin{definition}[Cauchy-Schwartz]
\end{definition}


How can I do it?

• Do you want to do it for every new theorem defined with \newtheorem or just for the definition environment? The latter case is much easier. Mar 12, 2014 at 20:17
• @giordano: Selected theorems defined with \newtheorem (some yes, some no). Mar 12, 2014 at 20:21

The solution I'm going to provide requires parsing of files enabled. As explained in the AUCTeX manual you can achieve it by adding this to your .emacs, if you haven't already done that

(setq TeX-parse-self t) ; Enable parse on load.
(setq TeX-auto-save t) ; Enable parse on save.


(setq-default TeX-master nil)


in order to manage multi-file documents.

Returning to your question, add the following code to your .emacs (this should work also with the current stable version of AUCTeX, 11.87)

(add-hook
'LaTeX-mode-hook
(lambda ()
;; Self Parsing -- see (info "(auctex)Hacking the Parser").
(defvar mg-LaTeX-theorem-regexp
(concat "\\\\newtheorem{\$$" TeX-token-char "+\$$}")
"Matches new theorems.")
(defvar mg-LaTeX-auto-theorem nil
"Temporary for parsing theorems.")
(defun mg-LaTeX-theorem-prepare ()
"Clear mg-LaTex-auto-theorem' before use."
(setq mg-LaTeX-auto-theorem nil))
(defun mg-LaTeX-theorem-cleanup ()
"Move theorems from mg-LaTeX-auto-theorem' to mg-LaTeX-theorem-list'.
Add theorem to the environment list with an optional argument."
(mapcar (lambda (theorem)
(,theorem ["Name"])))
mg-LaTeX-auto-theorem))
;; FIXME: This does not seem to work unless one does a manual reparse.
(TeX-auto-add-regexp (,mg-LaTeX-theorem-regexp 1 mg-LaTeX-auto-theorem))))

After this edit, restart Emacs (note: usually a M-x eval-buffer RET, but by restarting Emacs you'll be sure the changes will be loaded correctly). Open a document with a \newtheorem definition, (re)parse the file, e.g. do a dummy edit (and and delete a space) and save the buffer, then you'll be able to add your theorems with an optional Name argument.
• @EvanAad I added a little more information. I hope this helps :-)`. Mar 13, 2014 at 14:22