14

I want to display pinyin (romanized form) over Chinese characters. However, the two methods I know (using stackrel or tabular both change the spacing between characters. This demonstrates the problem:

\documentclass[a5paper]{article}
\usepackage{ctexcap}
\usepackage{stackrel}
\setCJKmainfont{Adobe Heiti Std}
\newcommand{\mymacroone}[1]{$\stackrel{a}{\textrm{#1}}$}
\newcommand{\mymacrotwo}[1]{\begin{tabular}{l}a\\#1\end{tabular}}
\begin{document}
    \Huge \noindent 床前明月光,疑是地上霜。举头望明月,低头思故乡。 \\
    \mymacroone{床}\mymacroone{前}\mymacroone{明}\mymacroone{月}\mymacroone{光},\mymacroone{疑}\mymacroone{是}\mymacroone{地}\mymacroone{上}\mymacroone{霜}。\mymacroone{举}\mymacroone{头}\mymacroone{望}\mymacroone{明}\mymacroone{月},\mymacroone{低}\mymacroone{头}\mymacroone{思}\mymacroone{故}\mymacroone{乡}。 \\
    \mymacrotwo{床}\mymacrotwo{前}\mymacrotwo{明}\mymacrotwo{月}\mymacrotwo{光},\mymacrotwo{疑}\mymacrotwo{是}\mymacrotwo{地}\mymacrotwo{上}\mymacrotwo{霜}。\mymacrotwo{举}\mymacrotwo{头}\mymacrotwo{望}\mymacrotwo{明}\mymacrotwo{月},\mymacrotwo{低}\mymacrotwo{头}\mymacrotwo{思}\mymacrotwo{故}\mymacrotwo{乡}。 \\
\end{document}

Each of the three lines should be displaying the same.

  • With stackrel, the words are spaced too closely together. This problem is not created by stackrel. It is from having math mode with textrm{} inside.
  • With tabular, the words are spaced too far apart, they do not properly break at the end of the line, and it has a strange vertical and horizontal alignment problems. Using stackrel would be ideal.

How can I place pinyin above Chinese characters without changing the spacing between the characters (at least in situations where the pinyin is shorter than the characters)?

3
  • Try your tabular with column specification @{}l@{} instead of just l. This removes the column separation (or spacing). I can't test it myself, since I don't have the appropriate editor installed.
    – Werner
    Dec 16, 2011 at 7:53
  • When using @{}l@{}, the line using tabular appears with the same spacing as with stackrel.
    – Village
    Dec 16, 2011 at 9:26
  • @Village: Please add cjk tag if you use xecjk tag. Then the question would be easier to be find for others.
    – Leo Liu
    Dec 20, 2011 at 15:49

4 Answers 4

11

Now I add another answer using LI Qing's xpinyin package.

It's rather easy to use:

\documentclass{ctexart}

\usepackage{xpinyin}

\begin{document}
\Huge\sffamily

\begin{pinyinscope}
床前明月光,疑是地上霜。举头望明月,低头思故乡。
\end{pinyinscope}

\xpinyin*{床前明月光,疑是地上霜。举头望明月,低头思故乡。}

\end{document}

enter image description here

9

I think the ruby package (from the CJK package bundle) would be useful here:

\documentclass[a5paper]{article}
\usepackage{ctexcap}

\usepackage{ruby}
% Sets size of the `ruby' i.e. pinyin annotation; default is 0.4
\renewcommand\rubysize{0.3}

\setCJKmainfont{Adobe Heiti Std}
\setmainfont{Arial}
\begin{document}
    \Huge \noindent 床前明月光,疑是地上霜。举头望明月,低头思故乡。 \\    
\noindent\ruby{床}{chuáng}\ruby{前}{qián}\ruby{明}{míng}\ruby{月}{yuè}\ruby{光}{guāng},\ruby{疑}{yí}\ruby{是}{shì}\ruby{地}{dì}\ruby{上}{shàng}\ruby{霜}{shuāng}。
\ruby{举}{jǔ}\ruby{头}{tóu}\ruby{望}{wàng}\ruby{明}{míng}\ruby{月}{yuè},\ruby{低}{dī}\ruby{头}{tóu}\ruby{思}{sī}\ruby{故}{gù}\ruby{乡}{xiāng}。
\end{document}

Output:

Pinyin annotation as rubies

6
  • This solution is also giving different spacing from text without the ruby macros.
    – Village
    Dec 20, 2011 at 4:48
  • Oh, now I see what you mean... I think the \ruby macros produce boxes, thus ctex no longer see them as CJK characters and thus can't do the flexible word spacing thing. Sorry.
    – imnothere
    Dec 20, 2011 at 7:05
  • It's still a very good answer. Dec 20, 2011 at 15:45
  • @LianTzeLim Which package do I need to install for Miktex to be able to use \usepackage{ctexcap}? So far I am getting: ! LaTeX Error: File ctexcap.sty' not found.` Feb 20, 2016 at 3:12
  • @Zelphir You want the "ctex" bundle -- ctan.org/pkg/ctex
    – imnothere
    Feb 20, 2016 at 14:40
8
+50

As LianTze Lim pointed out, ruby package in CJK bundle is specially designed for Japanese ruby characters and Chinese pinyin (拼音) or zhuyin (注音). However, it has some restrictions.

You can add \CJKglue at the end of the definition of \ruby, not only for proper spacing between Chinese characters, but also for correct line breaking. For example,

\documentclass{ctexart}

\usepackage{ruby}
\let\oldruby\ruby
\def\ruby#1#2{\oldruby{#1}{#2}\CJKglue}
\renewcommand\rubysize{0.3}

\begin{document}
\parindent=0pt
\Huge\sffamily

床前明月光,疑是地上霜。举头望明月,低头思故乡。 

\ruby{床}{chuáng}\ruby{前}{qián}\ruby{明}{míng}\ruby{月}{yuè}\ruby{光}{guāng},%
\ruby{疑}{yí}\ruby{是}{shì}\ruby{地}{dì}\ruby{上}{shàng}\ruby{霜}{shuāng}。%
\ruby{举}{jǔ}\ruby{头}{tóu}\ruby{望}{wàng}\ruby{明}{míng}\ruby{月}{yuè},%
\ruby{低}{dī}\ruby{头}{tóu}\ruby{思}{sī}\ruby{故}{gù}\ruby{乡}{xiāng}。
\end{document}

enter image description here

However, the result is still wrong sometimes: line break is allowed before punctuations now. And of course we have no puncutation spacing turning features then.

Although it is rather difficult to deal with all possible situations right now, we can do more if we use some TeX tricks (like \futurelet) and maybe hack into xeCJK. At least we can prevent linebreaking before punctuations:

\documentclass{ctexart}

\usepackage{ruby}
\renewcommand\rubysize{0.3}
\let\oldruby\ruby
\def\ruby#1#2{\oldruby{#1}{#2}\futurelet\next\addCJKglue}
\def\addCJKglue{\ifx\next\ruby \CJKglue \fi}

\begin{document}

\Huge\sffamily

床前明月光,疑是地上霜。

\ruby{床}{chuáng}\ruby{前}{qián}\ruby{明}{míng}\ruby{月}{yuè}\ruby{光}{guāng},%
\ruby{疑}{yí}\ruby{是}{shì}\ruby{地}{dì}\ruby{上}{shàng}\ruby{霜}{shuāng}。%

% Compare with old solution:
\def\ruby#1#2{\oldruby{#1}{#2}\CJKglue}
\ruby{床}{chuáng}\ruby{前}{qián}\ruby{明}{míng}\ruby{月}{yuè}\ruby{光}{guāng},%
\ruby{疑}{yí}\ruby{是}{shì}\ruby{地}{dì}\ruby{上}{shàng}\ruby{霜}{shuāng}。%

\end{document}

enter image description here

2

I would go with a low level construction:

\newcommand{\mymacroone}[1]{%
  \leavevmode
  \vbox{\offinterlineskip
        \halign{\hfil##\hfil\cr
        a\cr
        \noalign{\vskip-0.3ex}%
        \strut#1\cr}%
       }%
}

enter image description here

3
  • When I replaced mymacroone with your code, the spacing still has the same problem stackrel has. Yet, your illustration shows no problem.
    – Village
    Dec 20, 2011 at 0:21
  • @Village I said \raggedright; by inserting the characters in boxes you lose the peculiar spacing of Chinese. I can't see many other ways to proceed, though.
    – egreg
    Dec 20, 2011 at 0:24
  • Adding raggedright to a line before the text places a line break after punctuation for all of the macros. Is there anything I can place outside of the macros that means, "Handle all of what inside this as Chinese text"?
    – Village
    Dec 20, 2011 at 0:40

You must log in to answer this question.

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