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!