CTeX-org / ctex-kit

Macro Packages and Scripts for Chinese TeX users
965 stars 124 forks source link

LuaTeX – Babel – CTeX with multiple CJK languages #628

Open BenjaminGalliot opened 2 years ago

BenjaminGalliot commented 2 years ago

(Sorry to use English)

I would like to know if it was possible to combine in a single LuaTeX document several CJK languages (alongside non CJK languages), while keeping the language environments of Babel (with different fonts depending on family and language) as well as the advanced features that CTeX/Luatexja offers (with hyphenations, punctuation, etc., since I can have long texts that mix different scripts, sometimes nested)...

For example, currently I can have this (but no different fonts depending on family and language):

% !TEX program = lualatex

\documentclass[12pt,a4paper,oneside]{article}
\usepackage{ctex}
\usepackage[main=french]{babel}
\usepackage[svgnames]{xcolor}
\babelprovide{chinese}
\babelprovide{japanese}

\babelfont[french]{rm}{Liberation Serif}
\babelfont[french]{sf}{Liberation Sans}
% No effect:
\babelfont[chinese]{rm}[Color=DarkGreen]{AR PL UKai CN}
\babelfont[chinese]{sf}[Color=NavyBlue]{AR PL UMing CN}
\babelfont[japanese]{rm}[Color=MediumAquamarine]{Noto Serif CJK JP}
\babelfont[japanese]{sf}[Color=MediumBlue]{Noto Sans CJK JP}

% It works (but it affects all CJK languages):
\setCJKfamilyfont{rm}{AR PL UKai CN}
\setCJKfamilyfont{sf}{AR PL UMing CN}
% Same with Luatexja directly, with setmainjfont/setsansjfont/setmonojfont

\begin{document}

\foreignlanguage{chinese}{
     中文 \textsf{中文}
}

\foreignlanguage{japanese}{
     日本語 にほんご \textsf{日本語 にほんご}
}

\end{document}

Or this (but I lose all advanced features):

% !TEX program = lualatex

\documentclass[12pt,a4paper,oneside]{article}
\usepackage[main=french]{babel}
\usepackage[svgnames]{xcolor}
\babelprovide{chinese}
\babelprovide{japanese}

\babelfont[french]{rm}{Liberation Serif}
\babelfont[french]{sf}{Liberation Sans}
% It works:
\babelfont[chinese]{rm}[Color=DarkGreen]{AR PL UKai CN}
\babelfont[chinese]{sf}[Color=NavyBlue]{AR PL UMing CN}
\babelfont[japanese]{rm}[Color=MediumAquamarine]{Noto Serif CJK JP}
\babelfont[japanese]{sf}[Color=MediumBlue]{Noto Sans CJK JP}

\begin{document}

\foreignlanguage{chinese}{
     中文 \textsf{中文}
}

\foreignlanguage{japanese}{
     日本語 にほんご \textsf{日本語 にほんご}
}

\end{document}

