3

Here is the MWE:

\documentclass{minimal}

% \usepackage{lipsum}


% \let\uline\underline
\usepackage{ulem}
\newif\ifanswer

\newcommand{\showanswer}[0]{\answertrue}
\newcommand{\hideanswer}[0]{\answerfalse}

\newcommand{\answer}[1]{
    \ifanswer{#1}\else\hphantom{#1}\fi
}

\newcommand{\fillin}[1]{%
    \uline{\hspace{0.5em}\answer{#1}\hspace{0.5em}}}


\begin{document}

Test0:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, \uline{vestibulum ut, placerat ac,adipiscing vitae, felis}. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuerid, vulputate a, magna.


\showanswer




Test1-1:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, \fillin{vestibulum ut, placerat ac,adipiscing vitae, felis}. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuerid, vulputate a, magna.


Test1-2:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, \uline{\hspace{0.5em}\ifanswer{vestibulum ut, placerat ac,adipiscing vitae, felis}\fi\hspace{0.5em}}. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuerid, vulputate a, magna.


Test1-3:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, \hspace{0.5em}\ifanswer\uline{vestibulum ut, placerat ac,adipiscing vitae, felis}\fi\hspace{0.5em}. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuerid, vulputate a, magna.


Test1-4:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, \hspace{0.5em}\ifanswer\uline{vestibulum ut, placerat ac,adipiscing vitae, felis}\else\uline{\hphantom{vestibulum ut, placerat ac,adipiscing vitae, felis}}\fi\hspace{0.5em}. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuerid, vulputate a, magna.




\hideanswer


Test2-1:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, \fillin{vestibulum ut, placerat ac,adipiscing vitae, felis}. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuerid, vulputate a, magna.


Test2-2:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, \uline{\hspace{0.5em}\ifanswer{vestibulum ut, placerat ac,adipiscing vitae, felis}\fi\hspace{0.5em}}. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuerid, vulputate a, magna.


Test2-3:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, \hspace{0.5em}\ifanswer\uline{vestibulum ut, placerat ac,adipiscing vitae, felis}\fi\hspace{0.5em}. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuerid, vulputate a, magna.


Test2-4:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, \hspace{0.5em}\ifanswer\uline{vestibulum ut, placerat ac,adipiscing vitae, felis}\else\uline{\hphantom{vestibulum ut, placerat ac,adipiscing vitae, felis}}\fi\hspace{0.5em}. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuerid, vulputate a, magna.


\end{document}

Edit 1:

After some searching, the question Phantom and line break may explain the reason but without a satisfactory solution.

  • This behavior does not seem surprising and, at least in the case of Test1-3, you have found the better way to accomplish it. – Steven B. Segletes Sep 26 '19 at 10:24
  • \hphantom builds a box that cannot be split across lines. – egreg Sep 26 '19 at 10:34
1

You can't use \hphantom directly, because it builds an unbreakable box. In the code below I split the argument at spaces and build phantoms of each item.

\documentclass{article}
\usepackage{xparse}
\usepackage{ulem}

\ExplSyntaxOn
\NewDocumentCommand{\fillin}{m}
 {
  \bool_if:NTF \g_bill_answer_bool { #1 } { \bill_answer_hide:n { #1 } }
 }
\NewDocumentCommand{\showanswer}{}{\bool_gset_true:N \g_bill_answer_bool}
\NewDocumentCommand{\hideanswer}{}{\bool_gset_false:N \g_bill_answer_bool}

\bool_new:N \g_bill_answer_bool
\seq_new:N \l__bill_answer_in_seq
\seq_new:N \l__bill_answer_out_seq

\cs_new_protected:Nn \bill_answer_hide:n
 {
  \seq_set_split:Nnn \l__bill_answer_in_seq { ~ } { #1 }
  \seq_set_map:NNn \l__bill_answer_out_seq \l__bill_answer_in_seq
   { \exp_not:n { \hphantom { ##1 } } }
  \exp_args:Nx \uline {\seq_use:Nn \l__bill_answer_out_seq { ~ } }
 }
\ExplSyntaxOff

\begin{document}

Test0

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, 
\uline{vestibulum ut, placerat ac, adipiscing vitae, felis, vestibulum ut, 
placerat ac, adipiscing vitae, felis, vestibulum ut, placerat ac, adipiscing 
vitae, felis}. Curabitur dictum gravida mauris. Nam arcu libero, nonummy 
eget, consectetuerid, vulputate a, magna.

\showanswer

Test1 (show)

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, 
\fillin{vestibulum ut, placerat ac, adipiscing vitae, felis, vestibulum ut, 
placerat ac, adipiscing vitae, felis, vestibulum ut, placerat ac, adipiscing 
vitae, felis}. Curabitur dictum gravida mauris. Nam arcu libero, nonummy 
eget, consectetuerid, vulputate a, magna.

\hideanswer

Test2 (hide)

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, 
\fillin{vestibulum ut, placerat ac, adipiscing vitae, felis, vestibulum ut, 
placerat ac, adipiscing vitae, felis, vestibulum ut, placerat ac, adipiscing 
vitae, felis}. Curabitur dictum gravida mauris. Nam arcu libero, nonummy 
eget, consectetuerid, vulputate a, magna.

\end{document}

enter image description here

It's better, though, that you use \raggedright typesetting, in order to avoid hyphenation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.