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.

  • 1
    Are you using AUCTeX? – Sean Allred Nov 16 '14 at 5:12
  • @SeanAllred no I write the code and compile it through the terminal. I like it better that way. – Adam Nov 16 '14 at 13:37
  • 1
    AUCTeX has benefits other than compilation (but its compilation feature is useful, especially with multifile documents like yours). – Gilles Nov 16 '14 at 13:53
  • @Gilles thank you for the recommendation I will check it out. – Adam Nov 16 '14 at 15:45
  • BTW: AUCTeX (sometimes) has the same problem. – mbork Nov 16 '14 at 16:17

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.

  • Thank you I put the line in the top of my file and it worked. Is there any way to set it in my .emacs file globally? – Adam Nov 16 '14 at 14:09
  • @Adam See my edit – Gilles Nov 16 '14 at 17:21
  • @Gilles Doesn't AUCTeX use TeX-default-mode actually? Note the difference in capitalization. – lunaryorn Nov 16 '14 at 18:26

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.