4

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.

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

gboffi
  • 594
  • 2
  • 19
  • In retrospect, the first alternative should be impossible... – gboffi May 18 '17 at 10:01
  • Maybe duplicate of this: https://tex.stackexchange.com/questions/48632/underscores-in-words-text ? – Ian May 19 '17 at 10:37
  • @Ian No, it is not. My Q is about Emacs, I've browsed the Q&A you referenced and it's **strictly** about TeX – gboffi May 19 '17 at 12:20

2 Answers2

4

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.

Stefan
  • 26,154
  • 3
  • 46
  • 84
  • 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 '17 at 16:52
  • @gboffi: Indeed, thanks for spotting that; fixed. – Stefan May 20 '17 at 17:47
2

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.

Advantages

  • 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.

Disadvantages

  • Not exactly what you asked for
Teodor
  • 153
  • 6
  • 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 '17 at 16:41