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.
In addition I remind you that it's advisable to add also the line
(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 ()
(TeX-auto-add-type "theorem" "mg-LaTeX")
;; 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)
(add-to-list 'mg-LaTeX-theorem-list (list theorem))
(LaTeX-add-environments
`(,theorem ["Name"])))
mg-LaTeX-auto-theorem))
;; FIXME: This does not seem to work unless one does a manual reparse.
(add-hook 'TeX-auto-prepare-hook 'mg-LaTeX-theorem-prepare)
(add-hook 'TeX-auto-cleanup-hook 'mg-LaTeX-theorem-cleanup)
(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.
\newtheorem
or just for thedefinition
environment? The latter case is much easier.\newtheorem
(some yes, some no).