Skip to content

Instantly share code, notes, and snippets.

@lenary
Last active June 15, 2017 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lenary/284e43551f70877ffff9a99d87a0370c to your computer and use it in GitHub Desktop.
Save lenary/284e43551f70877ffff9a99d87a0370c to your computer and use it in GitHub Desktop.
%% Source Code Notes
\ProvidesPackage{listing-label}
% We use tikz for drawing the circles around the letters. Dumb, probably.
\RequirePackage{tikz}
% Counter, numbered by section
\newcounter{lstlistinglabel}[section]
% Length for left out-dent
\newlength{\lstlabelleftoutdent}
% Use this in listing, to label something right there. You'll need a way to
% escape into normal latex in listings, I use \lstset{escapechar=@} so
% this command goes in @\lstlabel{...}@. You'll probably also want
% \lstset{flexiblecolumns,keepspaces} because it looks nicer.
\newcommand{\lstlabel}[1]{\refstepcounter{lstlistinglabel}\label{#1}%
\lstlabeldecoration{\thelstlistinglabel}}
% Use this in listing (again once escaped to latex), to label
% something in the left margin (must be at start of a line, takes up 0
% horizontal space in the listing line)
\newcommand{\lstleftlabel}[1]{\refstepcounter{lstlistinglabel}\label{#1}%
\makebox[0pt]{\lstlabeldecoration{\thelstlistinglabel}\hspace{\lstlabelleftoutdent}}}
% Use this in text to refer to \lstlabel. If you use `hyperref` they
% get linked together!
\newcommand{\lstref}[1]{\lstlabeldecoration{\ref{#1}}}
% CONFIG OPTIONS
% Change these, the stuff above should be modular.
% Use letters, because we might have a lot of them
\renewcommand{\thelstlistinglabel}{\Alph{lstlistinglabel}}
% Put a circle around the letter, of course. You can customize this.
\newcommand*\lstlabeldecoration[1]{%
\tikz[baseline=(char.base)]{\node[shape=circle,draw,inner sep=1pt,minimum size=1.05em](char){\scriptsize\sffamily#1};}}
% Customize the out-dent length here
\setlength{\lstlabelleftoutdent}{2em}
% % If you want the label numbers to be per-listing, then uncomment the
% % following code. They are currently per-section.
% \AtBeginDocument{
% \makeatletter
% \@addtoreset{lstlistinglabel}{lstlisting}
% \makeatother
% }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment