9

I am typing a document in both Chinese and Hebrew. When I use the following codes for Chinese, everything works fine:

\documentclass[UTF8]{ctexart}

\begin{document}

    \tableofcontents

    \begin{abstract}
        这是简介及摘要。
    \end{abstract}

    \section{ 前言 }

    \section{关于数学部分}
    数学、中英文皆可以混排。You can intersperse math, Chinese and English (Latin script) without adding extra environments.

    這是繁體中文。

\end{document}

But when I tried to add Hebrew by adding

\usepackage[utf8x]{inputenc} \usepackage[hebrew,english]{babel}

It does not compile properly. The error message was

Font LHE/cmr/m/n/10=jerus10 at 10.0pt not loadable: Metric (TFM) file not found. \select@language{hebrew}

Anyone knows how to fix the problem?

Zuriel
  • 866
  • 1
    The problem is that, by default, babel is trying to load a legacy 8-bit font you don’t have installed. The fix is to switch to Unicode and use \babelprovide[import=he]{hebrew}, then \babelfont[hebrew]{rm}{David CLM} (or another font). – Davislor Apr 07 '19 at 04:41
  • I'll post a working example later. – Davislor Apr 07 '19 at 04:42

1 Answers1

13

Updated Answer

As of July 2020, it is possible to have babel select the correct language and font based on the language of the characters in the source. This allows me to post a complete answer to your question, which genuinely requires no explicit language tagging in the body of the document.

\documentclass{article}
\tracinglostchars=2
\usepackage[paperwidth=10cm]{geometry}
\usepackage{iftex}

\ifluatex \usepackage[bidi=basic]{babel} \usepackage{fontspec} \defaultfontfeatures{ Scale=MatchLowercase, Ligatures=TeX, Renderer=HarfBuzz } \else \usepackage[bidi=default]{babel} \usepackage{fontspec} \defaultfontfeatures{ Scale=MatchLowercase, Ligatures=TeX } \fi

\babelprovide[import, onchar=ids fonts]{hebrew} \babelprovide[import, onchar=ids fonts]{chinese-simplified} \babelprovide[import, onchar=ids fonts]{chinese-traditional}

\babelfont{rm} [Scale=1.0, Ligatures=Common]{Latin Modern Roman} \babelfont{sf} [Ligatures=Common]{Latin Modern Sans} \babelfont[hebrew]{rm} [Language=Default, UprightFont=-Medium, ItalicFont=-MediumItalic, BoldFont=-Bold, BoldItalicFont=-BoldItalic, Extension=.otf ]{DavidCLM} \babelfont[hebrew]{sf} [Language=Default]{Miriam CLM} \babelfont[chinese-simplified]{rm} {Noto Serif CJK SC} \babelfont[chinese-simplified]{sf} {Noto Sans CJK SC} \babelfont[chinese-traditional]{rm} {Noto Serif CJK TC} \babelfont[chinese-traditional]{sf} {Noto Sans CJK TC}

\begin{document}

\tableofcontents

\begin{abstract}
    这是简介及摘要。
\end{abstract}

\section{前言}

\section{关于数学部分}
数学、中英文皆可以混排。 You can intersperse
math, Chinese and English (Latin script) או עברית
without adding extra environments.

這是繁體中文

\end{document}

Babel font sample

This example did not really work correctly. I find when I look that the traditional Chinese font overrode simplified Chinese. Only the traditional Chinese font was used in the output, although I believe the simplified Chinese characters used different Unicode codepoints from their traditional counterparts. Be careful when two languages share the same script!

Original Answer

The [hebrew] package option of babel does not really work any more. It tries to load a set of legacy fonts in the 8-bit LHE encoding. (In this case, it is not even looking for a Type 1 font, but the Metafont source for a bitmap font.) In theory, there’s a package called ivritex floating out there that’s supposed to provide backwards compatibility for this; in practice, the maintainer says that it’s a lot simpler to switch to Unicode. Using the new toolchain also obviates all the hacks needed to make UTF-8 and Unicode fonts mostly work with an inherently 8-bit TeX engine.

The workaround is to load Hebrew with \babelprovide[import=he]{hebrew} instead. You must then pass the [bidi=default] package option to babel for bidirectional text to work. To change the output encoding to Unicode, you need to define a set of fonts with \babelfont.

Here’s an example. I changed the ctexart document class to article with babel, and set all the fonts with babelfont, mainly because ctex has no English documentation. I couldn’t tell you how compatible it is with either babel or polyglossia. If it’s important to write in multiple languages without special markup, you could try ucharclasses, but you might run into problems with right-to-left languages such as Hebrew or Arabic.

\documentclass{article}
\usepackage[paperwidth=10cm]{geometry}
\usepackage[bidi=default]{babel}
\usepackage{fontspec}

\babelprovide[main, import=en, language=Default]{english} \babelprovide[import=he]{hebrew} \babelprovide[import]{chinese-simplified} \babelprovide[import]{chinese-traditional}

