In LaTeX-mode (AUCTeX) buffers, the backslash "\" is treated as an escape character. I'd like it to be treated as word constituent (syntax class "w") instead, but only in LaTeX-mode buffers. The main reason is that it's more convenient for moving and deleting word-wise (M-f etc), including macros with their backslashes.

Some answers to questions like Treat symbols as words in prog modes suggest to use "superword-mode", but that doesn't work for me because it changes the underscore syntax, which is not what I want. Others suggest to use

(modify-syntax-entry ?\\ "w")


(modify-syntax-entry ?\\ "w" ***-mode-syntax-table)

at appropriate places in the initialization file, where "***" is the current mode.

Unfortunately the first solution changes the backslash syntax globally The first solution is buffer-local, as @Stefan pointed out, and it works if inserted in the LaTeX-mode hook. But the second doesn't work if I replace "***" with "latex", "tex", "LaTeX", or "TeX".

Any idea why the second command above is not working in latex-mode buffers? Cheers!

  • 2
    You should probably describe why you want to make this change (is it because you want regexps like \w,\< to treat it as a word-constitutent, or because you want M-f or M-b or M-C-b or some other navigation command to jump to before/after rather than the reverse, ...). Note also that your first sample code does not necessarily change the syntax globally (it changes it in the table active in the current buffer when the code is executed). – Stefan Jan 29 at 13:12
  • Thank you Stefan, I'll update my question and add some motivation. You're right, I re-checked and the first command is indeed buffer-local, so it works if I insert it in the hook for latex mode. I still wonder why restricting it to latex-mode-syntax-table doesn't work, though. – pglpm Jan 29 at 13:25
  • 2
    Maybe it's just because you do it before the table is defined? And of course for AUCTeX, it's LaTeX-mode-syntax-table. – Stefan Jan 30 at 9:01
  • @Stefan I do at the very end of the LaTeX-mode hook. I've also done it after the buffer is loaded (calling the command within the buffer itself), but it doesn't work. I realize now that my question is probably better sent to the AUCTeX development team rather than on StackExchange... – pglpm Jan 30 at 13:06

Not heavily tested, but the following lines in your init file should do the trick:

(defun pglpm/modify-LaTeX-mode-syntax-table ()
  (modify-syntax-entry (string-to-char TeX-esc)

(add-hook 'LaTeX-mode-hook #'pglpm/modify-LaTeX-mode-syntax-table)
  • Thank you Arash, great suggestions as usual! – pglpm Jan 30 at 21:33

Your Answer

