# TeX input method is fine — except for underscore

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

# DETERMINATION OF CONSTANTS OF INTEGRATION, substituting Λᵢ in M(L)=0

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.

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" ["------------------------"])
)))

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... May 20 '17 at 16:52
• @gboffi: Indeed, thanks for spotting that; fixed. May 20 '17 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.

• "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 May 20 '17 at 16:41