2

I would love my listings to look like this:

enter image description here

With the references as shown. is this possible? Would I need to use TikZ?

I have done a pretty exhaustive search but can't find out how this was achieved..

I did ask a similar question but had no image reference to hand to articulate exactly how I would like things to look.

Any help much appreciated.

Sean Allred
  • 27,421
Hi Lo
  • 215

1 Answers1

2

I was able to synthesize this answer for referencing lines of listings with this answer making colored, circled numbers. I did have to use TikZ.

Label a line with (*\codelabel{name}*) and reference it with \coderef{name}.

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{tikz}
\lstset{ 
    basicstyle=\ttfamily,
    escapeinside={(*}{*)}, % used to set up \codelabel
    frame=tb
}

\newcommand*{\circled}[1]{\tikz[baseline=(char.base)]{%
            \node[shape=circle,fill=black,draw,inner sep=0.5pt]
            (char) {\footnotesize\textcolor{white}{#1}};}}

\newcounter{codectr}
\newcommand*{\codelabel}[1]{\refstepcounter{codectr}\circled{\arabic{codectr}}
            \label{#1}}
\newcommand*{\coderef}[1]{\circled{\ref{#1}}}

\begin{document}

\begin{lstlisting}[caption={\itshape\texttt{type} and \texttt{attribute}
                   statements}]
attribute file_type; (*\codelabel{ft_attr}*)
attribute domain; (*\codelabel{dom_attr}*)

type system_data_file, file_type, data_file_type; (*\codelabel{sfd_type}*)
type untrusted_app, domain; (*\codelabel{untrusted}*)
\end{lstlisting}

Here, the first~\coderef{ft_attr} and second~\coderef{dom_attr} statements
declare the \verb+file_type+ and \verb+domain+ attributes, and the next
statement~\coderef{sfd_type} declares the \verb+system_data_file+

\end{document}

enter image description here

If you wish to use the hyperref package to make your references into clickable links, this works with that as well.

Arun Debray
  • 7,126
  • 2
  • 30
  • 54