I can't get AUCTeX to complete user-defined commands in math mode.

As an example, TeX-complete-symbol works outside of math mode in the document below, but it stops working if I prepend a dollar sign to the command name, returning TeX-complete-symbol: Can’t find completion for "\rnd".





\rnd%      TeX-complete-symbol works here
$\rnd$%    but not here,
$\rnd%     nor here.


I'm on GNU Emacs 25.2.2, AUCTeX is v. 12.1.2. My init.el consists of

(require 'package)
(require 'reftex)
  • 1
    Did you try $\rnd? – barbara beeton Mar 31 at 18:39
  • @barbarabeeton Yes ma'am. The behaviour is the same as when it's wrapped in dollar signs. – Arch Stanton Mar 31 at 21:08

If you open your example file above and do C-h v TeX-complete-list RET, you see in the value a list item like this:

(LaTeX--after-math-macro-prefix-p 1
                                  (lambda nil
                                     (mapcar #'cadr LaTeX-math-list)
                                     (mapcar #'cadr LaTeX-math-default)))
                                  (if TeX-insert-braces "{}"))

This means that in math expressions, the completion function looks in the variables LaTeX-math-list and LaTeX-math-default for completion candidates. LaTeX-math-list is the one for users to customize. As a test, you can add a comment like this to your document:

% (setq LaTeX-math-list '((nil "rndvar" nil nil)))

put the cursor after the last parenthesis and hit C-x C-e. Now try TeX-complete-symbol again. For me, it works and looks like this:

enter image description here

Now you have make this addition permanent. You can use the custom interface, make an addition on a file-local basis:

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% eval: (add-to-list 'LaTeX-math-list '(nil "rndvar" nil nil))
%%% End:

or even write your own AUCTeX style (would be my choice).

While running the completion, Emacs will probably throw a message like this in the Minibuffer:

TeX-complete-symbol: Wrong type argument: stringp, (if TeX-insert-braces "{}")

This seems to be a bug. Please report it to bug-auctex@gnu.org.

  • Do you know a way of adding every command I define to the LaTeX-math-list automatically? – Arch Stanton Apr 2 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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