Most(?) programming languages permit to use Unicode in comments, strings and, partially, variable names. It is handy to be able to write comments like


etc, and it's very handy (at least for me) to use TeX input method for that.

In the above example I had to type …\Lambda_i… and that's fine, but when I type regular program text, like n_intervals what I get is nᵢntervals...

It is perfectly possible for me to type n^Q_intervals and get n_intervals (typing n_<space><backspace>intervals works too) but I'd like to either

  • reverse the behaviour, i.e., typing \Lambda^Q_i to have Λᵢ and n_intervals to get n_intervals or
  • typing n__intervals (have you seen _ and _?) to get n_intervals.

BTW I'm not restricting your answers to the alternatives above.


2 Answers 2


Here's what I use

(defvar sm-quail-activate-hook-done nil)
(defun sm-quail-activate-hook ()
  ;; (define-key (quail-translation-keymap) "_" 'self-insert-command)
  (unless (member (quail-name) sm-quail-activate-hook-done)
    (push (quail-name) sm-quail-activate-hook-done)
    (when (member (quail-name) '("TeX"))
      ;; Copy the "_..." bindings to "\_...".
      (setf (alist-get ?_ (cdr (alist-get ?\\ (quail-map))))
            (alist-get ?_ (quail-map)))
      ;; Remove the "_..." bindings.
      (setf (alist-get ?_ (quail-map)) nil)
      ;; (quail-defrule "\\hline" ["------------------------"])
(add-hook 'quail-activate-hook #'sm-quail-activate-hook)

Rather than move the _... bindings to \_... you can also just remove the _... bindings and rely on the X11 input method using a key combination such as Multi_key _ 1.

  • Could you please edit the paragraph "Rather than..." to fix the disappeared backslash? I tried it myself but my edit were too short and I'd prefer not to introduce gratuitous edits into your post just to make it long enough...
    – gboffi
    May 20, 2017 at 16:52
  • @gboffi: Indeed, thanks for spotting that; fixed.
    – Stefan
    May 20, 2017 at 17:47

As you say for yourself, typing n_<space><backspace>intervals gives what you want with two extra key presses.

Proposed solution

  • Type n_<C-g>intervals to get underscores when you need them inside TeX input method
  • Use C-\ to quickly toggle the input method

I do this for scientific Python code. I generally keep the TeX input method off, but turn it on for writing longer comments and documentation where I need Unicode.

For more information, see <C-h> f then set-input-method.


  • Does not interfere with any other parts of the TeX input method
  • No configuration
  • You can reuse your C-g reflex to "abort this" when you see that Emacs is about to substitute something you don't want.


  • Not exactly what you asked for
  • 1
    "Not exactly what you asked for" but good enough indeed, especially the `C-\` thing, to enter and exit with one keystroke from TeX input method. Tx
    – gboffi
    May 20, 2017 at 16:41

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.