0

encountered a very strange problem, using Chinese characters would produce Too many} 's errors.

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


and,uline wraps failure.

Why not use \if ... \else environment ?

thanks!

  • You have to use answer inside math mode. Otherwise, uline stops working –  Jan 12 '20 at 09:18
  • I find problem, The error is due to the lack of space between the Chinese character and the $ character, but why does the lack of a space produce an error? – user140272 Jan 14 '20 at 12:53
  • \uline{\hspace{\keylength}} wraps the same way that \hspace{\keylength} does. – Donald Arseneau Jun 13 '20 at 00:05
  • See the question: https://tex.stackexchange.com/questions/509209/uline-from-ulem-package-wont-break-lines-automatically-when-containing-boo – Donald Arseneau Jun 13 '20 at 00:06

1 Answers1

1

I cannot reproduce your bug on TeX Live 2020, but here is a new version of \answer that correctly wraps lines when you select \answerfalse. Because it uses linegoal, it requires XeLaTeX and might need to be compiled twice to resolve all references. See this question for several alternatives. Or @egreg’s answer here (which I hadn’t seen when I wrote this).

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

Page 1

Page 2

Davislor
  • 44,045