Add Acronyms easily in Auctex with Emacs
Posted on Mar 09, 2018.
In my daily job, I need to write a lot of technical and scientific LaTeX documents. These documents often contain acronyms. The first time you use an acronym, it should be written out; after that, you can use the acronym itself. In bigger documents, there should even be an acronym list, to help the reader understand the text. Acronyms are a delicate problem because if you are in the process of writing you will move text around, add a new paragraph, or remove text. The sentence that has the acronym written out is later in the middle of the text. To avoid getting lost, LaTeX has a wonderful package called acronym
that takes care of this problem. With this package, you can define an acronym list and use it in the text. In the following, you will find a simple example:
\usepackage[nolist]{acronym} \begin{acronym} \acro{BSS}{Basic Service Set} \acro{COTS}{Commercial off-the-shelf} \acro{CSI}{Channel State Information} \acro{DBP}{Distance Bounding Protocol} \end{acronym} Here is now my text where I can easily use acronyms like \ac{CSI}.
This package checks when you use an acronym the first time and automatically replaces it with the full name. You can even force the full name with \acf{}
or use the plural version acp{}
. I highly recommend this package, because it makes the writing process much easier. So far so good. I find myself often deep in the text and then I need to define a new acronym. This means I need to go to the acronym list, add the acronym, going back to my text and use it. I find this cumbersome and I think Emacs should help me with this. This is why I wrote a little Emacs Lisp function that does this step automatically:
(defun fa/add-latex-acronym (region-beg region-end) "This function reads the written out form of an acronym via the minibuffer and adds it to the acronym list in a latex document. Addtionally, it sorts all acronyms in the list." (interactive "r") (save-excursion (let ((acronym (if (region-active-p) (buffer-substring region-beg region-end) (read-from-minibuffer "Acronym: "))) (full-name (read-from-minibuffer "Full Name: "))) (beginning-of-buffer) (if (search-forward "\\begin{acronym}" nil t) (progn (deactivate-mark) (open-line 1) (forward-line 1) (insert (concat " \\acro{" acronym "}{" full-name "}")) (beginning-of-line) (sort-lines nil (point) (search-forward "\\end{acronym}" nil nil))) (user-error "No acronym environment found")))))
You can either mark an acronym and then call this function, then you only have to enter the full name of the acronym. Or, you can call the function without the mark, but then you need to enter the acronym as well. The function searches for the acronym list in your LaTeX document and adds the full name to it. It will also sort your list of acronyms. In the following gif you can see the function in Action:
To make this even more convenient it makes sense to bind this function to a keybinding. To find some unused keybindings in Emacs is not easy. That is why I overwrote C-c C-a (TeX-command-run-all)
, which I don't use. I bound this function to C-c C-a a
, which gives me the possibility to add further functions that deals with acronyms:
(eval-after-load "latex" '(define-key LaTeX-mode-map (kbd "C-c C-a a") 'fa/add-latex-acronym))
Maybe other Emacs users find this function useful. If you have any comments or improvements please drop me a mail. Happy Hacking!