5

I installed auto-complete and also auto-complete-latex so to be able to use it with LaTeX files. Because in a document I use an external file with the preamble commands in it, Emacs recognises only the external one as a LaTeX file and the other is recognised as a plain TeX file, preventing the auto-completion. What can I do so fix this?

I don't use AUCTeX.

Adam
  • 2,407
  • 2
  • 21
  • 38

1 Answers1

5

When Emacs opens a .tex file, it runs the tex-mode function, which tries to guess what format the file is for and invokes plain-tex-mode or latex-mode (or slitex-mode) accordingly.

If you want to force a specific file to be opened in LaTeX mode, use a file variable. Put this line near the top of your .tex file:

%% -*-latex-*-

or these lines near the bottom:

%% Local Variables:
%% mode: latex
%% End:

This applies to both Emacs's default tex-mode and to AUCTeX. Both use a configuration variable which defaults to LaTeX for an empty file (tex-default-mode for tex-mode, TeX-default-mode for AUCTeX). Apart from that, they have similar but not identical heuristics to guess between LaTeX and plain TeX. Both select LaTeX if the first command in the file is an environment or a LaTeX sectioning command. Look at the code of tex-guess-mode (built-in) or TeX-tex-mode (AUCTeX) if you want to know the details.

If you want to force all files to use LaTeX mode regardless of content, AUCTeX lets you do that easily with the following line in your init file (you can also use the Customize interface to set this variable):

;; Always use `TeX-default-mode', which defaults to `latex-mode'
(setq TeX-force-default-mode t)

With Emacs's default tex-mode, there's no similar variable. You could instead redefine the tex-guess-mode function. To systematically force LaTeX mode, you can use

(defalias 'tex-guess-mode 'latex-mode)

Alternatively, for both tex-mode and AUCTeX, you can change the mode associated with .tex files:

(add-to-list auto-mode-alist '("\\.[tT]e[xX]\\'" . latex-mode))

Auto-complete-latex attaches itself to latex-mode-hook (and LaTeX-mode-hook for AUCTeX, and yatex-mode-hook for YaTeX), so if your buffer is in LaTeX mode, it should be active.