I am aware that this might not be the ideal place please move/close this if so.

So since yesterday I run into the following problem:

I use \usepackage[T1]{fontenc} and \usepackage[utf8]{inputenc} in the preamble and save the document in TeXShop accordingly as a UTF-8 encoded file.

As soon as I close the document and open it anew the document is now of type applemac and obviously the special characters are all different. Now this only seems to happen for new document because I wrote my thesis with the same preambel and there when I open it it is still a UTF-8 encoded document.

I really have no idea what's going on. Has anyone ever experienced something similar?

  • 4
    You need to choice utf-8 in the prefs of TexShop "Document" Commented Feb 29, 2012 at 5:47
  • 2
    It's not enough to choice utf8 when you save your file (save as, I suppose) because when you open a new file, it's the encoding of the Prefs that it's used. Commented Feb 29, 2012 at 6:13
  • Thank you, that did the trick. Changed that in the settings, created a new document, copied all the code into the new file and now it remembers its codec. Now I'm just wondering: did it work for my thesis document because it wasn't initially created by TeXShop (I got a template from a friend)? Also, post this as answer if you like, I'll accept it as soon as possible (if this question doesn't get closed).
    – Philipp
    Commented Feb 29, 2012 at 7:14
  • Reliably detecting the encoding is tricky, especially if the document itself (so far) uses only ASCII characters. Nevertheless, some editors (vim) do a quite decent job here; unfortunately, TexShop is less clever in this respect.
    – Daniel
    Commented Feb 29, 2012 at 9:49
  • I set the default encoding to UTF-8 which seemed to have solved it for me.
    – H.Rabiee
    Commented Jul 22, 2016 at 2:27

2 Answers 2


It's not enough to choose utf8 when you save your file (save as, I suppose) because when you open a new file, it's the encoding of the Prefs that it's used. You need to choose utf-8 in the prefs of TexShop "Document" .

Now you need to know the encoding of your initial file. Then you need to know the state of the actual document. There are some useful tools to change the encoding like

  iconv -f ISO-8859-1 -t UTF-8 myfilename.xxx > myfilename-utf8.xxx

For TeXShop there's a very convenient way: if you put at the beginning of the file the line (it can actually be among the first twenty lines of the file)

% !TEX encoding = UTF-8 Unicode

then the file will be opened and saved as UTF-8 no matter what the global preferences of TeXShop are set up to. The "Encoding" entry in the "Macros" menu allows to write this line after choosing among several encodings.

Note that the line is understood also by TeXworks. I call such lines the "magic lines". With a similar method one can set also the root file or the typesetting engine (check the documentation).

  • 6
    Another point worthy a note is the possibility of setting the encoding via a "magic line" % !TEX encoding = UTF-8 Unicode
    – egreg
    Commented Feb 29, 2012 at 7:39
  • @egreg Yes you are right I forgot this because I work always with utf8 and utf8 is the default encoding with Textmate. Commented Feb 29, 2012 at 8:03
  • @egreg: Oh this is very neat as well since I don't have to care about editor settings then.
    – Philipp
    Commented Feb 29, 2012 at 8:16
  • @egreg: I was about to post this as an answer when I saw your comment. IMHO this is the real answer to the OPs problem, so it should become one.
    – Daniel
    Commented Feb 29, 2012 at 8:20
  • @Daniel I think in some cases (editors) the "magic line" % !TEX encoding = UTF-8 Unicode is unknown. This is fine with Texshop, textmate (actually) but with the first version of Textmate this option did not work. Commented Feb 29, 2012 at 8:35

Many editors, including TexShop, provide the concept of modelines, "magic comments" in which one can specify various editor settings. This provides a per-document configuration of the editor. The following snippets sets the encoding to utf-8 for TexShop, Emacs and Vim:

% !TEX encoding = UTF-8 Unicode
% -*- coding: UTF-8; -*-
% vim: set fenc=utf-8

It is good practice to add something like this at the beginning to each document, especially if you collaborate with others or switch between different editors.

For most editors, such modelines have to appear in the first 10 lines of a document, some (Vim, Emacs) also support them at the end of the document (within the last 10 lines).

There are many more editors that support modelines and every has its own flavor on it. However, at least for simple settings (such as the file encoding), the Vim format has established as a de-facto standard, which is understood by most editors.

  • Do you know which one of these flavors work in Texmaker or TeXStudio?
    – egreg
    Commented Feb 29, 2012 at 9:51
  • @egreg: Sorry, I don't know. As Texmaker's editor is based on the generic Qt edit components, it might support kates modeline syntax. For TeXStudio I don't have a clue.
    – Daniel
    Commented Feb 29, 2012 at 13:40
  • I think some editors don't see anything except a comment. Some users think that it's not a good practice because % !TEX encoding= UTF-8 Unicode does not work if you make an error of syntax TS is case sensitive) . Then TeXshop can bypass this behavior, by hold down the option key while opening a file. And finally you could easily change the encoding of the file, by using iconv or some text editor, then the comment would not change and be wrong. Commented Feb 29, 2012 at 14:28
  • @Daniel kate has modelines? Are they documented someplace?
    – cfr
    Commented Jan 19, 2015 at 23:22
  • @cfr: For kate I have provided some more details in tex.stackexchange.com/questions/89859/… If you google for the example, you will easily find more.
    – Daniel
    Commented Jan 20, 2015 at 9:13

You must log in to answer this question.

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