[Tex/LaTex] How to create a Beamer overlay \cover command, dual to \uncover

beameroverlays

I would like to define a command \mycover that is dual to Beamer's \uncover. In beamerbaseoverlay.sty the \uncover command is defined as

\newcommand{\uncover}{\alt{\beamer@fakeinvisible}{\beamer@makecovered}}

Therefore, my first attempt at \mycover simply switches the \alt branches:

\newcommand{\mycover}{\alt{\beamer@makecovered}{\beamer@fakeinvisible}}

Unfortunately, the first slide in \mycover's overlay specification is always shown as completely invisible, even if I \setbeamercovered{transparent}. Here is an example:

\documentclass{beamer}

\makeatletter
\newcommand{\mycover}{\alt{\beamer@makecovered}{\beamer@fakeinvisible}}
\def\c@slideinframe{\beamer@slideinframe}
\makeatother

\setbeamercovered{transparent}

\begin{document}
\begin{frame}{}
  \structure{Slide \arabic{slideinframe}}

  \uncover<1,4>{Testing} \mycover<2-3>{my cover} \uncover<1,4>{command.}
\end{frame}
\end{document}

The words "my cover" should be covered on exactly the same slides as the words "Testing" and "command", namely slides 2 and 3. What actually happens, however, is that "my cover" is completely invisible on slide 2 and is properly covered on slide 3.

enter image description here

What is the problem with \mycover and how can I fix it?

Best Answer

The following definition seems to produce the desired result:

\documentclass{beamer}

\makeatletter
\def\beamer@startmycovered{%
  \def\opaqueness<##1>##2{%
    \only<##1>{%
      \beamer@actions{%
        \expandafter\xdef\csname beamer@oldcolorhook%
        \the\beamer@coveringdepth\endcsname{\beamer@colorhook}%
        \expandafter\xdef\csname beamer@oldpgfextension%
        \the\beamer@coveringdepth\endcsname{\beamer@pgfextension}%
        {\globalcolorstrue\colorlet{beamer@freeze\the\beamer@coveringdepth}{bg}}%
        \xdef\beamer@colorhook{!##2!beamer@freeze%
          \the\beamer@coveringdepth\beamer@colorhook}%
        \gdef\beamer@pgfextension{!##2opaque}%
        \color{.}%
      }%
      {%
        \xdef\beamer@colorhook{\csname beamer@oldcolorhook%
          \the\beamer@coveringdepth\endcsname}%
        \xdef\beamer@pgfextension{\csname beamer@oldpgfextension%
          \the\beamer@coveringdepth\endcsname}%
        \color{.}%
      }}}%
  \ifnum\beamer@slideinframe<\beamer@minimum%ok, at beginning
  {%
    \beamer@saveanother%
    \advance\beamer@minimum by-\beamer@slideinframe%
    \beamer@slideinframe=\beamer@minimum%
    \beamer@uncoverbeforeactions%
    \beamer@restoreanother%
  }%
  \else%
  {%
    \beamer@saveanother%
    \advance\beamer@slideinframe by-\beamer@minimum%
    \beamer@uncoverafteractions%
    \beamer@restoreanother%
  }%
  \fi%
  \beamer@do%
%  }%
}

\long\def\beamer@makemycovered#1{\beamer@startmycovered#1\beamer@endcovered}
\def\mycover{%
\alt{\beamer@makemycovered}{\beamer@fakeinvisible}}
\def\c@slideinframe{\beamer@slideinframe}
\makeatother

\setbeamercovered{transparent}

\begin{document}

\begin{frame}
\frametitle{Test frame 1}
\structure{Slide \arabic{slideinframe}}

\uncover<1,5>{Testing} \mycover<2-4>{my cover} \uncover<1,5>{command.}
\end{frame}

\begin{frame}
\frametitle{Test frame 2}
\structure{Slide \arabic{slideinframe}}

Testing \mycover<2,4,6>{my cover} command.
\end{frame}

\begin{frame}
\frametitle{Test frame 3}
\structure{Slide \arabic{slideinframe}}

\mycover<2,6>{Testing} \mycover<1-3,5-7>{my cover} command.
\end{frame}

\end{document}

enter image description here

I used your original idea, but using a variation of the original \beamer@startcovered used in \beamer@makecovered.

Although I don't use much overlay specifications in my presentations (\pause, \only and \onslide are enough for me) I like your idea a lot; I think this command could be a part of the official beamer class.