After the very brief introduction to Japanese typesetting and the available options in the previous blog post, let us now dive into some technical explanations concerning character sets and encodings.
In this blog post we will discuss character sets and encodings, something one has to think about already when using non-ASCII characters in e.g. European languages, but even more when writing Japanese (or any other ideographic writing system with thousands of glyphs). We also lift the eternal mystery about the ¥ appearing in many places instead of the \ in Japanese documents.
First of all, be reminded that I am not an expert in this area, so many of my explanations here are simplified from the real world, often incomplete, and maybe sometimes plain wrong (hint: leave a comment!).
In the ASCII worlds, everything was simple: Representing a letter like "A" in a computer, a certain code was decided (65), and that was at the same time the byte value. So in the ASCII world, character set and encoding coincide.
Already when non-ASCII languages asked for inclusion, the problem started. Here it was always about character-set-definition-and-encoding in one. We defined Latin1 as containing these characters, like "Ö", and assigned code points at the same time (214 for Ö). This worked because by having 256 possible values for character codes, we could satisfy all the alphabets we tried.
But with thousands and tens of thousands of kanji, this is not possible anymore. What is needed is a separation. On the one hand a standardization of the glyphs, assigning each kanji a unique id (number). And then a way to encode these numbers in a good way.
UCS – the Universal Character Set, contains nearly one hundred thousand abstract characters, trying to include all writing systems in history. There is only a slight hickup when in comes to Japanese, and that is the Han unification, where the similar ideographs in different languages were mapped to the same code point.
The other question is how to represent these characters in a computer system, i.e., in bits and bytes. Encoding standards are often tied closely with character set standards, which creates some confusion.
When it comes to TeX (and probably all computerization nowadays), the following encoding mechanisms are in common use. The first three (SJIS, JIS X 0202, EUC-JP) deal with character set defined by JIS standards (in various levels), while the last one (UTF-8) is one encoding for the Unicode character set standard.
JIS X 0202 – often referred to shortly as JIS for data in JIS X 0208. This encoding is good for transmitting over (only) 7bit capable channels, like email. Old versions (and maybe even now?) of Emacs used this encoding internally.
EUC(-JP) Extended Unix Code – actually a family of encodings for ISO-2022 compliant character sets. This encoding was strong in Unix-affine computer systems in Japan for many years.
In UTF-8 we see that the first line is the one-byte encoding, i.e., ASCII, of "Language". The second line shows that accented characters are encoded in two bytes (separated by space 0x20). Finally both the kanjis on the third line and hiragana on the fourth line are encoded into three bytes.
In SJIS we see some interesting effects: First, as expected, the ASCII part remains the same, the first line. But the second line exhibits problems – the Umlaut-A Ä is not encoded in the underlying character set, so SJIS cannot represent it. What my recode program here did is replacing Ä with "A (0x22 0x41). On the other hand, á is in the character set of SJIS, and is encoded in three bytes: 0x81 0x4c 0x61. Here you see the problem with parsers mentioned above. The 0x4c = L and 0x61 = a, the same "La" occurring at the beginning of the first line. So a simple search for "La" will show up two occurrences, which is wrong.
The kanjis in the third line and hiragana in the forth line take up each only two bytes.
A very similar problem as in SJIS arises in ISO2022-JP: As the Ä is not encoded, it is replaced with "A by my recode command. The á on the contrary takes up 9 bytes! And both the kanji and hiragana are encoded in a non-linear non-fixed way.
Since EUC-JP encodes the much bigger JIS X 0212 character set, the Ä is not lost. Both diacritical latin characters in line two are encoded in 3 bytes each. The kanjis on line three and the hiragana on the last line are all endoded in two bytes each.
From the above, you can easily see why UTF-8 wasn't initially well accepted in Japan, as all documents would increase in size, due to longer encoding per character. Other encodings provide shorter effective size of documents. Or maybe it was just the usual attitude of any sys-admin: "Never change a running system" that made the switch to UTF8 a long running project in Japan.
And what am I using?
Linux: in most cases you will have UTF-8 as encoding, check the output of locale command, in particular the value of LC_CTYPE, which in my case is en_US.utf8, indicating the language and the encoding.
Windows: Again, older versions (before XP??) used Microsoft's version of ShiftJIS, called Code Page 932. Newer versions hopefully use again Unicode/UTF-8.
If you start writing Japanese text, you should be aware of what character encoding you are using, because it somehow defines what engine you can use. The most widely accepted character set is UTF-8, as newer versions of pTeX accept also utf-8, and uptex, xetex, luatex expect UTF-8 natively, and for BXcjkjatype, too, you need UTF-8 input.
But you have to be aware that you will find many old files shared with you to be in one or another ShiftJIS encoding, which can as is only compiled with platex, or after conversion to UTF-8 with any engine.
The single-byte characters 0x00 to 0x7F match the ASCII encoding, except for a yen sign (U+00A5) at 0x5C and an overline (U+203E) at 0x7E in place of the ASCII character set's backslash and tilde respectively.
That means, in Shift JIS, the ¥ sign is encoded with the same code as the backslash in ASCII. Now TeX expects the 0x5C as the excape character by default, and that is the reason why on computers working with the ShiftJIS encoding as basis (older Windows, older Mac, any Unix with locale set to ShiftJIS), the commands of TeX will show up as ¥hfill etc.
After this a bit hard-core entry, we will return to hands-on tutorial in the next blog post, by writing Hello-World like documents for all the engines.
Enjoy, and please leave remarks and suggestions for improvements, and corrections for my misunderstandings, here!