The answer to the question as posted is a bit fuzzy
Spaces tend to have a glue component that makes predicting their actual width rather hard unless you remove the glue by using \spaceskip
& \xspaceskip
Do you really mean 'restrict' or do you mean 'expand'? If there is too much text for 3cm, there is no way to meet the requirements.
The three words, when on one line, will have two intermediate spaces. When on two lines, there will be only one space. It will be quite difficult to make sure the total used space
in both cases is identical (in fact, I no idea how to do that in TeX82).
Do you really want the extra room to be filled with \hfill
?
Anyway, you could try something like this:
\newdimen\mydimen
\mydimen=3cm
\setbox0=\hbox {Lorem Ipsum Dolor}\advance\mydimen-\wd0 \unhbox0\hskip\mydimen
which may or may not give you something that is exactly 3cm, depending on the rest of the paragraph.
A much more precise solution would be possible using node processing in LuaTeX, but there are not that many hooks in TeX82's line breaking routine that can really alter its behaviour.
If you use more than one paragraph Chinese, use our xeCJK
package.
For totally Chinese document, use our documents or packages in ctex
bundle. It is based on xeCJK
.
I have explained this topic in a previous question:
How does one type Chinese in LaTeX?
Of course xeCJK
solve the problem about line breaking. You don't need \XeTeXlinebreaklocale
with xeCJK
; and xeCJK
does not change the \XeTeXlinebreaklocale
either.
You don't need to care about the line break locale if you use
\XeTeXlinebreaklocale "zh"
it does nothing to do with western languages as far as I know. Similarly, using xeCJK
you also don't need to turn off it. Non-CJK text would be untouched. With xeCJK
, it is much easier to set the fonts for Chinese without changing western texts, and you'll get better result for puncuations.
For completeness only, I'll say that you can use \makexeCJKactive
and \makexeCJKinactive
to turn on and off the functions of xeCJK
.
Note: xeCJK
is not completely compatible with polyglossia
package for some of the languages. If you meet more problems, ask more questions or contact me. (I am currently the main maintainer of xeCJK
.)
For updated question, see this previous question:
How to write spaces between Korean words with XeCJK?
You can use space
and nospace
options in xeCJK
, and \CJKspace
and \CJKnospace
option, just like CJK
package do.
I think it is not suggested to suppress line breaks between Chinese characters even if you use \CJKspace
to allow spaces between Chinese characters. If you insist, you can change the definition of \CJKglue
to empty. You can read the document of xeCJK
if you are not sure about the options and commands.
After all, an example:
\documentclass{article}
\usepackage{xeCJK}[2011/05/20]
\setCJKmainfont{SimSun}
\begin{document}
在普通文字之间没有空格
可以避免源代码折行造成
的问题。
\CJKspace
如果 需要 也 可以 保留 空格。
这 对于 汉语 分词 研究 或许 是 有益的。
\end{document}
Best Answer
EDIT: As pointed by egreg, this does not take care of the optional argument of
\\
. Here is an alternative implementation that handles\\*
and\\[10pt]
:Note that one cannot directly use
\@ifstar
as it will gobble the space at\\
.