27

I want to make a cloze test using LaTeX. My Idea was to use the \phantom command to produce the space:

Some text \underline{\phantom{some text to complete}} some more text.

Now the problem is that \phantom seems to handle "some text to complete" as one word and does not break lines.

\documentclass{minimal}

\begin{document}

Some text \underline{\phantom{some  text to complete some  text to complete
some  text to complete some  text to complete some  text to complete some    text
to complete some  text to complete}} some more text.

\end{document}
2
  • 1
    Unless you are setting the document in a large size the space created by \phantom will be too small for manual completion. I'd suggest to take a look at the exam package. Apr 17, 2011 at 13:05
  • 1
    Thanks, that's a good point. However exam seems to be a documentclass and I don't want to change the documentclass.
    – student
    Apr 17, 2011 at 15:07

2 Answers 2

27

Neither \underline nor \phantom allow for line breaks because they use the so called restricted horizontal mode, i.e. the same as a \mbox{...}. Your application reminded me about the censor package which is able to remove text and replace it by black bars or underlines. However, it seems also not to support line breaks. The ulem package gives \uline which underlines text in a line-breakable way. You could use it and redefine the internal macro which sets the text to use a phantom box. Note that this does only allows for line breaks at normal spaces and not for hyphenation.

\documentclass{article}
\usepackage[normalem]{ulem}

\makeatletter
\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}%
    \phantom{\box\UL@box}%
  \fi}
\makeatother

\begin{document}

Some text \uline{some text to complete some  text to complete
some  text to complete some  text to complete some  text to complete some    text
to complete some  text to complete} some more text.

\end{document}

Result or output

18
  • Thanks, that's great! Is it possible to adjust your solution such that \uline has an optional arguments where you can say that it should make n-times of the space as in your \uline version?
    – student
    Apr 17, 2011 at 15:05
  • @user4011: After thinking about it I don't think so, because this would affect the line breaking again. Aug 29, 2011 at 11:47
  • 1
    Sidebar: The censor package now supports linebreaks through its \blackout command. Apr 23, 2013 at 14:11
  • @MartinScharrer Hi, MartinScharrer . Your solution is really power. But could you please explain the macro part a little? I saw no uline in your macro, why it will affect uline?
    – user15964
    May 28, 2016 at 4:58
  • @user15964: It's simple: The \uline uses \UL@putbox internally. I just analyzed \uline to discover its internal behavior. This way I don't have to recreate the whole complex macro, but can rely on the existing one. May 31, 2016 at 18:50
10

I solved this problem by using the color package to write the phantom lines in white:

\usepackage{color}

\newcommand{\white}[1]{{\textcolor{white}{#1}}}

Some text \underline{\white{some text to complete}} some more text.
1
  • 7
    My lecture notes (provided as PDF) appear to be done like this (with blanks to fill in) and you can see what's in the blanks just by selecting the white text. That's harmless, since the fully-filled in notes get released a week or so after the lecture, but if you gave students a test in PDF format they would be able to see the answers, which isn't ideal.
    – GKFX
    Feb 27, 2018 at 23:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .