1

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.)

Omar
  • 4,732
  • 1
  • 17
  • 32
  • 1
    AUCTeX provides `LaTeX-find-matching-begin` (`C-M-a`) and `LaTeX-find-matching-end` (`C-M-e`). Have you tried them? – Arash Esbati Jul 07 '18 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 Jul 08 '18 at 00:47
  • 2
    The package `latex-extra` on `melpa` adds support for that kind of navigation to auctex – João Cortes Oct 20 '18 at 16:27
  • 1
    I wish AUCTeX could simply re-use the functionality of the built-in tex-mode instead. – Stefan Oct 20 '18 at 18:30

1 Answers1

1

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.

Omar
  • 4,732
  • 1
  • 17
  • 32
  • 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. – Arash Esbati Jul 08 '18 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 Jul 09 '18 at 22:45