# How to make annotation to code listings

Is it possible to make annotations to source codes like below?

• Welcome to TeX.SE! Please show us the tex code you have so far! – Mensch Jun 9 at 19:48

You want two things: (1) add footnotes to the listing, and (2) change the way footnotes look. Lets us do both.

The package fancyvrb allows to add arbitrary TeX commands to a verbatim text. So

\begin{Verbatim}[\commandchars=\\\{\}]
text\footnote{this is a footnote}
\end{Verbatim}


will add a footnote to the verbatim snippet. Since we want the footnotes to be close to the listing, let us put the snippet in the minipage, then footnotes automatically become minipage footnotes. Now, you want footnote mark to be a dingbat, and to typeset footnote marks flush right. The first is achieved by

\makeatletter
\renewcommand{\thempfootnote}{\ding{\numexpr(181+\c@mpfootnote)}}
\makeatother


the second by defining a special command

\newcommand{\commentcode}[1]{\hfill\footnote{#1}}


All together:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{pifont}
\makeatletter
\renewcommand{\thempfootnote}{\ding{\numexpr(181+\c@mpfootnote)}}
\makeatother
\newcommand{\commentcode}[1]{\hfill\footnote{#1}}
\begin{document}
\thispagestyle{empty}
\begin{minipage}{\textwidth}
\begin{Verbatim}[commandchars=\\\{\}]
tar xzvf \commentcode{Unzip the archive}
cd gsl-1.16
./configure
make
sudo make install
\end{Verbatim}
\end{minipage}
\end{document}


Result:

• Thanks @Boris. This is it. I have one more question is it possible to add hyperlink from listing line to footnote and from footnote to the line of listing? – k3be Jun 10 at 6:49
• \usepackage{hyperref} will give you links from the code to comments – Boris Jun 10 at 14:28