[Tex/LaTex] Trying to hide blocks of text in beamer

beamertext-decorations

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.

Best Answer

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}

handout

And for the presentation:

slides