I am working on literate programming in LaTeX. Actually, I am coding a lot of dtx files, which contain both the code and its documentation.

When working in Emacs, it uses AUCTeX and switches into TeXDoc-mode.

Quite often, I have to insert docstrip options or tags, which look like this:


(That is: a percent sign in the first column and than the tag which is enclosed in less/greater brackets <>. The opening tag begins with a asteriks *, the closing tag has a slash /. Of course, the name of the tag (here: option) has to be spelled identically.)

I would love to have some kind of Emacs function, which inserts this special kind of enviroment. Using the classical LaTeX-environment is not suited, as it inserts \begin and '\end`, which is not what I need.

I didn't find a hint in the manual.

Edit: I had a second question related to this theme.

1 Answer 1


You could write your own function and hook it into docTeX mode with a key binding, e.g. C-c g

(defun my/docTeX-guards ()
  (let ((guard (TeX-read-string "Guard: ")))
    (unless (equal (char-before) ?%)
      (insert "%"))
    (insert (concat "<*" guard ">"))
      (insert (concat "%</" guard ">")))))

(add-hook 'docTeX-mode-hook
           (lambda ()
           (define-key docTeX-mode-map "\C-cg"
  • Thank you very much. This works pretty well! I am very happy!
    – Jan
    Commented Mar 7, 2017 at 19:22
  • @Jan - You're welcome. Happy TeXing. Commented Mar 8, 2017 at 19:46
  • Arash, thank you very much. I added an newline after the last insert. It works fine. I am very happy with it.
    – Jan
    Commented Mar 11, 2017 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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