Search code examples
emacsauctex

Insert starred environment or command in AUCTeX


Simple question today – I hope the answer is equally simple.

From TextMate I am used to a keyboard shortcut that changes the current environment or command to its starred version. Is there something similar in Emacs/AUCTeX?


Solution

  • As noted by Kilian Foth you can write a function to modify environments in such a way:

    (defun LaTeX-star-environment ()
      "Convert the current environment to its starred version."
      (interactive)
      ;; Add a star to the current environment.
      (LaTeX-modify-environment (concat (LaTeX-current-environment) "*")))
    

    This function will keep adding stars (*) to the current environment if you repeat the command.

    If you instead want a function to star the current environment if it is not already starred and to unstar it if it is already starred you can use the following function:

    (defun LaTeX-star-environment-dwim ()
      "Convert between the starred and the not starred version of the current environment."
      (interactive)
      ;; If the current environment is starred.
      (if (string-match "\*$" (LaTeX-current-environment))
          ;; Remove the star from the current environment.
          (LaTeX-modify-environment (substring (LaTeX-current-environment) 0 -1))
        ;; Else add a star to the current environment.
        (LaTeX-modify-environment (concat (LaTeX-current-environment) "*"))))
    

    You can use either function by including it in your .emacs and doing M-x LaTeX-star-environment or LaTeX-star-environment-dwim respectively or by binding the function to a key.