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 need two code blocks in my beamer frame. One will be the code and other will be the output for each line of code.

I want to uncover each code line and corresponding output line.

So, first it will show

code 1 line 1
code 1 line 2




output 1

and then

code 1 line 1
code 1 line 2
code 2 line 1
code 2 line 2



output 1
output 2

I cannot use semiverbatim because I need code formatting.

Here is my code:

\documentclass{beamer}
\usepackage{listings} %for listings of the source code

\lstset{basicstyle=\ttfamily}

\begin{document}

\lstset{language=sh}
\begin{lstlisting}
code 1
code 2
code 3
\end{lstlisting}

\begin{lstlisting}
output 1
output 2
output 3
\end{lstlisting}

\end{document}

Any ideas?

Only solution I could think of is, adding each code line into a separate savebox and then animating each savebox.

share|improve this question
    
tex.stackexchange.com/questions/8384/… holds nothing appropriate? (note that there are 3 answers) –  bloodworks Mar 2 '13 at 9:43
add comment

2 Answers

up vote 11 down vote accepted

A simple combination of Highlight text in code listing while also keeping syntax highlighting and Mindmap tikzpicture in beamer (reveal step by step) with one counter makes miracles (I should really thank Daniel :)).

The code:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{listings}

\usetheme{CambridgeUS}

