10

I want auto-complete (ac) to work when I am inside AucTeX mode. I get all packages from MELPA by package installation.

This is my relevant code in init.el file:

(require 'package)
(package-initialize)
(require 'auto-complete-config)
(ac-config-default)   
(global-auto-complete-mode t)

as you can see, although I am setting ac globally it does not appear in AucTeX. However, by following instructions in this emacswiki, ac mode was displayed in status bar in AucTeX but non-functioning, no auto-completion by typing characters.

Notes
Emacs 24.4 under Windows 7 32bit and packages were updated.

  • Your problem is to make ac work together yasnippet or just alone, while in LaTex mode? In the latter case, I use ac with AUCTeX without much work. – giordano Jan 1 '15 at 0:14
  • 1st, I want to have ac do well with auctex. I mentioned yasnippet because I also use it to have some user-defined snippets in latex. So until you provide your code I cannot tell if yas will get in the way of ac. – doctorate Jan 1 '15 at 8:18
  • Tone, please! Try taking out yasnippet to see if autocomplete and AUCTeX work together as @giordano suggested. If yes, then the problem is with the inclusion of yasnippet. If so, it's probably your autocomplete setup. – Dan Jan 1 '15 at 11:48
  • We can forget about yasnippet I removed it from my init.el. The problem is most probably in the setup of auctex and ac. – doctorate Jan 1 '15 at 14:48
  • 1
    See a package by Christopher Monsanto github.com/monsanto/auto-complete-auctex. I haven't test it. – Name Jan 2 '15 at 13:09
11

After doing a long research in the web and careful examination of ac documentation with a lot of trials on my machine to examine code line by line, I came up with this solution that can have yasnippet working as well (optioinal) without any confict.

put this in your init.el or .emacs taking care of sequence:

(require 'package)
(package-initialize)

;; yasnippet code 'optional', before auto-complete
(require 'yasnippet)
(yas-global-mode 1)

;; auto-complete setup, sequence is important
(require 'auto-complete)
(add-to-list 'ac-modes 'latex-mode) ; beware of using 'LaTeX-mode instead
(require 'ac-math) ; package should be installed first 
(defun my-ac-latex-mode () ; add ac-sources for latex
   (setq ac-sources
         (append '(ac-source-math-unicode
           ac-source-math-latex
           ac-source-latex-commands)
                 ac-sources)))
(add-hook 'LaTeX-mode-hook 'my-ac-latex-mode)
(setq ac-math-unicode-in-math-p t)
(ac-flyspell-workaround) ; fixes a known bug of delay due to flyspell (if it is there)
(add-to-list 'ac-modes 'org-mode) ; auto-complete for org-mode (optional)
(require 'auto-complete-config) ; should be after add-to-list 'ac-modes and hooks
(ac-config-default)
(setq ac-auto-start nil)            ; if t starts ac at startup automatically
(setq ac-auto-show-menu t)
(global-auto-complete-mode t) 

Notes:
as mentioned in the documentation of ac, the adding to ac-modes and hooks should be done before loading (ac-config-default).

Some suggested to put some code to play with TAB key which I didn't need and do not recommend as it will somehow destroy something else later. However, I saw people put something like this at the end just before global-auto-complete:

(ac-set-trigger-key "TAB")
(ac-set-trigger-key "<tab>")  

Until I added (ac-flyspell-workaround) auto-completion was too slow to think of using it in Auctex since I use flyspell. This bug was also mentioned in the documentation. Thanks!

Bonus
I was glad to know from the documentation, though not very clear how, that one can add a user-defined dictionary(ies)! I tried this and worked well.

add these two lines just after require-auto-complete line above:

(add-to-list 'ac-dictionary-directories "~/.emacs.d/.dict") ; make sure this folder exists
(add-to-list 'ac-user-dictionary-files "~/.emacs.d/.dict/custom-dict.txt") ; put any name to your `.txt` file  

Now in your .txt file add your favorite candidates for completion. Save yourself from candidates fewer than 4-letter long, they don't worth it! Simply put words of your like separated by a line feed or RET.

Example of the contents in the .txt file:

inconsistencies
foobaremaild@foobar.com
do-not-put-your-password-here
long-line-in-any-programming-language-of-your-like

Enjoy auto-completion in LaTeX/AUCTeX with Emacs!

  • it would be nice if there would be an elisp function to throw any marked word in a buffer while writing into that .txt dictioinary on-the-fly and append it without much effort! – doctorate Jan 2 '15 at 13:39
5

As I told in my above comment, the package auto-complete-auctex can also solve the problem. I tested both, and they both work nicely. It would be nice to compare the solution of @doctorate and the above package by giving screen-shots. If I write $\sig$, the code of doctorate give something like this

enter image description here

and that of the package auto-complete-auctex gives something like this

enter image description here

Added: I should mention that the solution of @giordano works as well, here a screen-shot:

enter image description here

  • I think auto-complete should be the main package while auto-complete-auctex might have some extra features. I will try it and report if necessary. Thanks – doctorate Jan 2 '15 at 15:24
  • 2
    I have installed the package but I am unable to open the popup with the completions... How did you do it and is there some documentation on this one? – Raven Mar 27 '17 at 8:33
  • @Raven I'm not sure I have it "really" working. But if you add in the TAB key steps that doctorate said to avoid, pressing TAB makes it work (for me). – Chill2Macht Feb 27 '18 at 0:45
4

The following is my auto-complete related configuration:

(require 'package)
(package-initialize)
(require 'auto-complete-config)
(ac-config-default)

;; Activate auto-complete for latex modes (AUCTeX or Emacs' builtin one).
(add-to-list 'ac-modes 'latex-mode)

;; Activate ac-math.
(eval-after-load "latex"
  '(when (featurep 'auto-complete)
     ;; See https://github.com/vspinu/ac-math
     (require 'ac-math)
     (defun ac-latex-mode-setup ()       ; add ac-sources to default ac-sources
       (setq ac-sources
         (append '(ac-source-math-unicode ac-source-math-latex ac-source-latex-commands)
             ac-sources)))
     (add-hook 'LaTeX-mode-hook 'ac-latex-mode-setup)))

As you can see, there is nothing special for AUCTeX, it's the standard configuration of auto-complete. The configuration for ac-math is described on its web site. If you don't use ac-math, the only sensible difference with your configuration is (add-to-list 'ac-modes 'latex-mode).

  • -1: Use package-initialize. You may not like it, but please don't recommend bad practices such as manual load-path management in your answers. – lunaryorn Jan 2 '15 at 12:36
  • 1
    @lunaryorn I don't think I recommended bad practices, I clearly wrote it's a dirty hack just to load an auxiliary package. I'll try to improve the answer using package-initialize later anyway. – giordano Jan 2 '15 at 12: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.