4

A little while ago I asked some questions about the Greek language in LaTeX and got some excellent responses. In the same context I'm looking now to the Korean Language but I'm having problems setting the typewriter / mono-spaced font.

Any suggestions how to handle this?

My MWE:

\documentclass[twoside]{book}
\tracinglostchars=2

\usepackage{xeCJK}

\setmainfont{Noto Sans CJK SC}[Language=Korean]

\begin{document}
\obeylines
00027         printf({"{}JUST A PRINT STATEMENT\(\backslash\)n"{}});
00027         printf({"{}Just a print statement\(\backslash\)n"{}});
00028         printf({"large 다음에 의해 생성됨 Doxygen 1.8.19\(\backslash\)n"{}});
\begin{verbatim}
00027         printf({"{}JUST A PRINT STATEMENT SOME MORE TEXT SOME MORE TEXT\(\backslash\)n"{}});
00027         printf({"{}Just a print statement some more text some more text\(\backslash\)n"{}});
00028         printf({"large 다음에 의해 생성됨 Doxygen 1.8.19\(\backslash\)n"{}});
\end{verbatim}
\end{document}
  • Try \setmonofont{Noto Sans Mono CJK KR}. – Cicada Jun 20 at 12:08
  • Unfortunately this does not work, got message like:Couldn't open `Noto Sans Mono CJK .cfg' (I'm using MikTeX). – albert Jun 20 at 12:13
  • The font name is Noto Sans Mono CJK KR. Why are you using xeCJK? Use fontspec instead, if you don't need it. – Cicada Jun 20 at 12:18
  • @Cicada As Marijn brings up, with xecjk you want \setCJKmonofont{Noto Sans Mono CJK KR}. You might also wish to set some additional features such as Script= and Language=. – Davislor Jun 20 at 16:22
4

With the package xeCJK you should use \setCJKmainfont to set the font for the CJK characters. The \setmainfont command is also used, but only to set the font for non-CJK characters.

MWE:

\documentclass{article}
\tracinglostchars=2
\usepackage{xeCJK}

\setCJKmainfont{Noto Sans CJK SC}
%\setCJKmonofont{Noto Sans CJK SC} optional: set separate monospace font

\begin{document}
\obeylines
00027         printf({"{}JUST A PRINT STATEMENT\(\backslash\)n"{}});
00027         printf({"{}Just a print statement\(\backslash\)n"{}});
00028         printf({"large 다음에 의해 생성됨 Doxygen 1.8.19\(\backslash\)n"{}});
\begin{verbatim}
00027         printf({"{}JUST A PRINT STATEMENT SOME MORE TEXT\(\backslash\)n"{}});
00027         printf({"{}Just a print statement some more text\(\backslash\)n"{}});
00028         printf({"large 다음에 의해 생성됨 Doxygen 1.8.19\(\backslash\)n"{}});
\end{verbatim}                                                                                                                      
\end{document}

Result:

enter image description here

Optionally, you can set a separate font for different font families, such as sans serif, math, or monospace. For monospace the command is \setCJKmonofont. See the xeCJK manual on page 6 and further for an overview of the commands for font families.

| improve this answer | |
  • OK so the script automatically switches to the mono spaced font in the verbatim environment? The next problem is with the \chapter command that should be translated to korean, but I think this is a separate issue. – albert Jun 20 at 12:29
  • Regarding the manual, unfortunately it is in Chines and I don't read Chines (nor Korean). – albert Jun 20 at 12:32
  • @albert for the chapter names you can find some information in tex.stackexchange.com/questions/210397/…. – Marijn Jun 20 at 12:40
  • Unfortunately didn't help ... I;ll create a new question for it. – albert Jun 20 at 12:50
  • For switching to monospace: xeCJK gives a warning when you don't set the mono font explicitly and you do use a verbatim environment - the warning is that the CJKttdefault family (tt for 'teletype', i.e., monospace) is not found and is being ignored, so the main font is used in verbatim. A difference is that in verbatim the space characters are printed, while in the main text xeCJK handles character spacing. So if you want to do it 'properly' and not get the warning then you should set the mono font always, even if you don't actually use a different font for this. – Marijn Jun 20 at 12:50
3

The following compiles without errors using xelatex on TeX Live 2020:

\documentclass[twoside]{book}
\tracinglostchars=2

\usepackage{xeCJK}

\defaultfontfeatures{Scale = MatchLowercase, Ligatures = TeX}
\setmainfont{Noto Sans}[Scale = 1.0]
\setmonofont{Noto Sans Mono}
%% The Noto Korean fonts can be downloaded from:
%% https://www.google.com/get/noto/help/cjk/
\setCJKmainfont{Noto Sans CJK KR}[Script=Hangul, Language=Korean]
\setCJKmonofont{Noto Sans Mono CJK KR}[Script=Hangul, Language=Korean]

\begin{document}
\obeylines
00027         printf({"{}JUST A PRINT STATEMENT\(\backslash\)n"{}});
00027         printf({"{}Just a print statement\(\backslash\)n"{}});
00028         printf({"large 다음에 의해 생성됨 Doxygen 1.8.19\(\backslash\)n"{}});
\begin{verbatim}
00027         printf({"{}JUST A PRINT STATEMENT SOME MORE TEXT SOME MORE TEXT\(\backslash\)n"{}});
00027         printf({"{}Just a print statement some more text some more text\(\backslash\)n"{}});
00028         printf({"large 다음에 의해 생성됨 Doxygen 1.8.19\(\backslash\)n"{}});
\end{verbatim}
\end{document}

You must first download the fonts you need, such as Noto Sans CJK KR. On Windows or Mac, make sure you install them as system fonts, and not just for yourself.

They are not distributed through CTAN, but you can put the files in any of the following places:

  • Install them as system fonts. On Windows or Mac, make sure to install them for all users, so XeTeX can find them.
  • Some Linux distributions package them and keep them updated. On Debian or Ubuntu, they are in the apt package fonts-noto-cjk.
  • Copy them to a local TEXMF tree. With MikTeX, you would set the environment variable TEXINPUTS= to this local directory.
  • Download them to a local folder and select them with Path=, e.g. \setCJKmonofont{NotoSansMonoCJKkr}[Path = ./customfonts , UprightFont = *-Regular , BoldFont = *-Bold , Extension = .otf] (If this is the version you downloaded.) This will break if you try to compile the document on another computer with a different version of the fonts. On the other hand, it’s the only way to be certain that you’re archiving the exact same version of the fonts, with the exact same output.
  • Put the files, or symbolic links to them, in your project directory along with your .tex files and change to that directory to compile them.
  • On Linux, you can copy fonts to a subdirectory of /usr/local/share/fonts/ to install them for everyone, or ~/.fonts/ to install them for yourself.

The reason only the Simplified Chinese fonts were working for you is that there is a CTAN package to install those.

| improve this answer | |
  • 1
    I think the same accounts for the Japanese language as well. – albert Jun 20 at 17:14
  • @albert I believe so. On the other hand, a CTAN file search cannot find NotoSansCJKsc-Regular.otf either, even though it is definitely in the tree. – Davislor Jun 20 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.