3

I'm dissatisfied with the results of how babel renders CJK scripts (it's hard to get protrusion and punctuation kerning looking nice and lines can break before punctuation—some of these issues can be worked around easily enough, but not all).

I'm experimenting with luatexja which gives nicer rendering results, at least for Japanese.

My end goal is to have nice Chinese (simplified and traditional) and Japanese (and Korean I guess) as well as other languages that babel supports in the same document. And be able to switch between languages easily, ideally hooking into babel's \selectlanguage to make required CJK changes. I'm beginning to think this is too hard.

(Note: I'm not trying to replace babel I just want the better output that luatexja gives.)

But for Chinese scripts there are a few oddities I don't understand.

In the MWE example below, the kerning between the ?and ” seems too large in comparison with 。and ”.

Is it meant to be like this? (I'm not a Chinese reader/speaker).

If not, can I tweak luatexja in some way?

I notice that similar problems occur if I try and load Noto Serif CJK TC. The kerning between 。and ” is completely wrong in this case.

(I would like to keep using LuaLaTeX rather than XeLaTeX.)

MWE

%! TeX Program = lualatex

\documentclass{article}
\pagestyle{empty}
\usepackage{luatexja-fontspec}
\setmainjfont{Noto Serif CJK SC}
\begin{document}
“你们一路上争论些什么?”

“谁想为首,就该在众人中做最小的,做众人的仆人。”
\end{document}

Output

output

12
  • Sorry - this is probably my ignorance - but why do you think luatexja-fontspec is an alternative to babel? As far as I can tell, it isn't. For Japanese, I'd assume you'd want either babel or polyglossia. Ditto for Chinese, but the support looks to be more rudimentary (and possibly only available in babel). The space you show is nothing to do with the package. It's determined by the font. The package apparently modifies something for the second line, but not the first. If you load the same font with plain fontspec, the second line shows a similar space at the end.
    – cfr
    Commented May 10 at 2:17
  • @cfr By default the babel CJK setup does not change kerning between characters (they are all full width). And lack of protrusion makes things look ugly. So if you have a comma or period at the end of a line you get a nasty gap. Also you can get line breaks before punctuation which is undesirable. Some of these things can be worked around by manually adjusting things with microtype and loading custom kerning options, but kerning between latin characters and things like opening quote marks in Chinese requires lower level changes. luatexja deals with these problems. Commented May 10 at 2:47
  • I'm not suggesting luatexja doesn't serve a purpose. But why do you want to use it rather than babel or polyglossia? Don't you want both? (Not both babel and polyglossia, of course.)
    – cfr
    Commented May 10 at 2:49
  • @cfr because luatexja output is better than babel CJK. I still want to use babel for other languages. (My use case is producing multilingual parallel texts - e.g., with English, Thai, Persian, Japanese, Simplified Chinese.) I think it would be a nice feature for babel to support something like luatexja as an optional backend for CJK scripts. That way for simple texts you could do what babel does now. But for better output, you could ask for luatexja but still use the babel front end for language switching. Commented May 10 at 2:55

3 Answers 3

2

The question mark is the fullwidth form, so what babel and luatexja output is what I would expect [Edit. At least in some punctuation styles]. You can apply a transform to replace it by the halfwidth form:

\documentclass{article}
\pagestyle{empty}
\usepackage[chinese, provide=*]{babel}
\babelfont{rm}{Noto Serif CJK SC}
\babelprehyphenation{chinese}{ ? }{ string=? }
\begin{document}
“你们一路上争论些什么?”
\end{document}

enter image description here

A more traditional approach is making the fullwidth character active and defining it as the halfwidth form.

Edited 2024-05-19

As to the period + quotes, it’s another task for a transform:

\documentclass{article}

\pagestyle{empty}
\usepackage[chinese, provide=*]{babel}
\babelfont{rm}{Noto Serif CJK SC}

\babelprehyphenation{chinese}{ [?。]” }{
  {},
  { insert, penalty = 10000 },
  { insert, space = -.5 0 0},
  {}
}

\begin{document}
“你们一路上争论些什么?”

“谁想为首,就该在众人中做最小的,做众人的仆人。”

\end{document}

enter image description here

In previous versions, because of a bug, a workaround was necessary:

\usepackage{etoolbox}
\makeatletter
\catcode`\%=12
\patchcmd{\bbl@settransform}
  {(space)%s*=%s*([%d%.]+)%s+([%d%.]+)%s+([%d%.]+)}
  {(space)%s*=%s*([%-%d%.]+)%s+([%-%d%.]+)%s+([%-%d%.]+)}
  {}{}
\catcode`\%=14
\makeatother

Addendum. There are new tools on the way to deal with compression and punctuation at end of lines, including predefined transforms, but with relation to end of lines, microtype can help with something like:

\usepackage[protrusion=true, expansion=false]{microtype}

\SetProtrusion{ encoding = TU }{、={,500}, ,={,500}, 。={,500}}
3
  • Thanks! I would actually prefer to stick with just babel. There's a big speed hit with luatexja and it does more than what I really want. I've been using fonts.handlers.otf.addfeature to apply custom kerning to the fonts. This worked for some pairs, but not the period or question mark for some reason. I also used microtype to deal with protrusion which is mostly successful. And \babelcharproperty to stop line breaks before quote marks. Commented May 11 at 11:23
  • I didn't really consider it a bug as I wasn't sure what plans you might have for CJK scripts. There are a few more issues I don't know how to work around. I'll ask some new questions. Commented May 11 at 11:28
  • BTW, I'm not sure about the logic that the question mark is full width so output is as expected. It's still full width in the final PDF and the period is also full width everywhere. You get especially ugly output with this combination: 。‘. With simplified script the 。is pushed to the left (unlike it being centred in traditional) and the ‘ is pushed to the right of a full width cell. Commented May 11 at 11:47
3

Use ctex bundle:

%! TeX Program = lualatex

\documentclass{article}
\usepackage{ctex}
\setCJKmainfont{SourceHanSerifSC}
\begin{document}
“你们一路上争论些什么?”

“谁想为首,就该在众人中做最小的,做众人的仆人。”
\end{document}

enter image description here

9
  • Does this work properly in multilingual documents, though? Say somebody wants Arabic, Chinese and English? That's what I couldn't figure out ....
    – cfr
    Commented May 10 at 4:04
  • The ctex bundle is used to typeset Chinese, Japanese and Korean characters as well as English. As for Arabic, I'm not sure.
    – Stephen
    Commented May 10 at 4:07
  • The OP wants RTL support, which is why I asked about Arabic. Otherwise, ctex is the obvious move here. I didn't know you could just use it as a package, though.
    – cfr
    Commented May 10 at 4:14
  • Is there ctex documentation in English? Commented May 10 at 4:16
  • ctex might work. As long as you can use babel for other languages and turn off the special handling of CJK scripts when you're doing things in other languages. But I can't read its documentation :( Commented May 10 at 4:17
2

The following seems to work in minimal testing. However, bear in mind that I know nothing about Chinese or Hebrew and almost nothing about Greek. So 'seems to work' is ... ych a fi!

This attempts to use babel for Chinese, British English, Greek, Hebrew and Welsh. The missing characters in the Hebrew are due to missing characters in the font. It works with a font which provides the characters.

The fonts are configured using babel's simple mechanism, except for Chinese, which uses the Noto configuration provided by ctex. This is not exactly the same font as you have, so you might want to use a custom configuration instead (for Simplified Chinese), but it is probably sufficient for demonstration purposes.

I hope the translations of chapter, months etc. are correct, but I can only confirm this, personally, for English and Welsh.

\selectlanguage{<language>} seems to work for all languages.

\documentclass{book}
\pagestyle{empty}
% Javier Bezos: https://tex.stackexchange.com/a/717269/
\usepackage[welsh,greek,british,bidi=basic]{babel}
\babelprovide[import=zh]{chinese}
\babelprovide[import=he]{hebrew}
\usepackage{fontspec}
\usepackage[fontset=ubuntu]{ctex}
\babelfont[hebrew]{rm}[Script=Hebrew,Renderer=Harfbuzz]{Noto Sans Hebrew}
\babelfont[greek]{rm}{Noto Serif}
\begin{document}


\chapter{English}

English

\today 

\contentsname

\figurename

\selectlanguage{chinese}



\chapter{天山山脉}% https://tex.stackexchange.com/a/717269/

\today

\contentsname

\figurename

“你们一路上争论些什么?”

“谁想为首,就该在众人中做最小的,做众人的仆人。”


\selectlanguage{welsh}


\chapter{Cymraeg}

Cymraeg

\today

\contentsname

\figurename

\selectlanguage{hebrew}

\chapter{}
  
\today

\contentsname

\figurename

  עברית

\selectlanguage{greek}

\chapter{}
  
\today

\contentsname

\figurename

εὐδαιμονία

\end{document}

I'm not even attempting an image for this one. Normally, I would, but an Okular bug makes it tricky and multi-page documents on standard sized pages are currently a bit challenging.

Note that the output does get RTL directions correct, even though SE appears unable to do so in code listings.

6
  • Ah - the preset is useful. Thanks. I don't want to use polyglossia because for RTL languages it's nowhere near as good as babel and lualatex. The problem is (as is often the case) that different toolchains give optimum results for different languages. babel is now the best option if you want to produce documents with many languages. But it's CJK support is not as good as it might be. Commented May 10 at 2:50
  • Re your edit: I can and am doing something like that. The question was purely about the rendering between ?”. Which I think you've answered (I'll do some checking and want to see what the preset does apart from just changing font) Commented May 10 at 2:59
  • I'll clarify my question. It's about how babel renders CJK scripts which e even the package maintainer admits is only basic. Commented May 10 at 3:02
  • Hmmm. Actually the preset does not fix the Issue. It loads the JP variant of the Noto font. Which places the ?in the centre of the character box rather than that to the left as the SC variant does. This makes it look like the kerning is better, but it's just the same. And the wrong font language setting is in use. I suspect this is because luatexja is set up for Japanese not Chinese. Things are even worse if you try and load the TC variant of the font. Commented May 10 at 3:44
  • 1
    @DavidPurton I rewrote this answer. Don't know if it is any more use than the last one, but I can delete it again if not. I'm deleting my share of the followup comments because, whatever the defects of this answer, use of Japanese presets hopefully isn't among them.
    – cfr
    Commented May 10 at 5:07

You must log in to answer this question.

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