Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.


I want to explain C# programming language step by step using overlay in beamer.cls. But it does NOT works as shown in the following figure.

How to solve this problem?

Code Snippet




using System;
public delegate void Foo(object o);
\uncover<1>{public class Foo}
\uncover<3>{public static void Main()}


share|improve this question

3 Answers 3

up vote 17 down vote accepted

use the semiverbatim environment, it is more flexible in this case. Here is an example how I did it some time ago:


\begin{frame}[fragile]{Matrixoperationen}{Benutzung von \texttt{semiverbatim}}
\setbeamercolor{alerted text}{fg=blue}
\setbeamerfont{alerted text}{series=\bfseries,family=\ttfamily}
\begin{semiverbatim} \small
\uncover<1->{\alert<0>{\alert<6>{sub \color{red}matrix_row2vector} \{}}
\uncover<2->{\alert<2>{  my $m = shift;    my($rows,$cols) = ($m->[1],$m->[2]);}}
\uncover<2->{\alert<2>{  my $r = shift;   # optional, which column from matrix}}
\uncover<2->{\alert<2>{  croak "Error: matrix hasn't 3D rows" unless ($colsRun also:>{}>3);}}
\uncover<3->{\alert<3>{  if ( defined $r ) \{}}
\uncover<3->{\alert<3>{    croak "Error: matrix hasn't that row" unless ($r < $rows);}}
\uncover<3->{\alert<3>{  \}}}
\uncover<4->{\alert<4>{  else \{}}
\uncover<4->{\alert<4>{    croak "Error: matrix is not a 3D row matrix"}}
\uncover<4->{\alert<4>{           unless ($rowsRun also: >>  1);}}
\uncover<5->{\alert<5>{    $r = 0;}}
\uncover<4->{\alert<4>{  \}}}
\uncover<6->{\alert<6>{  return Math::VectorReal->%

\visible<6>{Beachte den Gebrauch von \alert{\color{red}\texttt{shift}}.}

If you have a lot of TeX code to show then define something like


then it easier to write something like \Lcs{newpage}.

share|improve this answer
@Herbert, thanks again for answering. –  xport Jan 6 '11 at 10:17
Hi, does anybody know how to use lstlisting to get syntaxhighlighting? –  Tobi Apr 26 '11 at 19:20
@Tobi: That should have been posted as a fresh question. Buried here in a comment, almost no-one will see it. –  Loop Space May 12 '11 at 18:23
@Andrew. Thanks. I would have made an extra question but it wasn’t so important for me. I “solved“ the problem with semi transparent overlay (TikZ) so that I can focus a snippet while keeping the whole code visible. I thinks this is even better than view the code line by line. If someone likes I can make an example as another answer to this question –  Tobi May 12 '11 at 18:34
@Tobi: Post it. Sounds useful (I can imagine uses for that in mathematical lectures too). –  Loop Space May 12 '11 at 18:38

Even though xpert has already accepted the (frequently given) "use semiverbatim" answer by Herbert, I would like to present my listings-only solution here as well – for those like Tobi, who do not want to give up on syntax coloring and all the other cool features of the listings package.

The first thing to do (similar to Herbert's solution) is to not use beamer's \defverbatim, but to embed the listings inside the frame using the [fragile] option. (The whole point of \defverbatim is that the content is expanded before \begin{frame}, hence overlay specifications just don't work.)

The trick is then to use the moredelim style option of listings to inject the overlay-command into the listing:




\begin{frame}[fragile]{MyListing 1}
  \begin{lstlisting}[style=base, gobble=4]
    using System;
    public delegate void Foo(object o);
    @public class Foo@
    @  public static void Main()@

Note that I'am using \only instead of \uncover, as the formatting given by \moredelim** is applied additionally to all other formattings of the current line, which means that \uncover has basically no effect here.

To achive not only highlighting of the current line, but also "dimming" of the remaining parts (as in Tobi's solution), we have to play a bit more around with listing styles. The idea is to have one style (base) that renders the listing "dimmed" and another one (highlight) that is applied on top of it in the \moredelim command (using \lstset) to remove the "dimming" for the current line. This works for all style elements, but basicstyle, so we apply the "nondimmed"-version of basicstyle (which here is \color{black}) manually in the moredelim command:


\begin{frame}[fragile]{MyListing 2}
  \begin{lstlisting}[style=base, gobble=4]
    using System;
    public delegate void Foo(object o);
    @public class Foo@
    @  public static void Main()@

enter image description here

share|improve this answer
Seems to be a very good idea :-) I’ll keep it in mind. But why don’t you add basicstyle=\color{black} to higlight-style and remove \color{black} from the moredelim-line? –  Tobi May 15 '11 at 12:35
@Tobi: As explained above (albeit not very clear...) some listing styles apparently cannot be changed "in the middle" of an listing, among them basicstyle and backgroundcolor. Putting them into 'highlight` has just no effect. –  Daniel May 15 '11 at 13:25
Thanks I didn’t see/read it O:-) –  Tobi May 15 '11 at 21:13

As requested my answer about using TikZ with semi transparent overlays.



% basic packages

% load TikZ to draw the heighlighting areas
    \newcommand{\tikzref}[1]{% to define an anchor
        \tikz[remember picture]{%
            \coordinate (#1) at (0,0.5ex);%
          fill opacity=0.75,fill=white, inner sep=1.5mm,
          insert path={(current page.north east) --
            (current page.south east) --
            (current page.south west) --
            (current page.north west) --
            (current page.north east)},

% use listings to show an example how to heighlight code
        escapechar={§},% needed to set tikz anchors in listings


\begin{frame}{Heighlighting parts of an equation}
% 1.  Set the equation as usual. Add anchors with \tikzmark at the beginning and
%     end of the formular, named bmath and emath in this example, and at the
%     beginning and end of the part you want to heighlight, named bfrac and efrac.
    \tikzref{bmath}f(x) =
        \left(2x^4 + \tikzref{bfrac}\frac{3x^3}{4m}\tikzref{efrac}+
% 2.  Add the code to highlight
% 2.1 The {tikzpicture} shouldn't take space on the frame but 'overlay' it and
%     we make the \tikzref anchors acessible via 'remember picture'.
\begin{tikzpicture}[overlay, remember picture]
% 2.2 Draw the frame around the part that should be heighlighted, i.e. not coverd
%     by the semi-transparent overlay. This path has the 'clip' option meaning
%     that all following path are clipped by that.
    \path [clip] % [A, see Notes]
        ($(bfrac) - (0,5mm)$) -- %
        ($(efrac) - (0,5mm)$) -- %
        ($(efrac) + (0,5mm)$) -- %
        ($(bfrac) + (0,5mm)$) --%
% 2.3 Add the semi-transparent overlay to cover the whole equation.
    \node [transp,fit={($(bmath)+(0,5mm)$) ($(emath)-(0,5mm)$)}] {};

\begin{frame}[fragile]{Heighlighting parts of a code example}
% 1.  Set the code environment as usual and add \tikzref anchors. Here § is used
%     to escape from vermatim back to LaTeX.
        §\tikzref{z6}§    \lstset{basicstyle=\ttfamily}§\tikzref{lstset}§
        §\tikzref{z7}§    \lstMakeShortInline|§\tikzref{svrb}§
        Eine \emph{Auszeichnung} und 
        §\tikzref{bvrb}§|verb§\tikzref{verb}§atim|-Text§\tikzref{evrb}§ enthält.
        \dots und eine {unbedeutende Gruppe}.
        % Kommentare erscheinen nicht in der Ausgabe§\tikzref{txt}§
% 2.  Add the {tikzpicture} to draw the heiglighting (see example A).
    \begin{tikzpicture}[overlay, remember picture]
        % clipping:
        \begin{scope}% [B, see Notes]
            % cut out 1
            \path [clip] let \p1=(z5), \p2=(z7), \p3=(lstset) in%
                ($(z8) + (0,1ex)$) %
                -- ++(\x3-\x1,0) %
                -- ++($(0,0) + (0,2ex) + (0,\y1-\y2)$)
                -- ++(\x1-\x3,0) %
                -- cycle [reverseclip];
            % cut out 2
            \path [clip] %
                ($(bvrb) - (0,1ex)$) %
                -- ($(evrb) - (0,1ex) $) %
                -- ($(evrb) + (0,1ex) $) %
                -- ($(bvrb) + (0,1ex)$) %
                -- cycle [reverseclip];
            % clip everything without cutted areas
            \node [transp,fit=(z1) (z15) (txt)] {};
% 3.  Add some additional text annotations using 'nodes' and the \tikzref anchors.
        \draw<2-> [<-] ($(lst) + (\ddx,0)$) -- +(\dx,0) node [right, anchor=mid west] {load};
        \draw<3-> [<-] ($(lstset) + (\ddx,0)$) -- +(\dx,0) node [right, anchor=mid west] {mono font};
        \draw<4-> [<-] ($(svrb) + (\ddx,0)$) -- +(\dx,0) node [right, anchor=mid west] {short verb};
        \draw<5-> [<-] ($(verb) + (0,-\ddy)$) |- +(0.3,-0.5) node [right, anchor=mid west] {example};





  • To get a semi transparent overlay with some cut out areas, I lay a node above the whole relevant part of a frame and clip this. The needed reveseclip-Option was created by Jake at How can I invert a 'clip' selection within TikZ?.

  • While designing this example I’ve found out that the direction of drawing clip path [A] is relevant so try drawing path (counter) clockwise if it doesn’t work.

  • Note that TikZ needs two runs of (pdf)LaTEX to get the right positions.

  • In some cases [B] it seems to be a good idea to limit the clipping to a {scope} to make other drawings which are not effected by it.

  • A problem might be that we need to contaminate the original listing code with the §\tikzref{…}§. Maybe someone has an idea how to export adjusted listing automatically?

share|improve this answer
I just used this idea to make different parts of a slide stand-out at different times. So I was right to get you to post this code! –  Loop Space May 23 '11 at 11:25
@Andrew: Thanks, I’m glad to help you :-) –  Tobi May 23 '11 at 13:28
@Tobi can you please post a simpler example? How to animate this in multiple places? –  ATOzTOA Mar 2 '13 at 10:37
@ATOzTOA: Can you explain, why you need one? I can’t see many options to simplify my example. At least the second frame “Math” contains an easy example I guess. just ignore the first frame … If you tell me what you don’t understand I’ll add more information and appropriate examples. –  Tobi Mar 6 '13 at 16:46
@Tobi Wow, sorry.... i get it now... kool... –  ATOzTOA Mar 6 '13 at 17:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.