# Displaying pinyin over Chinese characters, without impacting word spacing

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}
\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)?

-
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 '11 at 7:53
When using @{}l@{}, the line using tabular appears with the same spacing as with stackrel. –  Village Dec 16 '11 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 '11 at 15:49

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}


-

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}


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

\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}


-

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}

\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:

-
This solution is also giving different spacing from text without the ruby macros. –  Village Dec 20 '11 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. –  LianTze Lim Dec 20 '11 at 7:05
It's still a very good answer. –  Andrey Vihrov Dec 20 '11 at 15:45

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}%
}%
}


-
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 '11 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 '11 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 '11 at 0:40