4

I am typing a math document in Russian using Emacs 24.3.1 and AUCTeX 11.87. I am using the russian-computer input method to type Russian. It would be very convenient to disable this input method inside math delimiters such as \( \) so that inside formulas I automatically switch to typing English without pressing Ctrl-\. For example, I could type "Рассмотрим формулу \(\)" ("Consider the formula \(\)"), put the point between the parentheses and start typing \forall. Currently, I would be typing \ащкфдд, but I would like Emacs to recognize that I am inside a formula and switch off the Russian input method.

I suspect that this can be done using post-self-insert-hook and texmathp from AUCTeX, but I am not sure if this is the most elegant method.

This question was asked on StackOverflow, but it was recommended there that I ask it here as well.

  • 2
    Note: with the upcoming AUCTeX 11.88 you'll be able to insert \(\) and move point between parentheses by pressing a single $ key. – giordano Aug 7 '14 at 21:42
  • Related (has an answer): emacs.stackexchange.com/q/5376/620 – giordano Dec 14 '14 at 21:03
2

The same question have been posted to emacs.SE:

According to the Emacs Lisp Manual every input method should disable key translation and conversion if overriding-local-map is set.

We should be able to rely on this to disable quail conditionally.

In Emacs 24.4 and later (using the new advising mechanism), add this to your init file:

(advice-add 'quail-input-method :around
            (lambda (orig key)
              "Disable quail-input-method in TeX and LaTeX formulas. Uses AUCTeX."
              (if (and (eq major-mode 'latex-mode)
                       (fboundp 'texmathp)
                       (texmathp))
                  (let ((overriding-local-map t))
                    (funcall orig key))
                (funcall orig key))))

Note that I check if the current major mode is latex-mode; you may need to adjust that. (We have to check the major mode because once AUCTeX is loaded texmathp is bound in all buffers, and we don't want to mess with input methods just because there are dollar signs in the current buffer.)

In older Emacs versions (tested under Emacs 23.4.1) we have to use defadvice:

(defadvice quail-input-method (around disable-quail-in-formulas activate)
  "Disable quail-input-method in TeX and LaTeX formulas. Uses AUCTeX."
  (if (and (eq major-mode 'latex-mode)
           (fboundp 'texmathp)
           (texmathp))
      (let ((overriding-local-map t))
        ad-do-it)
    ad-do-it))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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