1

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?

2
  • 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.
    – giordano
    Mar 12, 2014 at 20:17
  • @giordano: Selected theorems defined with \newtheorem (some yes, some no).
    – Evan Aad
    Mar 12, 2014 at 20:21

1 Answer 1

3

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.

10
  • Thank you very much. Unfortunately, it doesn't work for me. I use Emacs 24.3.1
    – Evan Aad
    Mar 12, 2014 at 21:37
  • @EvanAad Ok, but what's your AUCTeX version? Anyway, due to internal limitations of AUCTeX (mentioned in the comments to the code), you have first to (re)parse the file, e.g. do a dummy edit, save the buffer and then you'll be able to add your theorems with an optional argument.
    – giordano
    Mar 12, 2014 at 21:39
  • I've followed your instruction (i.e. did a dummy edit and saved the buffer), but still, it doesn't work. My AUCTex version is 11.87.2012-12-04.
    – Evan Aad
    Mar 12, 2014 at 21:54
  • @EvanAad That's strange: I've just tested the code with AUCTeX 11.87 and it should work. I always give it for granted, but have you enabled parsing of files?
    – giordano
    Mar 12, 2014 at 22:17
  • 1
    @EvanAad I added a little more information. I hope this helps :-).
    – giordano
    Mar 13, 2014 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.