# Ways to automatically insert a space after a LaTeX command (inserted by an AucTeX shortcut, e.g. g) if the next keystroke is a letter

With AucTeX, one can insert many LaTeX commands using shortcuts.

For example in a math environment (e.g., between ), the shortcut g insert \gamma. Normally right after \gamma if I would like to type a letter I should make a space, for example $\gamma a=1$, but if I would like to type a number, a symbol or another latex command, it is not necessary, for example $\gamma\alpha=1$ or $\gamma*1=\gamma$ or $\gamma1=\gamma$, or $\gamma^1=\gamma$.

Is there a way in AucTeX to insert a space right after a latex command (inserted by an AucTeX shortcut, e.g. g) if the next keystroke is a letter?

I think this can significantly increase the speed of typing, because the space should be inserted anyway.

I have added the requirement inserted by an AucTeX shortcut to avoid the complicated situations like \leq and \leqslant.

• Here is a rough idea for a solution: Put a function on post-self-insert-hook. The function should examine last-command, and if it is whatever auctex uses to expand shortcuts, it should examine the character just inserted and optionally insert a space in front of it. – Harald Hanche-Olsen Oct 30 '16 at 11:42

  (defun my-insert-space-if-alnum (orig-func &rest args)
"Insert an extra space if the char read is alphabetical or a number."
(when (memq (get-char-code-property c 'general-category) '(Ll Lu Nd))
(insert ?\ ))
(insert c)))
(dolist (f '(
LaTeX-math-alpha
LaTeX-math-beta
LaTeX-math-delta
LaTeX-math-Delta
LaTeX-math-epsilon
LaTeX-math-phi
LaTeX-math-Phi
LaTeX-math-gamma
LaTeX-math-Gamma
LaTeX-math-eta
LaTeX-math-theta
LaTeX-math-Theta
LaTeX-math-kappa
LaTeX-math-lambda
LaTeX-math-Lambda
LaTeX-math-mu
LaTeX-math-nu
LaTeX-math-nabla
LaTeX-math-pi
LaTeX-math-Pi
LaTeX-math-chi
LaTeX-math-rho
LaTeX-math-sigma
LaTeX-math-Sigma
LaTeX-math-tau
LaTeX-math-upsilon
LaTeX-math-Upsilon
LaTeX-math-omega
LaTeX-math-Omega
LaTeX-math-xi
LaTeX-math-Xi
LaTeX-math-psi
LaTeX-math-Psi
LaTeX-math-zeta
))

• This works well for the commands in the list, would be nice if it would be possible to detect the command manually in order to not miss something like like \rightarrow`. But this is a very good step toward a complete solution. – Name Nov 4 '16 at 16:51