Suppose I have the following text:

…as shown in figure (24).

Now, suppose I select "(24)." Then, I want a command to convert it to:

…as shown in figure \command{eq:24}.

Viz., how would I define a new Emacs command that I could place in ~/.emacs that would run C-C RET command RET on a regexp'ed version of the selected text? thanks


  • C-C RET command RET
    – giordano
    Oct 5, 2013 at 18:42
  • Do you want to bind another key to the same function C-c RET command RET runs? Why this?
    – giordano
    Oct 5, 2013 at 18:54
  • @giordano: Because there's some regexping in addition, too. "(24)" needs to be regexped to "eq:24" and then C-c RET command RET can run on that.
    – Geremia
    Oct 5, 2013 at 18:55
  • I think the easiest option here is to carefully record a keyboard macro and then save it. Then, look over what you did (command wise) and make a proper LISP macro. Oct 6, 2013 at 3:21
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. Oct 24, 2016 at 19:07

1 Answer 1


The function C-c RETcommandRET runs is (TeX-insert-macro "command"), so this code does what you want

(eval-after-load "latex"
     (define-key LaTeX-mode-map (kbd "INSERT-YOUR-KEYBINDING-HERE")
       (lambda (beg end)
     (interactive "^r")
     (if (> (point) (mark))
     (re-search-forward "(\\([^)]*\\))" (mark) t)
     (replace-match  "eq:\\1")
     (goto-char beg)
     (TeX-insert-macro "command")

You can adapt it to fit your needs.

  • How do I do the regexp'ing? Viz., what do I fill in for "command"? thanks
    – Geremia
    Oct 5, 2013 at 19:12
  • Also, your code is missing a final ")".
    – Geremia
    Oct 5, 2013 at 21:36
  • I've updated the answer, now it should be complete. In the previous code there was no missing ).
    – giordano
    Oct 6, 2013 at 10:06
  • @Geremia have you seen the updated answer? Is it OK now?
    – giordano
    Oct 11, 2013 at 19:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .