I have lots of LaTeX files, and my AucTeX has been set up to automatically open up all *.tex files in LaTeX-mode. But more recently I've started using ConTeXt, and for those files (which also end in .tex) I'd like to use ConTeXt-mode. Is there any way of embedding something in the file which will tell Emacs to open up the correct mode?

I can start a file with, for example

-*- mode: context; -*-

which works well, but even if it's the very first line in the file, ConTeXt still parses it and writes it to the output, although it comes way before \starttext.

Does anybody have any better suggestions? I suppose the sensible thing would be to move all ConTeXt files into one directory tree, and set up a .dir-locals.el file in that directory, which would tell Emacs to open up all .tex files using ConTeXt mode. But I'd still like to be able to do it on a file-by-file basis.

FWIW, I'm using Linux.


  • 6
    By the way: are there really no acceptable answers for all the 8 questions you have asked up to now? If there are, please click the accept button on the best answer each (it's the checkmark like symbol below the up/down icons). Mar 19, 2011 at 1:17

2 Answers 2


Can't you simply put a % in front of this line?

I'm using this in the last some lines of my files, for example.

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: "spezialisierung"
%%% End: 


% -*- mode: context; -*-

in the first line should work, too.


As an alternative to manually setting the mode file local variable (which is preferred because it does not rely on heuristics), AucTeX automatically recognizes the TeX format! It knows about LaTeX (detected via \documentclass), ConTeXt (detected via \starttext) and Plain TeX. For example, you can do the following:

  1. Create a file abc.tex. AucTeX starts in LaTeX mode by default.
  2. Enter \starttext.
  3. Hit C-c C-n to force reparsing of the buffer.
  4. AucTeX automatically detects that you plan to write a ConTeXt document and switches to ConTeXt mode.
  5. Hit M-x add-file-local-variable RET mode RET RET to add the mode as a file-local variable. Then the file will always trigger ConTeXt mode even if the auto-recognition fails.
  • 2
    +1 C-c C-n was very helpful. Yours should be the correct answer.
    – doctorate
    Feb 13, 2016 at 15:54

You must log in to answer this question.

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