# Getting a proper reference number with \ref while using custom counter and environment

I am trying to make a reference-able environment with its own counter, that I later refer to in text. I want the counter of my environment to have the style Chapter.Number . I have been able to make it work within a new environment and a new counter with this code (MWE in the end of the question):

\newcounter{code}[chapter]

\definecolor{bg}{rgb}{0.975,0.95,1.0}

\newenvironment{code}[3][hbp]
{\VerbatimEnvironment \refstepcounter{code}
\begin{listing}[#1]
\label{#3}
\textbf{Code~\arabic{chapter}.\arabic{code}} #2
\centering
\begin{minted}[bgcolor =bg, frame=lines,framesep=2mm,linenos]{julia}}
{\end{minted}\end{listing}}


(notice the maybe incorrect use of \label)

In my document I use the environment like so:

It is possible to compute this histogram with a naive approach.
This is shown in Code~\ref{code1}.
A much smarter approach is instead presented in Code~\ref{code2}.

\begin{code}{Naive calculation}{code1}
function naivehist(data, ε)
Δ += sin(ε) # lalala
m = rand(1000,1000)
end
\end{code}


Everything seems to work nicely, except when referencing the code blocks:

I suspect that my usage of \label inside my custom environment is probably incorrect. What is the way to do this, so that when using \ref{...} I would correctly get the number 2.1 instead of 1?

MWE

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{minted} % for making code snippets
\usepackage{amsmath}

\begin{document}
\chapter{Title of chapter.}

\newcounter{code}[chapter]

\definecolor{bg}{rgb}{0.975,0.95,1.0}

\newenvironment{code}[3][hbp]
{\VerbatimEnvironment \refstepcounter{code}
\begin{listing}[#1]
\label{#3}
\textbf{Code~\arabic{chapter}.\arabic{code}} #2
\centering
\begin{minted}[bgcolor =bg, frame=lines,framesep=2mm,linenos]{julia}}
{\end{minted}\end{listing}}

the histogram equation:
$$\label{eq1} p = \sum p_i + \int_0^1 \omega d\epsilon$$
It is possible to compute this histogram with a naive approach. This is shown in Code~\ref{code1}.

\begin{code}{Naive, memory-inefficient calculation of eq.~\eqref{eq1}.}{code1}
function naivehist(data, a)
x += sin(a) # lalala
m = rand(1000,1000)
end
\end{code}

\end{document}

• Shell escapes (minted) run as separate programs. Even a savebox won't fix it because the aux file is not written to until the box is expanded. Oct 18, 2019 at 16:02

Redefine \thecode to give the correct output:

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{minted} % for making code snippets
\usepackage{amsmath}

\begin{document}
\chapter{Title of chapter.}

\newcounter{code}[chapter]
\renewcommand\thecode{\thechapter.\arabic{code}}
\definecolor{bg}{rgb}{0.975,0.95,1.0}

\newenvironment{code}[3][hbp]
{\VerbatimEnvironment \refstepcounter{code}\label{#3}%
\begin{listing}[#1]
\textbf{Code~\thecode} #2
\centering
\begin{minted}[bgcolor =bg, frame=lines,framesep=2mm,linenos]{julia}}
{\end{minted}\end{listing}}

the histogram equation:
$$\label{eq1} p = \sum p_i + \int_0^1 \omega d\epsilon$$
It is possible to compute this histogram with a naive approach. This is shown in Code~\ref{code1}.

\begin{code}{Naive, memory-inefficient calculation of eq.~\eqref{eq1}.}{code1}
function naivehist(data, a)
x += sin(a) # lalala
m = rand(1000,1000)
end
\end{code}

\end{document}


• Thanks, this seems to work for referencing within the same chapter. But If I am in e.g. chapter 2 and want to reference code 1.2 from chapter 1, using your approach will actually output 2.2 as the reference instead of 1.2. Oct 21, 2019 at 9:34
• I can't reproduce this. Are you sure that you used exactly my code? Oct 21, 2019 at 9:50
• You are correct, this was a problem on my side. Sorry, and thanks for the correct reply! Oct 21, 2019 at 10:41