Trying to hide blocks of text in beamer

Motivation: giving lectures using beamer, I show all the details of solutions to examples, but in the handout I would like to hide these. students fill out the missing text during lectures, which seems to encourage attendance. Colleagues do this by having two separate latex files - but I'd rather have avoid that for obvious reasons.

I have tried (pretty successfully) to use the code suggested by Bruno Le Floch in How to replace a large block of text by an empty block of the same size?

NB replacing font colour by white is not an option: it can still be read on a student's pdf reader.

However, I have come across 4 or 5 bugs (I guess these are probably foreseen by Bruno):

1. In an itemize list, if you put \item \hideit{some text} it hides the bullet as well as the text (not a grave problem!)
2. It interacts poorly with beamer's \pause command, even with the handout option enabled, where \pause should have no effect. The text before the \pause command is not hidden. (More serious)
3. It only works properly on \vboxes (I think). If you hide one word in a sentence (ie in an \hbox) the \hideit command introduces an end of paragraph. (OK, one could use \phantom in this case, but it would be nice to have one command.)
4. A strange colour effect following the eqnarray* environment: I have defined beamers math text to be blue, and if you \hideit{\begin{eqnarray*} equations \end{eqnarray*}} then the text that follows (which is not hidden) is blue!

(Because it's in beamer, I'm not concerned with page breaks.)

I copied Bruno's code into a file called hidestuff.tex, which I input into the following minimal example showing all 4 bugs:

\documentclass[handout]{beamer}
\usepackage{xparse}
\usepackage{ifthen}

\newboolean{showitall}
%\setboolean{showitall}{true}

\ifthenelse{\boolean{showitall}}{\def\hideit{}}
{\input{hidestuff}}

\setbeamercolor{math text}{fg=blue}

%%%%
\begin{document}

%%%%%%%%%%%
\begin{frame}
\frametitle{Two bugs:}
{\color{red}Error with itemize}
\begin{itemize}
\item The line below should have a bullet' (only the
text should be hidden)
\item \hideit{Some text to be hidden}
\item This is visible again.
\end{itemize}

\bigskip

{\color{red}Error with beamer's pause command:}

\textbf{Example 1:}\quad  Differentiate \ $f(x)=x^2\sin x$

\bigskip
\underline{\it Solution:}
\hideit{Let $u=x^2$ and $v=\sin x$. (this line should be hidden)

\pause  Then $u'=2x$ and $v'=\cos x$.

Using the product rule we get
$$f' = 2x\sin x + x^2\cos x.$$
}
\end{frame}

%%%%%%%%%%%
\begin{frame}
\frametitle{Two more bugs:}

{\color{red}Error with inline' hiding:}

Here is some \hideit{hidden} text on one line.

\bigskip

{\color{red}Error with eqnarray* in beamer:}

\textbf{Example 1:}\quad  Differentiate \ $f(x)= \frac{\sin x}{e^x}$.

\medskip

\underline{Solution:} Let $u=\sin x$ and $v=e^x$.
Then $u'=\cos x$ and $v'=e^x$.  Therefore

\hideit{\begin{eqnarray*}
f'(x) &=& \frac{e^x\cos x-(\sin x)e^x}{(e^x)^2} \\
&=& \frac{e^x(\cos x-\sin x)}{(e^x)^2} \\
&=& \frac{\cos x-\sin x}{e^x}.
\end{eqnarray*}
}

And that's how you do it! (This should be black text)

\end{frame}
%%%%%%%%%%%
\end{document}


If you uncomment \setboolean{showitall}{true} then it will output all the text, hidden and not hidden.

I don't understand the expl3 language used, but if anyone has any suggestions for patching the code to avoid these bugs, I'd be very grateful. Thanks.

• Welcome to TeX.SX! Unrelated: $$...$$ and eqnarray* are bad – user31729 Sep 6 '15 at 16:45
• Isn't this easier with \mode<handout> etc? – user31729 Sep 6 '15 at 16:56
• Isn't what easier? I think \mode<handout> is the same as passing the option to beamer. But AFAIK it doesn't hide text. – JamesM Sep 6 '15 at 16:58
• You can specify what you want to show in the handout version using it. Then, when you pass the option to Beamer, that's what you'll get. I'd think this would be the obvious way to do it, too. – cfr Sep 6 '15 at 17:10
• A simpler solution could be found here-> texdev.net/2014/01/17/the-beamer-slide-overlay-concept – youmna Ismaeil Apr 15 '18 at 9:39

Here's a way of defining a \hideit using Beamer's extended \newcommand:

\documentclass[handout]{beamer}

\setbeamercolor{math text}{fg=blue}

\newcommand\hideit[1]{%
\only<0| handout:1>{\mbox{}}%
\invisible<0| handout:1>{#1}}

\begin{document}

\begin{frame}
\frametitle{Two bugs:}
{\color{red}Error with itemize}
\begin{itemize}
\item The line below should have a bullet' (only the
text should be hidden)
\item \hideit{Some text to be hidden}
\item This is visible again.
\end{itemize}

\bigskip

{\color{red}Error with beamer's pause command:}

\textbf{Example 1:}\quad  Differentiate \ $f(x)=x^2\sin x$

\bigskip
\underline{\itshape Solution:}
\hideit{Let $u=x^2$ and $v=\sin x$. (this line should be hidden)

\pause  Then $u'=2x$ and $v'=\cos x$.

Using the product rule we get
$$f' = 2x\sin x + x^2\cos x.$$
}
\end{frame}

%%%%%%%%%%%
\begin{frame}
\frametitle{Two more bugs:}

{\color{red}Error with inline' hiding:}

Here is some \hideit{hidden} text on one line.

\bigskip

{\color{red}Error with eqnarray* in beamer:}

\textbf{Example 1:}\quad  Differentiate \ $f(x)= \frac{\sin x}{e^x}$.

\medskip

\underline{Solution:} Let $u=\sin x$ and $v=e^x$.
Then $u'=\cos x$ and $v'=e^x$.  Therefore

\hideit{\begin{eqnarray*}
f'(x) &=& \frac{e^x\cos x-(\sin x)e^x}{(e^x)^2} \\
&=& \frac{e^x(\cos x-\sin x)}{(e^x)^2} \\
&=& \frac{\cos x-\sin x}{e^x}.
\end{eqnarray*}
}

And that's how you do it! (This should be black text)

\end{frame}
\end{document}


And for the presentation:

• This seems to work perfectly. I obviously need to read the user guide more closely. Thanks. – JamesM Sep 9 '15 at 20:06
• @JamesMontaldi Excellent! – cfr Sep 9 '15 at 21:14

I shortened the example to show the relevant feature with \mode<handout>{...} and \mode<beamer>{...}, which is basically beamer's own way of \hideit etc.

\documentclass[handout]{beamer}

\setbeamercolor{math text}{fg=blue}

%%%%
\begin{document}

%%%%%%%%%%%
\begin{frame}{Two bugs:}
%\frametitle{Two bugs:}
%{\color{red}Error with itemize}

\begin{itemize}
\item The line below should have a bullet' (only the text should be hidden)
\mode<handout>{%
\item
}%
\mode<beamer>{%
\item Some text to be hidden
}%
\item This is visible again.
\end{itemize}
\end{frame}
%%%%%%%%%%%
\end{document}
`

Beamer mode

Handout mode