uline 换行失败并使用中文字符产生太多 }'s

uline 换行失败并使用中文字符产生太多 }'s

遇到一个很奇怪的问题,使用中文字符会产生Too many} 的错误。

\documentclass{article}
\usepackage{ctex}
\usepackage{ulem}

\newif\ifanswer
\newlength{\keylength}
\newcommand{\answer}[1]{%
    \ifanswer
    \uline{#1}%
    \else
    \settowidth{\keylength}{#1}
    \uline{\hspace{\keylength}}
    \fi}


\begin{document}

What do you think ? I think \uline{You are Good,You are Good,You are Good,You are Good,You are Good,You are Good}.  We are Good.
\\

\answertrue
What do you think ? I think \answer{You are Good,You are Good,You are Good,You are Good,You are Good,You are Good}. We are Good.
\\

\answerfalse
What do you think ? I think \answer{You are Good,You are Good,You are Good,You are Good,You are Good,You are Good}. We are Good.
\\

\answertrue
What do you think ? I think \answer{天地人}. We are Good.
\\

\answerfalse
What do you think ? I think \answer{天地人}. We are Good.
\\

\answertrue
What do you think ? I think \answer{$ 2+2=2^{2} $}. We are Good.
\\

\answerfalse
What do you think ? I think \answer{$ 2+2=2^{2} $}. We are Good.
\\



What do you think ? I think \uline{天地人 $ 2+2=2^{2} $}. We are Good.
\\

\answerfalse
What do you think ? I think \answer{天地人$ 2+2=2^{2} $}. We are Good.
\\

\answertrue
What do you think ? I think \answer{天地人$ 2+2=2^{2} $}. We are Good.


\end{document}


并且,uline包装失败。

为什么不使用 \if ... \else 环境?

谢谢!

答案1

我无法在 TeX Live 2020 上重现您的错误,但这里有一个新版本,\answer当您选择时,它可以正确换行\answerfalse。因为它使用linegoal,所以它需要 XeLaTeX,可能需要编译两次才能解析所有引用。请参阅这个问题有几种替代方案。或者@egreg 的答案这里(当我写这篇文章时我还没有看到它)。

%% This MWE requires XeLaTeX.

\documentclass{article}
\usepackage{ctex}
\usepackage{ulem}
\usepackage{parskip} % You seemed to be trying to get this style with \\.
\usepackage{ifthen, linegoal}

\newif\ifanswer
\newlength{\keylength}
\newlength{\chunklength}
\newcommand{\answer}[1]{%
    \ifanswer%
      \uline{#1}%
    \else%
      \settowidth{\keylength}{#1}%
      \setlength{\chunklength}{\linegoal}%
      \ifthenelse{\lengthtest{\chunklength<\keylength}}
        {\underline{\hspace{\chunklength}}\newline%
         \addtolength{\keylength}{-\chunklength}%
         \whiledo{\lengthtest{\keylength>\linewidth}}%
                 {\underline{\hspace{\linewidth}}\newline%
                  \addtolength{\keylength}{-\linewidth}}%
         \ifdim\keylength>0pt%
           \underline{\hspace{\keylength}}%
         \fi%
        }
        {\underline{\hspace{\keylength}}}%
    \fi}


\begin{document}

What do you think ? I think \uline{You are Good,You are Good,You are Good,You are Good,You are Good,You are Good}.  We are Good.

\answertrue
What do you think ? I think \answer{You are Good,You are Good,You are Good,You are Good,You are Good,You are Good}. We are Good.

\answerfalse
What do you think ? I think \answer{You are Good,You are Good,You are Good,You are Good,You are Good,You are Good}. We are Good.

\answertrue
What do you think ? I think \answer{天地人}. We are Good.

\answerfalse
What do you think ? I think \answer{天地人}. We are Good.

\answertrue
What do you think ? I think \answer{$ 2+2=2^{2} $}. We are Good.

\answerfalse
What do you think ? I think \answer{$ 2+2=2^{2} $}. We are Good.

What do you think ? I think \uline{天地人 $ 2+2=2^{2} $}. We are Good.

\answerfalse
What do you think ? I think \answer{天地人$ 2+2=2^{2} $}. We are Good.

\answertrue
What do you think ? I think \answer{天地人$ 2+2=2^{2} $}. We are Good.

\clearpage
\answerfalse
Lorem ipsum dolor sit amet, \answer{consectetur adipiscing elit. Nulla
tincidunt nec velit non convallis. Aliquam lectus erat, consectetur vel quam
eget, aliquam vehicula mauris. Nam aliquet, erat nec placerat pulvinar, tellus
est accumsan augue, eget aliquam ligula ligula id nunc. Integer in imperdiet
arcu. Pellentesque venenatis commodo blandit. Aenean scelerisque, erat quis
auctor dapibus, elit sapien tincidunt purus, id varius felis ipsum sed nulla.
Nullam semper est ut fringilla blandit. Aliquam viverra, libero sodales porta
dictum, felis urna egestas eros, ac posuere dui tellus et arcu. Nullam
vulputate, ligula non placerat mattis, erat tellus tempus ligula, eu commodo
risus lectus ut enim. Donec vitae ante sit amet purus rhoncus vestibulum non at
turpis. Suspendisse aliquam tellus vitae erat condimentum, sit amet maximus
tellus ullamcorper. Cras tincidunt, eros vel pharetra eleifend, neque orci
pharetra dui, sit amet dictum risus leo eget neque. Duis varius ligula in dui
posuere, in sollicitudin metus pellentesque. Nulla tellus sapien, lacinia nec
nisl sed, luctus volutpat felis}. Class aptent taciti sociosqu ad litora
torquent per conubia nostra, per inceptos himenaeos. 

\answertrue
Lorem ipsum dolor sit amet, \answer{consectetur adipiscing elit. Nulla
tincidunt nec velit non convallis. Aliquam lectus erat, consectetur vel quam
eget, aliquam vehicula mauris. Nam aliquet, erat nec placerat pulvinar, tellus
est accumsan augue, eget aliquam ligula ligula id nunc. Integer in imperdiet
arcu. Pellentesque venenatis commodo blandit. Aenean scelerisque, erat quis
auctor dapibus, elit sapien tincidunt purus, id varius felis ipsum sed nulla.
Nullam semper est ut fringilla blandit. Aliquam viverra, libero sodales porta
dictum, felis urna egestas eros, ac posuere dui tellus et arcu. Nullam
vulputate, ligula non placerat mattis, erat tellus tempus ligula, eu commodo
risus lectus ut enim. Donec vitae ante sit amet purus rhoncus vestibulum non at
turpis. Suspendisse aliquam tellus vitae erat condimentum, sit amet maximus
tellus ullamcorper. Cras tincidunt, eros vel pharetra eleifend, neque orci
pharetra dui, sit amet dictum risus leo eget neque. Duis varius ligula in dui
posuere, in sollicitudin metus pellentesque. Nulla tellus sapien, lacinia nec
nisl sed, luctus volutpat felis}. Class aptent taciti sociosqu ad litora
torquent per conubia nostra, per inceptos himenaeos. 

\end{document}

第 1 页

第2页

相关内容