2

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

MWE:

\documentclass{article}

\newcommand{\rndvar}{X}

\begin{document}

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

\end{document}

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

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

1 Answer 1

3

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
                                    (append
                                     (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 [email protected].

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .