2

I am using Emacs with AUCTeX to edit LaTeX code which uses algorithm2e. How do I make Emacs highlight \nllabel{stuff} the same way it highlights \label{stuff}?

1

You could write a support file (style) for AUCTeX in order to make your typing life easier. I don't use algorithm2e, but a skeleton for such a style can look like this:

;;; algorithm2e.el --- AUCTeX style for `algorithm2e.sty' (v5.1)

(defun LaTeX-algorithm2e-lineend ()
  "Insert \"\\;\" to terminate lines in env's provided by algorithm2e package."
  (end-of-line 0)
  (delete-horizontal-space)
  (insert "\\;")
  (forward-line 1)
  (indent-according-to-mode))

(TeX-add-style-hook
 "algorithm2e"
 (lambda ()

   (let ((envs '("algorithm" "algorithm*"
         "procedure" "procedure*"
         "function"  "function*")))
     (dolist (env envs)
       ;; Add env's to `LaTeX-item-list' in order to run
       ;; `LaTeX-algorithm2e-lineend' when `M-RET' is invoked
       (add-to-list 'LaTeX-item-list `(,env . LaTeX-algorithm2e-lineend) t)

       ;; Tell AUCTeX that we want to prefix the labels with `alg:'
       (add-to-list 'LaTeX-label-alist `(,env . "alg:") t)

       ;; Tell RefTeX the same thing:
       (when (fboundp 'reftex-add-label-environments)
         (reftex-add-label-environments
          `((,env ?a "alg:" "~\\ref{%s}" caption (regexp "[Al]lgorithm")))))))

   (LaTeX-add-environments
    '("algorithm")
    '("algorithm*")
    '("procedure")
    '("procedure*")
    '("function")
    '("function*"))

   (TeX-add-symbols
    '("KwData" "Keyword Data")
    '("KwResult" "Keyword Result")
    '("nllabel" TeX-arg-define-label))

   ;; Fontification
   (when (and (featurep 'font-latex)
          (eq TeX-install-font-lock 'font-latex-setup))
     (font-latex-add-keywords '(("nllabel" "{"))
                  'reference)))
 LaTeX-dialect)

(defvar LaTeX-algorithm2e-package-options
  '(;; 7.1 Language option
    "english" "french" "frenchkw" "german" "germankw"
    "ngerman" "spanish" "spanishkw" "italiano" "italianokw"
    "portuguese" "portuguesekw" "croatian" "croatiankw"
    "czech" "czechkw" "turkish" "turkishkw" "onelanguage"
    ;; 7.2 Compatibility issue
    "algo2e" "endfloat" "norelsize" "slide" "nokwfunc"
    ;; 7.3.1 boxed, ruled, plain environment
    "boxed" "boxruled" "ruled" "algoruled" "tworuled" "plain"
    ;; 7.3.2 algorithm numbering
    "algochapter" "algosection" "algopart" "procnumbered"
    ;; 7.3.3 figure and toc
    ;; ...
    )
  "Package options for the algorithm2e package.")

;;; algorithm2e.el ends here

As a feature, you can hit M-RET at end of lines in the environments defined and Emacs inserts \; and a line break.

In order to use this style, set the variable TeX-style-private to a directory of your choice and save the code as algorithm2e.el there. Next time you load your .tex file, AUCTeX should load the style (remember to set (setq TeX-parse-self t) in your init file).

You can extend this file and once you have the macros in place, you can submit this file to AUCTeX devel-list and bundle it with AUCTeX and make it available for other users.


With the setup described, a test file looks like this for me:

enter image description here

  • @LeonMeier - I assume you have restarted Emacs after editing your init file. If yes, open your .tex file and press C-c C-n with the setup described above. – Arash Esbati Nov 4 '16 at 15:37
  • @LeonMeier - After pressing C-c C-n, switch to buffer *Messages*, do you see a line like Loading ~/usr/local/share/emacs/algorithm2e.el (source)...done? – Arash Esbati Nov 4 '16 at 16:09
  • @LeonMeier - Hmm, for testing, open your .tex file, press C-h v TeX-style-path RET and move algorithm2e.el into a directory mentioned there (BTW, the value of your TeX-style-private should be there, otherwise your init file isn't read). I also suggest to update your AUCTeX with M-x package-install RET auctex RET. 11.87 is about 4 years old. – Arash Esbati Nov 4 '16 at 16:21
  • Yes, of course. – giordano Nov 4 '16 at 21:29

Your Answer

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