以下是 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}
编辑1:
经过一番搜索,问题幻像和换行符或许可以解释原因,但没有令人满意的解决方案。
答案1
您不能\hphantom
直接使用,因为它会构建一个牢不可破的盒子。在下面的代码中,我将参数拆分为空格并构建每个项目的幻像。
\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}
不过,最好使用\raggedright
排版,以避免连字符。
答案2
这只是对 ulem 的一个微小的调整。
\usepackage[normalem]{ulem}
\makeatletter
% Altered \UL@putbox:
\def\UL@putbox{\ifx\UL@start\@empty \else % not inner
\vrule\@width\z@ \LA@penalty\@M
{\UL@skip\wd\UL@box \UL@leaders \kern-\UL@skip}%
\ifanswer \box\UL@box % The original
\else \hbox to\wd\UL@box{}% Special for invisible
\fi %
\fi}