# How can I disable quail input methods in LaTeX formulas?

If you set an input method (for example via `M-x set-input-method` RET `cyrillic-translit` RET) you can type Cyrillic characters using a US keyboard: Emacs will use transliteration rules to convert Roman characters into Cyrillic ones. If you look in `cyrillic.el`, you see for example `("f" ?ф)`, i.e., typing `f` gives `ф`.

Now imagine you are editing a LaTeX file and `LaTeX-mode` (AUCTeX) in enabled. You use Cyrillic characters in the body of the text and Roman characters in formulae. In this context you would like `f` to produce `f` and not `ф` in math mode, (i.e. in `\$...\$`, `\[...\]`, `\(...\)`, and in math environments).

If I understand the question how to detect if inside a LaTeX math environment? and this answer correctly, the function `texmathp` can detect if we are in a math environment.

My question is about the possibility of the following conditional transliteration using `quail`

typing `f` gives `f` inside a LaTeX math environments and `ф` elsewhere.

• Related (but without answer): tex.stackexchange.com/questions/195369/… Dec 14, 2014 at 18:25
• @giordano: Note that the question you linked to links to a StackOverflow question which does have an answer. I haven't compared it to my answer, though. (I may have the time to do it later, though.) Dec 14, 2014 at 19:55
• @Constantine I like your approach, more than that of the answer on Stack Overflow. I was looking for the function to advise, but I'm not really familiar with input methods. Dec 14, 2014 at 21:06
• @giordano: I'm not that familiar with input methods either, I just used this as an excuse to dig around and learn about it. :-) I updated the answer, by the way. I think it's a little less of a hack now. Dec 15, 2014 at 18:12

## 1 Answer

It's been almost 10 years since the last time I needed to edit a LaTeX document in Russian, but I can relate. :-)

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))
``````
• Thanks for your answer, when I put your code I get the error Symbol's function definition is void: advice-add. i don't know from where this problem arises.
– Name
Dec 15, 2014 at 6:22
• @Name: `advice-add` was added in Emacs 24.4. I'll update the answer to include a version of the code that works with older Emacs versions. (I'll let you know once that's done.) Dec 15, 2014 at 6:32
• @Name: I updated the answer; let me know if it works for you now. Dec 15, 2014 at 17:52
• +1000 for this wonderful answer, it works perfect.
– Name
Dec 16, 2014 at 10:29