• Linux Mageia 8
  • GNU Emacs 27.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.24, cairo version 1.16.0) of 2021-04-01



the mode is changed from Org to TeX.

I don't see in this file what could cause such a change. Weird, isn't it?

1 Answer 1


On line one you have this:

# Hey, Emacs!  This is a -*- mode: org -*- file!

And on line 3563 you have this:

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

Both are valid file variables, and both set the mode. When I open this file in Emacs, it starts out in LaTeX-mode. If I try to go to org-mode, it actually goes into an infinite loop of running mode hooks, noticing the file variables, changing to the other mode, running more hooks, changing back to the first mode, etc. Eventually it hits the max-lisp-eval-depth and whichever mode is still active wins by default.

I’m not sure that I can offer you much advice, except to hide the variables at the end from Emacs somehow. As long as it doesn’t contains the plain strings “Local Variables:” and “End:”, then Emacs won’t notice them. You could uuencode them, or rot13 them, or base64–encode them, or whatever. Then use an elisp source block to get what you want when you tangle this thing.

Or make both the org file and the generated LaTeX file both set the mode at the top of the file; I think Emacs only looks at the very first line of the file to find it (unless the first line contains a shebang or looks like a man page).

See chapter Specifying File Variables of the Emacs manual for more information.

  • I couldn't imagine file variables inside source code block would be taken in account. Removing mode: latex from them and adding % Hey, Emacs! This is a -*- mode: latex -*- file! at the top of the source code blocks corresponding to the tangled files did the trick. May 23 at 9:02
  • 2
    Emacs doesn’t know a thing about the syntax of the file, because it doesn’t know what mode to use for it. And even once it is in some mode, there’s no way for that mode to guide Emacs; the choice is already made.
    – db48x
    May 23 at 9:41
  • 2
    One way to do it is to make sure that the Local Variables block is NOT on the last "page" of the file. If you add a form feed character (that is a C-l) at the end of the file (inside a comment, if need be), then the last "page" of the file will be empty and Emacs will NOT look before that for the Local Variables block. To enter a C-l into the file, say C-q C-l to Emacs. Also, make sure to add a comment explaining what you did: six months from now, you will not remember :-)
    – NickD
    May 23 at 12:00
  • 1
    Another way to do it would be to move the Local Variables section into a separate file and then use either the #+SETUPFILE or #+INCLUDE directive. (I discovered this because I didn't want or expect it to have that effect). May 24 at 9:08

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.