1

Im currently working on a file where I need to display some Kanji in the middle of some English text. For this I created a new environment with the CJKuft8 package, according to this post

\usepackage{CJKutf8}
\newcommand{\japanese}[1]{\begin{CJK}{UTF8}{ipxm}#1\end{CJK}}

This works still just fine, but I have a slight problem with it: I'd like to change the font in which the Kanji are displayed (only the Japanese characters, not the rest of the text), but I don't know how to achieve that.

I found this answer, but it suggests using XeLaTeX, with which I am not familiar. Since I wanted to use Source Han Code JP, which is a OpenType Source font, I found also this question here, but there LuaLaTex was suggested.

After all of this I'm somewhat confused what the best strategy is to achieve my goal. Should I switch to XeLaTeX or LuaLaTex? Or can I even achieve my goal without changing to much (currently running pdflatex)?


Further Information: If possible I'd like to avoid having to switch to XeLaTeX or LuaLaTeX since the file is already quite big, has a lot of imported packages and I'm not familiar what is compatible with what.

7
  • 1
    pdftex can only use fonts with at most 256 characters so setting up cjk means arranging dozens of different fonts and macros working out which font has which character. There may be more font options (I do not know) but there won't be many, with luatex or xetex you could use any opentype font, most documents can easily be switched to one or the other, but it depends... Commented Aug 8, 2018 at 18:42
  • 2
    I have found that for this kind of typesetting you will save a lot of time in the long run if you switch to xelatex. There is an upfront cost (although in my experience it wasn't too bad), but it can save you hours of frustration.
    – Derek
    Commented Aug 8, 2018 at 19:32
  • And, of course, familiarize yourself with the fontspec package.
    – Derek
    Commented Aug 8, 2018 at 19:33
  • If you stick to pdfLaTeX, you have to convert an arbitary OpenType/TrueType font to a Type1 font and create a map file; that requires a lot of works and special knowledge about "how font is handled by TeX". Usually, the process cannot be completed automatically, and it's too difficult for beginners. IPAex font (= you are using that in the above MWE as "ipxm") is currently the only example which is contributed by a Japanese TeXnician, so I strongly recommend one of the followings: (1) use "ipxm" with pdfLaTeX (2) switch to LuaLaTeX. (LuaLaTeX is upper-compatible with pdfLaTeX in most cases) Commented Aug 9, 2018 at 2:04
  • @Derek Thank you for the suggestion, have done exactly that and after some minir changes everything works just fine!
    – Sito
    Commented Aug 9, 2018 at 12:19

2 Answers 2

5

If you stick to pdfLaTeX, you have to convert an arbitrary OpenType/TrueType font to a Type1 font and create a map file; that requires a lot of works and special knowledge about "how font is handled by TeX". Usually, the process cannot be completed automatically, and it's too difficult for beginners. IPAex font (= you are using that in the above MWE as "ipxm") is currently the only example which is contributed by a Japanese TeXnician, so I strongly recommend one of the followings:

  1. use "ipxm" with pdfLaTeX.
  2. switch to LuaLaTeX. (LuaLaTeX is upper-compatible with pdfLaTeX in most cases)

LuaLaTeX can handle OpenType/TrueType font natively. Here is a simple example of using Japanese fonts. (Here I used \font primitive directly but another LaTeX-like solution would be possible)

\documentclass{article}
\usepackage{fontspec}
\font\ipaexm=IPAexMincho % PSName of an arbitrary font
\font\ipaexg=IPAexGothic
\begin{document}
Type your content here.
{\ipaexm 日本語}
{\ipaexg 日本語}
\end{document}

lualatex example

2

Here’s an alternative MWE using babel. Unfortunately, there’s an incompatibility between babel 3.22, which loads all Japanese fonts with Script=Japanese, and fontspec 2.6h, which expects Script=Kana. I’ve provided a workaround. (Also, Source Han Code JP doesn’t support the Japanese-language OpenType feature, which is odd but probably harmless.)

Since you wanted to use Source Han Code JP, an OpenType font, this document compiles with XeLaTeX (and appears to work with LuaLaTeX, although as of 2018, its support for non-European scripts is more rudimentary.)

\documentclass[varwidth, preview]{standalone}
\usepackage{fontspec}
\usepackage[english]{babel}

% The Script= setting is broken for Japanese as of August 2018.
% Workaround:
\babelprovide[import=ja, script=Kana]{japanese}

\defaultfontfeatures{Scale=MatchUppercase, Ligatures=TeX}

\babelfont{rm}[
  Scale = 1.0,
  Ligatures={Common, TeX}]{Source Serif Pro}
\babelfont{sf}[Ligatures={Common, TeX}]{Source Sans Pro}
\babelfont{tt}{Source Code Pro}

% Font available at: https://github.com/adobe-fonts/source-han-code-jp
\babelfont[japanese]{rm}[
  Ligatures = {Discretionary, TeX},
  UprightFont = *-Regular ,
  BoldFont = *-Bold ,
  ItalicFont = *-RegularIt ,
  BoldItalicFont = *-BoldIt ,
  Extension = .otf ]{SourceHanCodeJP}

\begin{document}
Text in \foreignlanguage{japanese}{日本語}.
\end{document}

Text in Nihongo

2
  • There is another approach: \newfontscript{Japanese}{kana}. It could be done by babel itself, because the ini file already contains this info, so very likely I'll add in the next release of babel (I'd like to stick to the basic script names as provided by the Unicode CLDR). But I have to think a bit about it before. Commented Aug 25, 2018 at 8:09
  • @JavierBezos Thanks for replying! I understand the argument for using the Unicode script names. Perhaps the reason fontspec does not do that is to disambiguate between Japanese the language and Japanese the script? But that’s relevant to the public interface of babel, right? Internally, if it’s built on top of fontspec, it had better be compatible. You could always use the raw OpenType tag. :)
    – Davislor
    Commented Aug 25, 2018 at 17:37

You must log in to answer this question.

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