\babelfont{rm} [Scale=1.0, Ligatures={Common, TeX}]{Latin Modern Roman} \babelfont{sf} [Scale=MatchLowercase, Ligatures={Common, TeX}]{Latin Modern Sans} \babelfont[hebrew]{rm} [Scale=MatchLowercase, Language=Default]{David CLM} \babelfont[hebrew]{sf} [Scale=MatchLowercase, Language=Default]{Miriam CLM} \babelfont[chinese-simplified]{rm} [Scale=MatchLowercase]{Noto Serif CJK SC} \babelfont[chinese-simplified]{sf} [Scale=MatchLowercase]{Noto Sans CJK SC} \babelfont[chinese-traditional]{rm} [Scale=MatchLowercase]{Noto Serif CJK TC} \babelfont[chinese-traditional]{sf} [Scale=MatchLowercase]{Noto Sans CJK TC}

\begin{document}

\tableofcontents

\begin{abstract}
  \begin{otherlanguage}{chinese-simplified}
    这是简介及摘要。
  \end{otherlanguage}
\end{abstract}

\section{\foreignlanguage{chinese-simplified}{前言}}

\section{\foreignlanguage{chinese-simplified}{关于数学部分}}
\foreignlanguage{chinese-simplified}{数学、中英文皆可以混排。} You can intersperse
math, Chinese and English (Latin script) \foreignlanguage{hebrew}{או עברית}
without adding extra environments.

\foreignlanguage{chinese-traditional}{這是繁體中文。}

\end{document}

Multilingual sample text

“Without adding extra environments” is now a blatant lie. You can make Chinese the main language instead of English, but then you will have to localize certain strings such as the Table of Contents. The call to geometry is solely to make the output fit within the allowed image size here.

If you’re writing documents in Chinese and Hebrew, you presumably have fonts for these languages installed on your system already, and can replace the ones I picked with them. You do not need any special TeX fonts with fontspec; any modern system font will work. If you want to install the fonts I used in this example, you should do so from one of the following sources, in order. A: an operating-system package, such as fonts-noto-cjk and culmus on Ubuntu; B: a CTAN package, such as notoCJKsc; C: from the Culmus project and the Noto CJK homepage.

This compiles with XeLaTeX, requires Babel 3.27 or later, and is written to work around the bug that \babelfont in 3.27 ignores default font features. There’s already a patch to fix this.

Davislor
  • 44,045
  • Thanks for your answer! My problem is, Package babel Error: You haven't specified a language option. ...ry to proceed from here, type x to quit.} Do you happen to know any fix to this? – Zuriel Apr 07 '19 at 11:33
  • @Zuriel Upgrade to babel 3.27. – Davislor Apr 07 '19 at 12:15
  • 1
    @Zuriel If you absolutely can’t, you could try, as a workaround, \usepackage[bidi, english]{babel} and commenting out the line %\babelprovides[main, import=en]{english}. But you want to run an recent TeX engine, or you’ll get a lot of problems we can’t help you with. – Davislor Apr 07 '19 at 12:16
  • @Davislor Hi. This is an off-topic: sorry very much. Please, can you see my recent question for to have a precious your answer? Thank you very much. https://tex.stackexchange.com/questions/512663/a-big-tau-used-in-an-italian-university-book – Sebastiano Oct 26 '19 at 20:05
  • Huh, someone un-upvoted it after I posted a more up-to-date answer, without removing the old one they upvoted? If you’re still around, I’m curious why you thought the edit made it that much worse. – Davislor Jul 11 '20 at 04:39
  • 1
    +1: Important update! – Dr. Manuel Kuehner Jul 11 '20 at 05:50
  • "based on the language of the characters in the source": Are there cases where a character can be part of both languages, like numbers? – Dr. Manuel Kuehner Jul 11 '20 at 05:52
  • 1
    @Dr.ManuelKuehner Yes, definitely. In those cases, it’s not supposed to change the language, but I haven’t stress-tested that. – Davislor Jul 11 '20 at 06:59
  • 2
    @Dr.ManuelKuehner In this case, a bit of testing shows that the Traditional Chinese font overrides the Simplified Chinese font entirely. However, to the best of my limited knowledge, the simplified and traditional characters use different Unicode codepoints anyway. – Davislor Jul 11 '20 at 09:24
  • 1
    Thanks for your research – Dr. Manuel Kuehner Jul 11 '20 at 20:39
  • Is this supposed to work outside LuaTeX? I'm trying with XeTeX and it seems that Babel assumes LuaTeX when onchar is provided... – gaelicWizard Jul 20 '22 at 21:20
  • @gaelicWizard Afraid not. The first MWE only works in LuaLaTeX. It is possible to do the same for certain languages in XeLaTeX, using ucharclasses. PDFTeX language support is more ad hoc. I have a few examples up in all three engines for a handful of languages. – Davislor Jul 20 '22 at 21:54
  • @gaelicWizard The second MWE should work in XeLaTeX. However, it looks like it could use an update, since the workaround for a bug in an old version of babel is no longer needed. And I should probably make the \ifluatex block actually work. – Davislor Jul 20 '22 at 23:08
  • @davislor the code in babel.sty that handles onchar begins immediately with LuaTeX-specific macros, and then the next line is \directlua. I can’t imagine how it would work in XeTeX, but I would be very happy to be proven wrong! – gaelicWizard Jul 26 '22 at 03:08
  • 1
    @gaelicWizard I meant the final MWE, the one without onchar= or any LuaTeX-specific commands. – Davislor Jul 26 '22 at 03:29