A .tex file is ultimately bytes, and the engine must read them in the right character encoding to interpret them correctly. Newline codes matter too, for editing and diffs. Japanese has historically juggled several encodings, which is where people trip. The short answer: today, standardize on UTF-8 + LF. This page covers the encodings, the newline codes, and the nkf conversion tool.
Encodings — UTF-8 today
A character encoding is the rule mapping bytes to characters. Japanese has historically used Shift_JIS (older Windows), EUC-JP (older Unix), and ISO-2022-JP (email’s “JIS code”); today Unicode’s UTF-8 is the universal standard. Recent TeX Live and LaTeX default to UTF-8, so saving your .tex as UTF-8 avoids most garbling. A mismatch in encoding produces the classic garbled text (*mojibake*).
| Encoding | Where it was used |
|---|---|
UTF-8 | Today’s standard (Unicode); the only choice for new work |
Shift_JIS | Older Windows environments |
EUC-JP | Older Unix environments |
ISO-2022-JP | Email, “JIS code” |
For (u)pLaTeX, you can state the input encoding explicitly with -kanji=utf8 (see “Compile commands”).
Newline codes — LF / CRLF / CR
Lines end with invisible control bytes: LF (\n, Unix/macOS), CRLF (\r\n, Windows), and CR (\r, classic Mac). LaTeX itself tolerates all of them, but mixed newlines within a file or across a team make diffs messy and trip up some tools. Standardize on LF, and normalize with .gitattributes under Git.
# .gitattributes — 改行を正規化 / normalize newlines
*.tex text eol=lfConvert with nkf
nkf (Network Kanji Filter) is the standard tool to detect and convert Japanese encodings and newlines. First inspect with nkf -g file; then convert to UTF-8 with -w, Shift_JIS with -s, EUC-JP with -e, or ISO-2022-JP with -j. Newlines use -Lu/-Lw/-Lm, and --overwrite rewrites the file in place.
| Option | What it does |
|---|---|
-g | Detect the current encoding and newline (no conversion) |
-w | Convert to UTF-8 (no BOM) |
-s / -e / -j | Convert to Shift_JIS / EUC-JP / ISO-2022-JP |
-Lu / -Lw / -Lm | Convert newlines to LF / CRLF / CR |
--overwrite | Overwrite the input file in place |
nkf -g old.tex # 文字コードを判定 / detect the encoding
nkf -w -Lu --overwrite old.tex # UTF-8 + LF に変換して上書き / to UTF-8 + LF, in placeRecommended in practice
- Standardize everything on UTF-8 + LF, new or old.
- Batch-convert legacy Shift_JIS/EUC sources with
nkf -w -Lu --overwrite *.tex. - Force UTF-8 input for (u)pLaTeX with
-kanji=utf8when needed. - Normalize newlines via
.gitattributesunder Git; check withnkf -gwhen unsure.