7

When entering Chinese in a table, despite trying to limit the column size with p{5cm}, the phrase isn't going to the line below.

\documentclass{article}
\usepackage{fontspec}
\usepackage{array}

\setmainfont{NSimSun}

\begin{document} \begin{tabular}{|l|l|p{5cm}|l} 驾轻就熟 & jià qīng jiù shú & 驾轻车,走熟路。比喻对某事有经验,很熟悉,做起来容易。 & 驾轻车,走熟路。比喻对某事有经验,很熟悉,做起来容易。 \ \end{tabular} \end{document}

Enter image description here

Camille
  • 81

3 Answers3

9

You want to set a couple of line break parameters, if you use XeLaTeX.

\documentclass{article}
\usepackage{fontspec}
\usepackage{array}

%\setmainfont{NSimSun} \setmainfont{SimSong}

\XeTeXlinebreaklocale=ZH \XeTeXlinebreakskip=0pt plus 1pt

\begin{document} \begin{tabular}{|l|l|p{5cm}|} 驾轻就熟 & jià qīng jiù shú & \raggedright\arraybackslash 驾轻车,走熟路。比喻对某事有经验,很熟悉,做起来容易。\ \hline 驾轻就熟 & jià qīng jiù shú & 驾轻车,走熟路。比喻对某事有经验,很熟悉,做起来容易。 \end{tabular}

\end{document}

I changed the font to one I have on my machine, but it's not involved in the business.

enter image description here

For LuaLaTeX, use luatexja as recommended in https://tex.stackexchange.com/a/224734/4427

\documentclass{article}
\usepackage{luatexja-fontspec}
\usepackage{array}

%\setmainfont{NSimSun} \setmainjfont{STSong}

\begin{document} \begin{tabular}{|l|l|p{5cm}|} 驾轻就熟 & jià qīng jiù shú & \raggedright\arraybackslash 驾轻车,走熟路。比喻对某事有经验,很熟悉,做起来容易。\ \hline 驾轻就熟 & jià qīng jiù shú & 驾轻车,走熟路。比喻对某事有经验,很熟悉,做起来容易。 \end{tabular}

\end{document}

Note \setmainjfont. Use a font you have.

enter image description here

egreg
  • 1,121,712
7

babel package can be used for both LuaLaTeX and XeLaTeX. If you wanna a multilingual document. Babel is good chocie. If only for XeLaTeX, xeCJK is a good package take care of everything for you. Here is a example of using babel:

\documentclass{article}
\usepackage{array}
\usepackage{lipsum}
\usepackage{zhlipsum}
\usepackage[chinese,english,provide+=*]{babel}
% declare two languages, babel treat second one as the main language. 
% you can switch to Chinese using {\selectlanguage{chinese} ...some chinese} in the document.
% make sure your put curly brackets in between otherwise it will applied for 
% rest of the documents

\babelfont{rm}{Noto Serif CJK SC}% Select font for both english and chinese %\babelfont[chinese]{rm}{Noto Serif CJK SC} %\babelfont[english]{rm}{Libertinus Serif} % Or you can select different font for different language individually

\begin{document} {\selectlanguage{chinese} \begin{table}[ht] \centering \begin{tabular}{|l|l|p{3.5cm}|p{3cm}|} 驾轻就熟 & jià qīng jiù shú & 驾轻车,走熟路。比喻对某事有经验,很熟悉,做起来容易。 & 驾轻车,走熟路。比喻对某事有经验,很熟悉,做起来容易。 \ \end{tabular} \end{table}

\zhlipsum[1][name=zhufu] }

\lipsum[1] \end{document}

enter image description here

Tom
  • 7,318
  • 4
  • 21
  • I also tried using " \begin{tabular}{|l|l|p{3.5cm}|p{3cm}|} " but it wasn't working either. – Camille May 05 '22 at 15:52
  • @Camille It works for me. Maybe your version is old. – Javier Bezos May 05 '22 at 17:20
  • @Camille It was not just set \begin{tabular}{|l|l|p{3.5cm}|p{3cm}|}. You need use package \usepackage[chinese,english,provide+=*]{babel} and select the language {\selectlanguage{chinese}......} when you wanna type Chinese. – Tom May 06 '22 at 16:27
6

If you use LuaLaTeX to compile your document, you could set up a utility macro that enables line breaking at arbitrary points in a string (except, of course, right before a punctuation mark). In the code below, the LaTeX utility macro is called \addZWSP -- short for "add zero-width space", I suppose. This macro calls a Lua function named add_ZWSP that does all of the actual work.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article} % or some other suitable document class
\usepackage{tabularx,booktabs,ragged2e}
\newcolumntype{L}{>{\RaggedRight}X}

\usepackage{fontspec} \setmainfont{Noto Serif SC} % I don't have the NSimSun font

\usepackage{luacode} % for "\luastringN" macro and "luacode" env. \begin{luacode}

  -- The Lua function 'add_ZWSP' does most of the work:
  function add_ZWSP ( s )
     t = ""
     for i = 1 , unicode.utf8.len(s) do
        t = t .. unicode.utf8.sub ( s , i , i )
        u = unicode.utf8.sub ( s , i+1 , i+1 )
        if not unicode.utf8.match ( u , "[,。]" ) then
           t = t .. "\\hspace{0pt}"
        end
     end
     return t
  end
\end{luacode}

%% Set up a utility LaTeX macro to access the Lua function:
\newcommand\addZWSP[1]{\directlua{tex.sprint(add_ZWSP(\luastringN{#1}))}}

\begin{document}
\noindent
%% Use "L" col. type in columns 3 and 4 to allow automatic line breaking
\begin{tabularx}{\textwidth}{@{} ll LL @{}}
\toprule
驾轻就熟 & 
jià qīng jiù shú & 
\addZWSP{驾轻车,走熟路。比喻对某事有经验,很熟悉,做起来容易。} & 
\addZWSP{驾轻车,走熟路。比喻对某事有经验,很熟悉,做起来容易。} \\
\bottomrule
\end{tabularx}
\end{document}
Mico
  • 506,678