Skip to content

Instantly share code, notes, and snippets.

@willtim
Created June 12, 2014 06:23
Show Gist options
  • Save willtim/371e5243d01ce1660427 to your computer and use it in GitHub Desktop.
Save willtim/371e5243d01ce1660427 to your computer and use it in GitHub Desktop.
Source annotation example in Latex
\documentclass{beamer}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{fit,calc,shadows}
% Define styles for balloons and lines
\tikzstyle{line} = [draw, rounded corners=3pt, -latex]
\tikzstyle{balloon} = [draw, fill=blue!20, opacity=0.4, inner sep=4pt, rounded corners=2pt]
\tikzstyle{comment} = [fill=white!70, text=black, text width=3cm, minimum height=1cm, align=center, font=\normalsize]
% Command to place a TikZ anchor at the current position
\newcommand{\tikzmark}[1]{%
\tikz[overlay,remember picture,baseline] \coordinate (#1) at (0,0) {};}
% Command to draw a balloon over two anchors
\newcommand<>{\balloon}[3]{%
\coordinate (c) at ($(#2)+(0,1ex)$);
\node#4 (#1) [balloon, fit=(#3) (c)] {};}
\begin{document}
\begin{frame}[fragile]{Foo!}
\begin{lstlisting}[language=c,escapechar=\$]
$\tikzmark{sw1}$#include <stdio.h>$\tikzmark{ne1}$
int main( $\tikzmark{sw2}$void$\tikzmark{ne2}$ )
{
printf("hello, world\n");$\tikzmark{ne3}$
$\tikzmark{sw3}$return 0;
}
\end{lstlisting}
\begin{tikzpicture}[overlay,remember picture]
\balloon<1>{balloon1}{ne1}{sw1};
\balloon<1>{balloon2}{ne2}{sw2};
\balloon<1>{balloon3}{ne3}{sw3};
\node (comment1) [comment] at (\textwidth-3.5cm, .5\textheight+1cm) {%
\only<1>{Include headers}%
};
\node (comment2) [comment] at (\textwidth-3.5cm, .5\textheight+0cm) {%
\only<1>{Parameter}%
};
\node (comment3) [comment] at (\textwidth-1.5cm, .5\textheight-1cm) {%
\only<1>{Do stuff}%
};
\draw<1-1> [line] (comment1.west) -| (balloon1.north);
\draw<1-1> [line] (comment2.south) |- (balloon2.east);
\draw<1-1> [line] (comment3.south) |- (balloon3.east);
\end{tikzpicture}
\end{frame}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment