Text with gaps and white text

I have a kind of learning document with exercises. In this document I have texts with gaps. The gaps are currently filled by dots. I am using the following commands for this purpose:

\newcommand{\gapunit}{.....}
\newcommand{\tinygap}{\gapunit}

% ===== EGREG VARIANT =====
\makeatletter
\newcount\my@repeat@count% initialize a new counter for the loop
\newcommand{\myrepeat}[3]{% new command with 2 arguments
\begingroup% using a group here allows nested calls
\my@repeat@count=1% initialize at 1, so that there are argument - 1 iterations and the last iterations doesn't have a separator following it
\@whilenum\my@repeat@count<#1\do{#2#3\advance\my@repeat@count1}#2% as long as the iteration count is smaller than the argument, advance, meaning that the counter will be increased by 1
\endgroup%
}
\makeatother

\newcommand{\shortgap}{%
\myrepeat{3}{\gapunit}{\ }
}
\newcommand{\mediumgap}{%
\myrepeat{5}{\gapunit}{\ }
}
\newcommand{\longgap}{%
\myrepeat[10]{\gapunit}{\ }
}


Now I want to add solutions to the PDF which I compile. I thought maybe I could add white text, which I write on top of the dots (want to have the dots and the white text) to add solutions. I am quite OK with the idea, that someone could highlight white text to read it. In the end either someone "cheats themselves" or really learns something.

But how can I write white text on top of the gaps?

If there is a not too complicated better way of getting solutions into my PDF, I am open to ideas as well. I just don't want to lose the gap dots and don't want to add pages of code to my template, to get something super fancy, when actually white text is totally sufficient : )

Good would be a solution, which figures out the background color used by the document somehow, so that it does not have to be set in stone to be white.

Here is the document on GitHub, in case I am too vague about the gaps thing or something: repo

Edit

I am using Andrew's solution in a slightly modified way:

\newlength\blankblanklength
\newlength\blanktextlength
\newlength\blankcenterindentation
\newcommand\BlankText[2][]{%
% do some length calculations for text length
\if\relax\detokenize{#1}\relax%
\settowidth{\blanktextlength}{#2}%
\else%
\setlength\blanktextlength{#1}%
\fi% calculate width of text
\rlap{\makebox[\blanktextlength][c]{\textcolor{white}{#2}}}% write the text without moving, centered in a box
\raisebox{-0.5ex}{\hbox to \blanktextlength{\dotfill}}% add dots to cover text
}

\newcommand{\ShortBlankText}[1]{%
\BlankText[2cm]{#1}
}
\newcommand{\MediumBlankText}[1]{%
\BlankText[3.2cm]{#1}
}
\newcommand{\LongBlankText}[1]{%
\BlankText[4.4cm]{#1}
}

• Are you looking for something like xsim's \blank command? – cgnieder Oct 16 '17 at 9:50
• After \usepackage{xcolor} you can use \textcolor{white}{solution text} to write white text. – Andrew Oct 16 '17 at 10:29
• @clemens The docs say: "Creates a blank in normal text or in n exercise but fills the text of its argument if inside a solution. If used at the begin of a paragraph \blank will do two things: it will set the linespread according to an option explained below and will insert \par after the lines. The starred version doesn’t do these things." – Does not sound like having placeholder dots plus something invisible. I don't think this is a solution for me. – Zelphir Kaltstahl Oct 16 '17 at 11:36
• @Andrew How does that keep the dots as well? That seems like it would only write white text. – Zelphir Kaltstahl Oct 16 '17 at 11:37
• You write the dots as you current are, putting either the dots or the white text inside either\llap{...} or \rlap{...}. I'll add an example solution. – Andrew Oct 16 '17 at 12:33

Edit II

Fixing the behaviour when used at the start of a paragraph. The macro now has two optional arguments:

 %usage: \BlankText(colour)[length]{text}


The default colour for the "blank text" is white and the default length for the dots is the length of the "blank text".

Edit

Adding an optional argument to set the length of dots.

The MWE below shows how to use \BlankText with its two optional arguments. It produces:

Here is the code:

\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}

\newlength\blanktextlength
% usage: \BlankText(colour: default=white)[length]{text}
\NewDocumentCommand\BlankText{ D(){white} o m }{%
\IfNoValueTF{#2}{% no width specified
\settowidth{\blanktextlength}{#3}%
\makebox[0pt][l]{\textcolor{#1}{#3}}% write the text without moving
}{% calculate width of text
\setlength\blanktextlength{#2}%
\makebox[0pt][l]{\hbox to \blanktextlength{\hfil\textcolor{#1}{#3}\hfil}}
}%
\raisebox{-0.5ex}{\hbox to \blanktextlength{\dotfill}}% add dots for text
}

\begin{document}

\BlankText{Start of paragraph}
Here is some white text: \BlankText{Hello}
Here is a little more: \BlankText{Can you read me through the dots?}

Same again with a declared width:
\BlankText[8cm]{Can you read me through the dots?}

\bigskip\noindent With \textcolor{blue}{blue} blank text:

\BlankText(blue){Start of paragraph}
Here is some blue text: \BlankText(blue){Hello}
Here is a little more: \BlankText(blue){Can you read me through the dots?}

Same again with a declared width:
\BlankText(blue)[8cm]{Can you read me through the dots?}

\end{document}


The previous problem with using \BlankText was caused by \llap{...} not playing well with LaTeX paragraphs. As detailed in \llap (or \rlap) at the beginning of an indented paragraph, the solution is to use \makebox[0pt][l]{...}.

Note that the \BlankText text will run off the right-hand margin the text is too long to fit on the line.

• This is what I was looking for. Is there an easy way to put the dots a bit lower or the text a bit higher, so that they do not overlap? (Should I put it into a new question, because it is a non-trivial thing to do?) – Zelphir Kaltstahl Oct 16 '17 at 13:13
• agreed with @Zelphir that the dotted line should be a bit lower -- about the same distance below the baseline as an underline applied to text with no descenders would be. – barbara beeton Oct 16 '17 at 14:15
• @Zelphir I have lowered the dots a little using \raisebox{-0.5ex}{...}. I used -0.5ex as a rough guess as to the right distance and it looks OK but you may need to fine-tune. – Andrew Oct 16 '17 at 16:12
• @Andrew: There seems to be a problem with indention: In case a paragraph begins with \BlankText{...} the output (whether white or blue) is wrong. Prefixing this paragraph with \noindent delivers correct output. – lAtExFaN Oct 16 '17 at 19:23
• @Andrew There is one inconvenience left when using this: When you have to insert words into a text and the number of dots shows you how many letters the word has, it is a clue. However this can be circumvented by simply inserting whitespace into the \BlankText{text<whitespace here>}, so I'd not consider it a problem. This is very useful already. If one wanted to improve it even more, one might add a feature to declare a length for the number of dots and indirectly the space they take. – Zelphir Kaltstahl Oct 16 '17 at 20:23

With this example, your dotted gap is as long as the typesetted text would be in the same font:

\documentclass{article}
\usepackage{color}
\makeatletter
\def\gap#1{%
\setbox\@tempboxa\hbox{{\color{white}#1}}%
\@tempdima\wd\@tempboxa
\rlap{\unhbox\@tempboxa}%
\hb@xt@\@tempdima{\dotfill}%
}
\makeatother

\begin{document}

Bla bla bla \gap{Answer 1} Bla bla bla Bla bla bla Bla bla bla Bla bla
bla Bla bla bla Bla bla bla Bla bla bla \gap{Answer 2 is a bit longer}
bla blab bla.

\end{document}


You could, however, add some font size commands beneath the color-command if you need more space, like

  \setbox\@tempboxa\hbox{{\color{white}\Large#1}}%

• This somehow leads to curious effects in my document: There are no dots visible and the text being white is not restricted to the "answer", but extends to text after the answer as well. – Zelphir Kaltstahl Oct 16 '17 at 12:11
• Sorry, i changed my posting after I tested it. I edited my answer to include an additional pair of braces in the box definition. That should make the color change local again. – Lupino Oct 16 '17 at 12:13
• This also works now. I can only accept one answer unfortunately. – Zelphir Kaltstahl Oct 16 '17 at 13:15