1

I'm trying to use the official AucTeX insertion mechanism to insert environments, it being official and all, but I'm having a really hard time getting into it. For instance, AucTeX automatically provides a template for an enumerate environment, but that template doesn't automatically add a reftex-label to the first item it inserts. Following the documentation and inspecting LaTeX-env-item and LaTeX-env-label I put the following into my init:

  (TeX-add-style-hook
   "latex"
   (lambda ()
     (LaTeX-add-environments
      '("enumerate" LaTeX-env-enumerate))))

  (defun LaTeX-env-enumerate (environment)
    "Insert ENVIRONMENT and the first item.
The first item is inserted by the function `LaTeX--env-item'."
    (LaTeX-insert-environment environment)
    (LaTeX--env-item environment)
    (LaTeX-label environment))

Positively speaking I can still insert environments, but negatively speaking it still doesn't insert a label. What am I doing wrong?

2
  • What exactly are you trying to achieve? You want to have a \label after the first \item in every enumerate environment you enter? Commented Jun 26 at 17:47
  • @ArashEsbati Yes, a reftex-indexed label. For later items, a label is added automatically by cdlatex, but when I use AucTeX to insert the enumerate environment, the first item is not automatically labeled, that's what I'm trying to correct Commented Jul 1 at 18:26

1 Answer 1

1

You have to consider some aspects in your code:

  • AUCTeX doesn't have an entry for the enumerate environment in the variable LaTeX-label-alist, so you have to add one first
  • You have to give your function LaTeX-env-enumerate more arguments than the standard LaTeX-env-item, otherwise you will not replace it
  • You need to tweak the way you call LaTeX-label by using the second TYPE argument

Putting it together, something like this in your init file should work:

(defun LaTeX-env-enumerate (environment &rest _ignore)
  "Insert ENVIRONMENT and the first item.
The first item is inserted by the function `LaTeX--env-item' followed by
a label."
  (LaTeX-insert-environment environment)
  (LaTeX--env-item environment)
  (save-excursion (LaTeX-label environment 'environment)))

(add-hook 'LaTeX-mode-hook
          (lambda ()
            (TeX-add-style-hook
             "latex2e"
             (lambda ()
               (add-to-list 'LaTeX-label-alist
                            '("enumerate" . "item"))
               (LaTeX-add-environments
                '("enumerate" LaTeX-env-enumerate))))))

AUCTeX will pass the generation of label to RefTeX and inserts it after the first \item.

P.S. I'm not a fan of having this kind of stuff in init file. You really should put your LaTeX customizations in a <name>.sty file and corresponding AUCTeX code in <name>.el in a directory where AUCTeX can find the library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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