# Give code fragments a numbering (like equation-numbers, not line-numbers)

Can I give my code fragments in minted a numbering, like with equation numbers, so they'd have a (1) etc. at the right side of them so they can be referred to? (E.g. to write something like "Combine code fragment (1) and (2) to get xyz..."). And also so that other people can reference them. I don't want to create a full floating listing, I would like to do it within the text-flow, like with equations.

Here's what I mean with some code:

Some text... blah.
\begin{minted}{cpp}
int a=0;
int b=5;
\end{minted} % => I want this code fragment to have a number on the right side, like equations do.
Now here (or later in the text), I would like to refer to above code fragment.

Like this with math:

$$a=0 % this will get a numbering, i.e. "(1)" on the right side \label{myeq}$$
As we can see in Eq. \ref{myeq}, ...blah.

So the text will look like "As we can see in Eq. 1, ...blah."

But that syntax doesn't work with minted.

I would prefer to do this in minted and not e.g. switch to listings.

• Please give us an MWE of what you have. Compilable and as short as possible. Are you bound to minted or would answers for listings or pseudo code be ok as well? You should also tell us, how you want to cite and how the citation should look like in your running text. – LaRiFaRi Jun 10 '15 at 13:28
• @LaRiFaRi I added a MWE, but I think it's a bit pointless tbo - that's why I didn't add one in the first place. If it isn't what you meant, I'm happy to improve it! :-) – Ela782 Jun 10 '15 at 21:05
• See the numberedblock package. e.g., tex.stackexchange.com/questions/202966/how-can-i-show-codeboxes/… – Steven B. Segletes Jun 10 '15 at 21:12
• That is not a minimum example, it's a snippet. A minimum example is a complete, compilable document. A t least the documentclass needs to be known – MaxNoe Jun 10 '15 at 21:16
• @MaxNoe Okay, it's not a MWE - it's a "ME", minimum example required to understand and reproduce the question. The documentclass is irrelevant to my question - the one I use in this case is not in a standard latex distribution, but the question can be reproduced with any class. – Ela782 Jun 11 '15 at 21:36

You can do it with tcolorbox. Customize the frame as you like.

\documentclass{article}
\usepackage[many]{tcolorbox}
\tcbuselibrary{minted}

\NewTCBListing[auto counter]{codefragment}{mo}{%
before skip=\topsep,
after skip=\topsep,
colback=red!5!white,colframe=red!75!black,
comment={\hfill(\thetcbcounter)\IfValueT{#2}{\listinglabel{#2}}},
listing outside comment,
righthand width=2.5em,
sidebyside gap=0pt,
minted language=#1,
}
\makeatletter
\newcommand{\listinglabel}[1]{%
\edef\@currentlabel{\thetcbcounter}\label{#1}%
}
\makeatother

\begin{document}

Some text... blah. Some text... blah. Some text... blah.
Some text... blah. Some text... blah. Some text... blah.
Some text... blah.
\begin{codefragment}{cpp}[frag-A]
int a=0;
int b=5;
\end{codefragment}
Now here (or later in the text), I would like to refer to above code fragment
and I can:~\eqref{frag-A}.

Some text... blah. Some text... blah. Some text... blah.
Some text... blah. Some text... blah. Some text... blah.
Some text... blah.
\begin{codefragment}{cpp}[frag-B]
int a=0;
int b=5;
\end{codefragment}
Now here (or later in the text), I would like to refer to above code fragment
and I can:~\eqref{frag-B}.

\end{document}