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 am trying to create a beamer presentation in which I would step through an algorithm, highlighting the current line in the algorithm on each slide. I know it is possible to do with the semiverbatim environment, however, my algorithm has to be written in pseudocode. I am trying to match the pseudocode format that is used by the textbook I am using, and for that the algpseudocode package that is a part of the algorithmicx package seems to do an excellent work.

However, I cannot figure out how to highlight the current line in the algorithmic environment. I tried to do this:

\documentclass[ignorenonframetext]{beamer}
\usepackage{algpseudocode}

\begin{document}
\section{Blah}
\begin{frame}
   \frametitle{Blah}
   \begin{columns}
 \begin{column}{.4\hsize}
    \begin{algorithmic}
       \alert<1>{\State $z \gets 1$}
       \alert<2>{\For{$i = 1 \to n$}}
       \alert<3>{\State $z \gets z + \frac{1}{i}$}
       \alert<4>{\EndFor}
    \end{algorithmic}
 \end{column}%
 \begin{column}{.6\hsize}
    Some text discussing the current line.
 \end{column}
   \end{columns}
\end{frame}
\end{document}

however that gives me an error about missing \endcsname. Does anybody have any suggestion on how to achieve this?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

The problem seems to be that the algorithmic environment needs to be able to "see" the commands \State, \For, \EndFor at the top level. It doesn't like having them in the \alert environments. One way around this is to simulate the alerting by merely changing the text colour on each line. Here's a way to do it:

\documentclass[ignorenonframetext]{beamer}
\usepackage{algpseudocode}

\newcommand{\alertline}{%
 \usebeamercolor[fg]{normal text}%
 \only{\usebeamercolor[fg]{alerted text}}}

\begin{document}
\section{Blah}
\begin{frame}
   \frametitle{Blah}
   \begin{columns}
 \begin{column}{.4\hsize}
    \begin{algorithmic}
\alertline<1>\State  $z \gets 1$
\alertline<2>\For    {$i = 1 \to n$}
\alertline<3>\State  $z \gets z + \frac{1}{i}$
\alertline<4>\EndFor
    \end{algorithmic}
 \end{column}%
 \begin{column}{.6\hsize}
    Some text discussing the current line.
 \end{column}
   \end{columns}
\end{frame}
\end{document}

This approach doesn't work if you want the background to be highlighted as that puts the text in a \colorbox which seems to hide it from the algorithmic environment.

(Note: my suppositions about what's happening are based purely on observation and not on any knowledge of what's going on under the bonnet.)

share|improve this answer
    
Overlays don't work, when they are used inside \For, \If, etc. But when you wrap the whole \For, \If it works like a charm –  damluar Feb 5 '13 at 22:39
    
What do you do if you need to highlight a line twice, e.g. a for-loop you might need to highlight the inner part more than once. I've tried this with your solution, but it seems not to work. –  moose Sep 18 '13 at 15:01
add comment

I don't know what kind of overlay do you prefer, but it works with using \uncover:

\documentclass[ignorenonframetext]{beamer}
\setbeamercovered{transparent}
\usepackage[T1]{fontenc}
\usepackage{bera}
\usepackage{algpseudocode}

\begin{document}
\section{Blah}
\begin{frame}{Blah}
   \begin{columns}
 \begin{column}{.4\hsize}
    \begin{algorithmic}
       \uncover<1>{\State $z \gets 1$}
       \uncover<2>{\For{$i = 1 \to n$}}
       \uncover<3>{\State $z \gets z + \frac{1}{i}$}
       \uncover<4>{\EndFor}
    \end{algorithmic}
 \end{column}%
 \begin{column}{.6\hsize}
    Some text discussing the current line.
 \end{column}
   \end{columns}
\end{frame}
\end{document}
share|improve this answer
    
For me at works also with \alert (but not in the case of \IF when using the algorithmic package) –  Dror Apr 8 at 8:44
add comment

Your Answer

 
discard

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.