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}
Martin Scharrer
  • 262,582
student
  • 29,003
  • 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. – Christian Lindig Apr 17 '11 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 '11 at 15:07

2 Answers2

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

Martin Scharrer
  • 262,582
  • 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 '11 at 15:05
  • @user4011: After thinking about it I don't think so, because this would affect the line breaking again. – Martin Scharrer Aug 29 '11 at 11:47
  • 1
    Sidebar: The censor package now supports linebreaks through its \blackout command. – Steven B. Segletes Apr 23 '13 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 '16 at 04: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. – Martin Scharrer May 31 '16 at 18:50
  • @MartinScharrer Is it possible to have your macro work in math environment as well? Currently it underlines formulas without hiding them. Thanks! – Tobard Jan 07 '17 at 13:05
  • @Steven B. Segletes but macros\\\ or newline does not work in hiddent text with blackout though auto line-feed and \par works. – lyl Jun 17 '21 at 13:53
  • @lyl I have been working on an improved version that would support the \ linebreaking. It employs my new tokcycle package to rework things. – Steven B. Segletes Jun 17 '21 at 14:03
  • @lyl \usepackage{tokcycle}[2021/03/10] \makeatletter \newif\ifexpandarg \def\@dump#1{\addcytoks[1]{\expandafter\censor\expandafter{#1}}\def#1{}} \def\spacelap{.6ex} \def\censpace{\rlap{\censorrule{\spacelap}} \llap{\censorrule{\spacelap}}} \def\@append#1#2{\tc@defx#1{#1#2}} – Steven B. Segletes Jun 17 '21 at 14:19
  • @lyl \xtokcycleenvironment\xblackoutenv {\@append\censored@word{##1}} {\tctestifcon\ifexpandarg{\expandafter\processtoks\expandafter{\expanded{##1}}}% {\processtoks{##1}}\@dump\censored@word\expandargfalse} {\@dump\censored@word\tctestifx{~##1}{\@append\censored@word{##1}}% {\tctestifx{\expanded##1}{\expandargtrue}{\addcytoks{##1}}}} {\@dump\censored@word\addcytoks{\censpace}} {\def\censored@word{}} {\@dump\censored@word} – Steven B. Segletes Jun 17 '21 at 14:19
  • @lyl \xtokcycleenvironment\blackoutenv {\@append\censored@word{##1}} {\tctestifcon\ifexpandarg{\expandafter\processtoks\expandafter{\expanded{##1}}}% {\processtoks{##1}}\@dump\censored@word\expandargfalse} {\@dump\censored@word\tctestifx{~##1}{\@append\censored@word{##1}}% {\tctestifx{\expanded##1}{\expandargtrue}{\addcytoks{##1}}}} {\@dump\censored@word\addcytoks{##1}} {\def\censored@word{}} {\@dump\censored@word} \makeatother – Steven B. Segletes Jun 17 '21 at 14:20
  • @lyl Usage: This \xblackoutenv is a test is a test. Checking for line wrap again and again. This is really an issue if there is\\ a problem. This is a test is a test. \textit{Checking for line wrap again and again.} This is really an issue\endxblackoutenv. Still not finalized, but working on it. – Steven B. Segletes Jun 17 '21 at 14:20
  • Many many thanks @Steven B. Segletes for this. Would you please step to https://tex.stackexchange.com/questions/601473/how-to-hide-text-content-with-line-break-by-blackout – lyl Jun 17 '21 at 14:27
  • @Steven B. Segletes When is the newest version of the package available in CTAN? Or other way to get it? – lyl Jun 17 '21 at 15:19
  • @lyl It is not yet available. But if you copy/paste what I put above in to a preamble, along with loading the existing censor package, you will have access to the two pseudo-environments, \blackoutenv...\endblackoutenv and \xblackoutenv...\endxblackoutenv. – Steven B. Segletes Jun 17 '21 at 15:43
  • @Steven B. Segletes I tried, and get this fault message: ! LaTeX Error: Filetokcycle.sty' not found.` I use xelatex. – lyl Jun 17 '21 at 16:02
  • @lyl You are missing the tokcycle package. If you can't get your system to install it, then you can download files from https://ctan.org/pkg/tokcycle. Needed for compilation are tokcycle.sty and tokcycle.tex. – Steven B. Segletes Jun 17 '21 at 16:14
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.
Torbjørn T.
  • 206,688
user36299
  • 101
  • 8
    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 '18 at 23:41