Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to write an elisp emacs command that inserts a fixed string that contains a variable part at the current cursor position:

\label{$STRING} \index{\nameref{$STRING}}

where the command should query for $STRING and insert the whole text.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Elisp

Here is a simple elisp function for it:

(defun labelnameref (string)
  "Insert \label{ARG} \index{\nameref{ARG}} at point"
  (interactive "sString for \\label and \\nameref: ")
  (insert "\\label{" string "} \\index{\\nameref{" string "}}"))

This function queries in the minibuffer for the string and then inserts it all at point. To use it you can put it in your .emacs and then invoke it via M-x labelnameref or bind it to a key.

YASnippet

If you want to use lots of similar constructs it might be easier to write them as yasnippets. With YASnippet you can easily create a snippet with similar behavior as above. For example you can use the following (you have replace "keybinding" with a proper keybinding if you want a keybinding for it):

# -*- mode: snippet -*-
# name: foo
# key: foo
# binding: "keybinding"
# --
\label{$1} \index{\nameref{$1}}

With this you write foo and press Tab directly afterwards to expand it to \label{$1} \index{\nameref{$1}} and query for $1.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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