Is there already a way to accomplish all of this (which I haven't found), or is this a feature that can be considered in the future?

CastleStar14654 commented 2 years ago

Perhaps you need to add some commands to \addto\extraschinese (or japanese) which asks ctex (or the underlying luatexja) to change fonts whenever a different language is loaded by babel? Similar methods work under XeLaTeX.

BenjaminGalliot commented 2 years ago

Thank you for your answer. I was able to find a solution thanks to your previous answer, which you may have deleted because it was for XeTeX according to you, but it worked well for LuaTex so I put it back here !

You wrote:

Here is a workaround: using \addto\extras<language> to ask xeCJK (which is the underlying package used by ctex using XeLaTeX) to change the default CJK font family when a different language is loaded.

% Need XeLaTeX

\documentclass{minimal}
\usepackage[japanese,chinese,french,provide+=*]{babel}
\usepackage{xeCJK}

\babelfont[french]{rm}{Liberation Serif}
\babelfont[french]{sf}{Liberation Sans}

\newCJKfontfamily[chineserm]\chineserm[Color=DarkGreen]{AR PL UKai CN}
\newCJKfontfamily[chinesesf]\chisesesf[Color=NavyBlue]{AR PL UMing CN}
\newCJKfontfamily[japaneserm]\japaneserm[Color=MediumAquamarine]{Noto Serif CJK JP}
\newCJKfontfamily[japanesesf]\japanesesf[Color=MediumBlue]{Noto Sans CJK JP}

\addto\extraschinese{% here % is necessary
    \renewcommand{\CJKrmdefault}{chineserm}%
    \renewcommand{\CJKsfdefault}{chinesesf}%
}

\addto\extrasjapanese{% here % is necessary
    \renewcommand{\CJKrmdefault}{japaneserm}%
    \renewcommand{\CJKsfdefault}{japanesesf}%
}

\begin{document}

Now it is French, the CJK font will be the default Fandol:

\begin{center}
    RM: 字符测试;
    SF: \textsf{字符测试。}
\end{center}

\foreignlanguage{chinese}{
    Now it is Chinese, the CJK font will be AP PL:
    \begin{center}
        RM: 字符测试;
        SF: \textsf{字符测试。}
    \end{center}
}

\foreignlanguage{japanese}{
    Now it is Japanese, the CJK font will be Noto:
    \begin{center}
        RM: 字符测试;
        SF: \textsf{字符测试。}
    \end{center}
}

\end{document}

Here is what I was able to achieve:

% !TEX program = lualatex

\documentclass[12pt,a4paper,oneside]{article}
\usepackage{ctex}
\usepackage[main=french]{babel}
\usepackage[svgnames]{xcolor}
\usepackage{multicol}
\babelprovide{chinese}
% I'm using "import=zh-Hans" here instead of using "chinese-simplified" because
% I couldn't figure out how to do "\addto{extraschinese-traditional}" with the hyphen
% (but it seems nice if I want to duplicate same language with aliases)...
\babelprovide[import=zh-Hans]{chinesesimplified}
\babelprovide[import=zh-Hant]{chinesetraditional}
\babelprovide[import=zh-Hant]{chinesetraditionalhk}
\babelprovide[import=zh-Hant]{chinesetraditionaltw}
\babelprovide{japanese}

\babelfont[french]{rm}{Liberation Serif}
\babelfont[french]{sf}{Liberation Sans}
% Effect on nested non-CJK glyphes:
\babelfont[chinese]{rm}[Color=DarkGreen]{Liberation Serif}
\babelfont[chinese]{sf}[Color=NavyBlue]{Liberation Serif}
\babelfont[chinesesimplified]{rm}[Color=Brown]{AR PL KaitiM GB}
\babelfont[chinesesimplified]{sf}[Color=Purple]{AR PL SungtiL GB}
\babelfont[chinesetraditional]{rm}[Color=Red]{AR PL KaitiM Big5}
\babelfont[chinesetraditional]{sf}[Color=Orange]{AR PL Mingti2L Big5}
\babelfont[chinesetraditionalhk]{rm}[Color=Red]{AR PL UKai HK}
\babelfont[chinesetraditionalhk]{sf}[Color=Orange]{AR PL UMing HK}
\babelfont[chinesetraditionaltw]{rm}[Color=Red]{AR PL UKai TW}
\babelfont[chinesetraditionaltw]{sf}[Color=Orange]{AR PL UMing TW}
\babelfont[japanese]{rm}[Color=MediumAquamarine]{Noto Serif CJK JP}
\babelfont[japanese]{sf}[Color=MediumBlue]{Noto Sans CJK JP}

% It works (for CJK):
\setCJKfamilyfont{chineserm}[Color=DarkGreen]{AR PL UKai CN}
\setCJKfamilyfont{chinesesf}[Color=NavyBlue]{AR PL UMing CN}
\setCJKfamilyfont{chinesesrm}[Color=Brown]{AR PL KaitiM GB}
\setCJKfamilyfont{chinesessf}[Color=Purple]{AR PL SungtiL GB}
\setCJKfamilyfont{chinesetrm}[Color=Red]{AR PL KaitiM Big5}
\setCJKfamilyfont{chinesetsf}[Color=Orange]{AR PL Mingti2L Big5}
\setCJKfamilyfont{chinesethkrm}[Color=Red]{AR PL UKai HK}
\setCJKfamilyfont{chinesethksf}[Color=Orange]{AR PL UMing HK}
\setCJKfamilyfont{chinesettwrm}[Color=Red]{AR PL UKai TW}
\setCJKfamilyfont{chinesettwsf}[Color=Orange]{AR PL UMing TW}
\setCJKfamilyfont{japaneserm}[Color=MediumAquamarine]{Noto Serif CJK JP}
\setCJKfamilyfont{japanesesf}[Color=MediumBlue]{Noto Sans CJK JP}

\addto\extraschinese{% here % is necessary
    \renewcommand{\CJKrmdefault}{chineserm}%
    \renewcommand{\CJKsfdefault}{chinesesf}%
}

\addto{\extraschinesesimplified}{% here % is necessary
    \renewcommand{\CJKrmdefault}{chinesesrm}%
    \renewcommand{\CJKsfdefault}{chinesessf}%
}

\addto{\extraschinesetraditional}{% here % is necessary
    \renewcommand{\CJKrmdefault}{chinesetrm}%
    \renewcommand{\CJKsfdefault}{chinesetsf}%
}

\addto{\extraschinesetraditionalhk}{% here % is necessary
    \renewcommand{\CJKrmdefault}{chinesethkrm}%
    \renewcommand{\CJKsfdefault}{chinesethksf}%
}

\addto{\extraschinesetraditionaltw}{% here % is necessary
    \renewcommand{\CJKrmdefault}{chinesettwrm}%
    \renewcommand{\CJKsfdefault}{chinesettwsf}%
}

\addto{\extrasjapanese}{% here % is necessary
    \renewcommand{\CJKrmdefault}{japaneserm}%
    \renewcommand{\CJKsfdefault}{japanesesf}%
}

\begin{document}

\foreignlanguage{chinese}{
     中文 zhongwen 中文 虎 兑 兌 骨 – \textsf{中文 zhongwen 中文}
}

\foreignlanguage{chinesesimplified}{
     中文 zhongwen 中文 虎 兑 兌 骨 – \textsf{中文 zhongwen 中文}
}

\foreignlanguage{chinesetraditional}{
     中文 zhongwen 中文 虎 兑 兌 骨 – \textsf{中文 zhongwen 中文}
}

\foreignlanguage{chinesetraditionalhk}{
     中文 zhongwen 中文 虎 兑 兌 骨 – \textsf{中文 zhongwen 中文}
}

\foreignlanguage{chinesetraditionaltw}{
     中文 zhongwen 中文 虎 兑 兌 骨 – \textsf{中文 zhongwen 中文}
}

\foreignlanguage{japanese}{
     日本語 nihongo にほんご \textsf{日本語 nihongo にほんご}
}

% To see easily CJK hyphenation/kerning.
\begin{multicols}{2}

\foreignlanguage{chinese}{
     孔生\textsf{雪笠},聖裔也。為人蘊藉,工詩。有執友令天臺,寄函招之。生往,令適卒,落拓不得歸,寓菩陀寺,傭為寺僧抄錄。寺西百餘步有單先生第,先生故公子,以大訟蕭條,眷口寡,移而鄉居,宅遂曠焉。
}

\foreignlanguage{chinesesimplified}{
     孔生\textsf{雪笠},聖裔也。為人蘊藉,工詩。有執友令天臺,寄函招之。生往,令適卒,落拓不得歸,寓菩陀寺,傭為寺僧抄錄。寺西百餘步有單先生第,先生故公子,以大訟蕭條,眷口寡,移而鄉居,宅遂曠焉。
}

\foreignlanguage{chinesetraditional}{
     孔生\textsf{雪笠},聖裔也。為人蘊藉,工詩。有執友令天臺,寄函招之。生往,令適卒,落拓不得歸,寓菩陀寺,傭為寺僧抄錄。寺西百餘步有單先生第,先生故公子,以大訟蕭條,眷口寡,移而鄉居,宅遂曠焉。
}

\end{multicols}

\end{document}