Where is the encoding format stored for a .tex file? I had my editor set to latin1 by mistake when I created my file and I had to resort to

% !TEX encoding = UTF-8 Unicode

in the preamble to force UTF-8. I think the file still is encoded in latin1 and this modeline makes the editor ignore the actual encoding and force it to UTF-8.

  • The way encoding is treated depends on the editor you use: some assume a particular encoding, some try auto-detection, some respect 'magic comments' such as the one you have. Which editor are you using?
    – Joseph Wright
    Mar 27, 2014 at 15:58
  • TeXstudio 2.7.0 is the editor I use. I noticed I was in trouble when using åäöÅÄÖ and after I opened the file again they had been replaced with à and similar.
    – phell
    Mar 27, 2014 at 16:05
  • To change a file encoding it's usually easiest to copy-and-paste from the source encoding to the target encoding, and then just overwrite the source with the target.
    – Werner
    Mar 27, 2014 at 16:22
  • Does that give the same result as save as UTF-8 without BOM in notepad++?
    – phell
    Mar 27, 2014 at 16:34
  • Accented A are almost always a sign that the file is in UTF-8 but it is being read as latin1, Mar 27, 2014 at 18:03

2 Answers 2


You can tell LaTeX what character encoding you are using with for example \usepackage[utf8]{inputenc} or \usepackage[latin1]{inputenc}

With Emacs+AUCTeX the editor will recognize that line as well, and change the file's character coding if necessary. With other editors you might not be so lucky. Other than that, it's not really stored in the LaTeX file what encoding it uses, anymore than in other text files.

  • Your answer made me realize the difference between exchanging characters and just asking the editor to see it from a new perspective.
    – phell
    Mar 27, 2014 at 17:00
  • Sorry, but if you tell your editor the wrong encoding, things can become really strange.
    – Keks Dose
    Mar 27, 2014 at 17:10

If you tell LaTeX the encoding of a certain file were latin1, but it is utf-8 in reality, you might encounter strange effects and errors. Even worse: changing encoding while writing...

The encoding of a file is not stored in the file. It might be necessary to reencode your file, here is how to do that: How do I change the encoding of my files?

  • My editor always recognized the file as latin1 when re-opening it. That made me think there was encoding information stored somewhere. I think the editor guesses what the content is despite changing \usepackage[latin1]{inputenc} to utf8.
    – phell
    Mar 28, 2014 at 13:44

You must log in to answer this question.

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