# TeXShop doesn't remember file encoding

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?

-
You need to choice utf-8 in the prefs of TexShop "Document" –  Alain Matthes Feb 29 '12 at 5:47
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. –  Alain Matthes Feb 29 '12 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 Feb 29 '12 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 Feb 29 '12 at 9:49

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).

-
Another point worthy a note is the possibility of setting the encoding via a "magic line" `% !TEX encoding = UTF-8 Unicode` –  egreg Feb 29 '12 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. –  Alain Matthes Feb 29 '12 at 8:03
@egreg: Oh this is very neat as well since I don't have to care about editor settings then. –  Philipp Feb 29 '12 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 Feb 29 '12 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. –  Alain Matthes Feb 29 '12 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.

-
+1 Yes it's interesting for a lot of users. –  Alain Matthes Feb 29 '12 at 9:50
Do you know which one of these flavors work in Texmaker or TeXStudio? –  egreg Feb 29 '12 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 Feb 29 '12 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. –  Alain Matthes Feb 29 '12 at 14:28
@Daniel kate has modelines? Are they documented someplace? –  cfr Jan 19 at 23:22