% Daniel's code:
% http://tex.stackexchange.com/questions/55806/tikzpicture-in-beamer/55827#55827
  \tikzset{
    invisible/.style={opacity=0},
    visible on/.style={alt=#1{}{invisible}},
    alt/.code args={<#1>#2#3}{%
      \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
    },
  }

% Daniel's code:
% http://tex.stackexchange.com/questions/15237/highlight-text-in-code-listing-while-also-keeping-syntax-highlighting/49309#49309

\newcounter{step}

\makeatletter
\newenvironment{btHighlight}[1][]
{\begingroup\tikzset{bt@Highlight@par/.style={#1}}\begin{lrbox}{\@tempboxa}}
{\end{lrbox}\bt@HL@box[bt@Highlight@par]{\@tempboxa}\endgroup}

\newcommand\btHL[1][]{%
  \begin{btHighlight}[#1]\bgroup\aftergroup\bt@HL@endenv%
}
\def\bt@HL@endenv{%
  \end{btHighlight}%   
  \egroup
}
\newcommand{\bt@HL@box}[2][]{%
  \tikz[#1]{%
    \pgfpathrectangle{\pgfpoint{0pt}{0pt}}{\pgfpoint{\wd #2}{\ht #2}}%
    \pgfusepath{use as bounding box}%
    \refstepcounter{step}
    \node[visible on=<\thestep->,anchor=base west,rounded corners, fill=none,outer sep=0pt,inner xsep=0.5pt, inner ysep=2pt,  #1]{\usebox{#2}};
  }%
}
\makeatother


\lstset{language=sh,
        basicstyle=\footnotesize\ttfamily,
        keywordstyle=\footnotesize\color{blue}\ttfamily,
        moredelim=**[is][\btHL]{|}{|},
}

\begin{document}
\begin{frame}[fragile]{Code}

\begin{columns}
\begin{column}{0.45\textwidth}
\setcounter{step}{0}
\begin{lstlisting}
|code 1|
|code 2|
|code 3|
\end{lstlisting}
\end{column}
\begin{column}{0.45\textwidth}
\setcounter{step}{0}
\begin{lstlisting}
|output 1|
|output 2|
|output 3|
\end{lstlisting}
\end{column}
\end{columns}
\end{frame}
\end{document}

The result:

enter image description here

The style visible on is in charge of making visible the code: the sequence is achieved by means of the counter step, incremented at each step the code is highlighted. To make code and output synchronized, one just has to reset the counter before opening the lstlisting environment.

Disclaimer

I used the columns environment because in this case looks better, but the solution works also without it.

Improved solution: overlaying blocks of code and opacity customizable

To display a block of lines, things are a bit more complicated: we need another environment in which the counter is not increased. So we end up with two types of delimiters: the first one which let the counter increase (you should use it for the first line of the block) and the second type keeps the previous value such that the current line is displayed in the same overlay of the first line of the block (use this for all other lines of code).

In order to customize the opacity of the lines, a specific key has been introduced:

\pgfkeys{/tikz/.cd,
  set opacity/.initial=0,
  set opacity/.get=\opacity,
  set opacity/.store in=\opacity,
}

applied to:

\tikzset{
  invisible/.style={opacity=\opacity},
  visible on/.style={alt=#1{}{invisible}},
  alt/.code args={<#1>#2#3}{%
    \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
  },
}

Initially, the opacity is set to 0 thus the code appears magically: in the example it is shown how to modify it in order to get some semi transparent effect.

The code:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{listings}

\usetheme{CambridgeUS}

% key to customize the opacity
\pgfkeys{/tikz/.cd,
  set opacity/.initial=0,
  set opacity/.get=\opacity,
  set opacity/.store in=\opacity,
}

% Daniel's code:
% http://tex.stackexchange.com/questions/55806/tikzpicture-in-beamer/55827#55827
\tikzset{
  invisible/.style={opacity=\opacity},
  visible on/.style={alt=#1{}{invisible}},
  alt/.code args={<#1>#2#3}{%
    \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
  },
}

% Daniel's code:
% http://tex.stackexchange.com/questions/15237/highlight-text-in-code-listing-while-also-keeping-syntax-highlighting/49309#49309

\newcounter{step}

\makeatletter
\newenvironment{btHighlight}[1][]
{\begingroup\tikzset{bt@Highlight@par/.style={#1}}\begin{lrbox}{\@tempboxa}}
{\end{lrbox}\bt@HL@box[bt@Highlight@par]{\@tempboxa}\endgroup}

\newcommand\btHL[1][]{%
  \begin{btHighlight}[#1]\bgroup\aftergroup\bt@HL@endenv%
}
\def\bt@HL@endenv{%
  \end{btHighlight}%   
  \egroup
}
\newcommand{\bt@HL@box}[2][]{%
  \tikz[#1]{%
    \pgfpathrectangle{\pgfpoint{0pt}{0pt}}{\pgfpoint{\wd #2}{\ht #2}}%
    \pgfusepath{use as bounding box}%
    \refstepcounter{step}
    \node[visible on=<\thestep->,anchor=base west,rounded corners, fill=none,outer sep=0pt,inner xsep=0.5pt, inner ysep=2pt,  #1]{\usebox{#2}};
  }%
}

\newenvironment{btHighlights}[1][]
{\begingroup\tikzset{bt@Highlights@par/.style={#1}}\begin{lrbox}{\@tempboxa}}
{\end{lrbox}\bt@HLs@box[bt@Highlights@par]{\@tempboxa}\endgroup}

\newcommand\btHLs[1][]{%
  \begin{btHighlights}[#1]\bgroup\aftergroup\bt@HLs@endenv%
}
\def\bt@HLs@endenv{%
  \end{btHighlights}%   
  \egroup
}
\newcommand{\bt@HLs@box}[2][]{%
  \tikz[#1]{%
    \pgfpathrectangle{\pgfpoint{0pt}{0pt}}{\pgfpoint{\wd #2}{\ht #2}}%
    \pgfusepath{use as bounding box}%
    \node[visible on=<\thestep->,anchor=base west,rounded corners, fill=none,outer sep=0pt,inner xsep=0.5pt, inner ysep=2pt,  #1]{\usebox{#2}};
  }%
}
\makeatother

\lstset{language=sh,
        basicstyle=\footnotesize\ttfamily,
        keywordstyle=\footnotesize\color{blue}\ttfamily,
        moredelim=**[is][\btHL]{|}{|}, % counter increased
        moredelim=**[is][\btHLs]{`}{`}, % counter not increased
}


\begin{document}
\begin{frame}[fragile]{Code}

\begin{columns}
\begin{column}{0.45\textwidth}
\setcounter{step}{0}
\begin{lstlisting}

|code 1 line 1|
`code 1 line 2`
`code 1 line 3`

|code 2 line 1|
`code 2 line 2`

|code 3 line 1|
`code 3 line 2`
`code 3 line 3`
`code 3 line 4`
\end{lstlisting}
\end{column}
\begin{column}{0.45\textwidth}
\setcounter{step}{0}
\tikzset{set opacity=0.2}
\begin{lstlisting}
|output 1|
|output 2|
|output 3|
\end{lstlisting}
\end{column}
\end{columns}
\end{frame}
\end{document}

The result:

enter image description here

share|improve this answer
    
Where is it specified in the code to use lstlisting? –  ATOzTOA Mar 2 '13 at 11:10
    
The delimiters are specified in moredelim=**[is][\btHL]{|}{|}, and later on they are used as |code1| and |output 1|. –  Claudio Fiandrino Mar 2 '13 at 11:13
    
What if I want to show a block of code instead of line by line. This puts everything into a single line. –  ATOzTOA Mar 2 '13 at 11:17
    
I have updated my question. –  ATOzTOA Mar 2 '13 at 12:54
    
This is awesome... you are a genius... Can I have the first line of code after a keypress? I tried pause but shows first two blocks on first keypress. –  ATOzTOA Mar 2 '13 at 14:30
show 5 more comments

This is what I did in the end...

\documentclass{beamer}

\usepackage{color}
\usepackage{listings}
\usepackage{setspace}
\usepackage{palatino}
\usepackage{lmodern}


\setbeamertemplate{blocks}[rounded]


\lstset{
    basicstyle=\ttfamily\tiny\setstretch{1},
    aboveskip=-40pt,
    breaklines=true,
    frame=leftline
}


\begin{document}

\begin{frame}[fragile, t]{Quotes}
    \vspace{4mm} \begin{block}{}<2-> \begin{lstlisting}
str1 = "Hello World!"
print str1
    \end{lstlisting} \end{block}

    \vspace{-1.5mm} \begin{block}{}<3-> \begin{lstlisting}
str2 = 'Hello World!'
print str2
    \end{lstlisting} \end{block}

    \vspace{-1.5mm} \begin{block}{}<4-> \begin{lstlisting}
str3 = "Don't do that!"
print str3
    \end{lstlisting} \end{block}

    \vspace{-1.5mm} \begin{block}{}<5-> \begin{lstlisting}
str4 = 'Don\'t do that!'
print str4
    \end{lstlisting} \end{block}

    \vspace{1mm} \begin{block}{}<6-> \begin{lstlisting}
str5 = "This is a long line with double quotes here\", here\" and here\" and single quotes here', here' and here'."
print str5
    \end{lstlisting} \end{block}

    \vspace{1mm} \begin{block}{}<7-> \begin{lstlisting}
str6 = """This is a long line with double quotes here", here" and here" and single quotes here', here' and here'."""
print str6
    \end{lstlisting} \end{block}

    \vspace{1mm} \begin{block}{}<8-> \begin{lstlisting}
str7 = '''This is a long line with double quotes here", here" and here" and single quotes here', here' and here'.'''
print str7
    \end{lstlisting} \end{block}

    \begin{block}{}<2-> \begin{lstlisting}
Hello World!
    \end{lstlisting} \end{block}

    \vspace{-4mm} \begin{block}{}<3-> \begin{lstlisting}
Hello World!
    \end{lstlisting} \end{block}

    \vspace{-4mm} \begin{block}{}<4-> \begin{lstlisting}
Don't do that!
    \end{lstlisting} \end{block}

    \vspace{-4mm} \begin{block}{}<5-> \begin{lstlisting}
Don't do that!
    \end{lstlisting} \end{block}

    \vspace{-1.5mm} \begin{block}{}<6-> \begin{lstlisting}
This is a long line with double quotes here", here" and here" and single quotes here', here' and here'.
    \end{lstlisting} \end{block}

    \vspace{-1.5mm} \begin{block}{}<7-> \begin{lstlisting}
This is a long line with double quotes here", here" and here" and single quotes here', here' and here'.
    \end{lstlisting} \end{block}

    \vspace{-1.5mm} \begin{block}{}<8-> \begin{lstlisting}
This is a long line with double quotes here", here" and here" and single quotes here', here' and here'.
    \end{lstlisting} \end{block}
\end{frame}
\end{document}
share|improve this answer
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.