I generally prefer AucTeX over the built-in tex-mode, but one thing that is much better in tex-mode is navigation by s-expression. Namely, in tex-mode, forward-sexp (C-M-f) treats LaTeX environments as s-expressions; with the point right before \begin{foo}, forward-sexp will take you to after the corresponding \end{foo}. Other sexp based functions also do the right thing, for example backward-up-list (C-M-u) will take you to the enclosing \begin{foo}.

How can I get this functionality in AucTeX?

(I noticed tex-mode defines a latex-forward-sexp and sets forward-sexp-function to that function. I tried loading tex-mode after AucTeX and evaluating (setq-local latex-forward-sexp #'latex-forward-sexy) but that didn't seem to change the behavior of forward-sexp in the AucTeX buffer.)

  • 1
    AUCTeX provides LaTeX-find-matching-begin (C-M-a) and LaTeX-find-matching-end (C-M-e). Have you tried them? Commented Jul 7, 2018 at 19:33
  • Those are great, @ArashEsbati, and I was unaware of them. This goes a long way to satisfying my sexp-navigation needs, but forward-sexp in tex-mode also knows about $math$, \(math\) and \[math\].
    – Omar
    Commented Jul 8, 2018 at 0:47
  • 2
    The package latex-extra on melpa adds support for that kind of navigation to auctex Commented Oct 20, 2018 at 16:27
  • 1
    I wish AUCTeX could simply re-use the functionality of the built-in tex-mode instead.
    – Stefan
    Commented Oct 20, 2018 at 18:30

2 Answers 2


After stepping through the latex-forward-sexp-1 function from tex-mode (long live edebug!), I figured out why it works in tex-mode buffers, but not in AucTeX: it's because tex-mode gives the backslash syntax class /, but AucTeX gives it syntax class \!

So, I've simply added this to my AucTeX configuration to get tex-mode's better sexp-navigation:

(autoload #'latex-forward-sexp "tex-mode" nil t)
(modify-syntax-entry ?\\ "/" LaTeX-mode-syntax-table)
(defun fix-LaTeX-sexp ()
   (setq-local forward-sexp-function #'latex-forward-sexp))
(add-hook 'LaTeX-mode-hook #'fix-LaTeX-sexp)

(I like how wrong it feels to load tex-mode in my AucTeX configuration. :P)

I'd love it if someone comes up with a different approach.

  • Changing the syntax class for ?\\ globally in LaTeX-mode-syntax-table looks like asking for trouble to me. Why not using with-syntax-table to change the syntax class locally and execute your function? The original syntax table is restored afterwards. Commented Jul 8, 2018 at 19:26
  • @ArashEsbati, it doesn't seem like anything broke in this case, but you're right that in general it is safer to make the change locally.
    – Omar
    Commented Jul 9, 2018 at 22:45

The package https://github.com/ultronozm/tex-parens.el is motivated by questions like this concerning sexp/list-based navigation in AUCTeX

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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