How do I reference a custom counter that shows the section number?

You can manually set the label value by defining \@currentlabel at the start of the proof environment.

Adding something to the start of a command can be done using the xpatch package which provides the \xpretocmd (xpatch pre to command) macro. This works the same for environments, an environment like proof defines two commands, one that is executed with \begin{proof} which is internally called \proof, and one that is executed with \end{proof} which is internally called \endproof.

Patching the environment allows you to use the normal \label command instead of a newly defined labelling command.

Because \@currentlabel has an @ symbol in the name the redefinition should be enclosed in \makeatletter and \makeatother.

MWE:

\documentclass[12pt,letterpaper]{article}

\usepackage{amsthm}
\usepackage{xpatch}
\usepackage{hyperref}
\makeatletter
\xpretocmd{\proof}{\def\@currentlabel{\theproof}}{}{}
\makeatother

\newcounter{proof}[section] % adds a new counter for the proof environment included in the amsthm package that restarts for every new section
\renewcommand{\theproof}{\thesection.\arabic{proof}} % adds the section number before your proof counters
\renewcommand{\proofname}{\refstepcounter{proof}Proof \theproof} % the \proofname was embedded in your proof environment to output the italicized 'Proof', which is displayed at the beginning of every proof. This changes the \proofname to print 'Proof' and \theproof, which we defined as the section-proof counter above. This adds a counter to your proofs so you can hyperref them. 

\newtheorem{definition}{Definition}[section]

\begin{document}
\section{One}
\begin{proof}
\label{p1}
this is proof one.
\end{proof}

This should reference proof \ref{p1}, which should display as proof 1.1.
\begin{definition}
\label{d1}
this is definition one.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d1}.

\section{Two}
\begin{proof}
\label{p2}
this is proof two.
\end{proof}
This should reference proof \ref{p2}, which should display as proof 2.1.

\begin{definition}
\label{d2}
this is definition two.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d2}.

\end{document}

Result:

enter image description here


Here is an approach that works, but I am not sure what is going on behind the scenes. I adapted the accepted answer from this question, though copying it verbatim increased the counter proof once when the environment began, and once when the label was defined, resulting in offset numbering, so I just decreased the counter every time the label is called.

After you define the counter proof, add the the following line:

\newcommand{\prooflabel}[1]{\addtocounter{proof}{-1}\refstepcounter{proof}\label{#1}}

Then whenever you have a proof, instead of \label, use \prooflabel as below.

\section{One}
\begin{proof}
\prooflabel{p1}
this is proof one.
\end{proof}

This should reference proof \ref{p1}, which should display as proof 1.1.

This is the result:

enter image description here

Perhaps someone more knowledgeable than me can adjust the \prooflabel and other definitions so that there isn't this back and forth couting, but it seems to work as you would like it to work.


Your \refstepcounter is issued inside the optional argument to \item and the corresponding value of \@currentlabel gets forgotten as soon as \item is processed, so you actually get references to the section number. You can check it by clicking on the reference.

A possible solution is to patch \proof (the command executed upon entering the proof environment) to issue \refstepcounter after \trivlist and before \item.

\documentclass[12pt,letterpaper]{article}

\usepackage{amsthm}
\usepackage{xpatch}
\usepackage{hyperref}

\newcounter{proof}[section]
\renewcommand{\theproof}{\thesection.\arabic{proof}}
\renewcommand{\proofname}{Proof \theproof}

\xpatchcmd{\proof}{\trivlist}{\trivlist\refstepcounter{proof}}{}{}

\newtheorem{definition}{Definition}[section]

\begin{document}
\section{One}
\begin{proof}
\label{p1}
this is proof one.
\end{proof}

This should reference proof \ref{p1}, which should display as proof 1.1.
\begin{definition}
\label{d1}
this is definition one.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d1}.

\section{Two}
\begin{proof}
\label{p2}
this is proof two.
\end{proof}
This should reference proof \ref{p2}, which should display as proof 2.1.

\begin{definition}
\label{d2}
this is definition two.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d2}.

\end{document}

enter image description here

You can also define a proof* environment for unnumbered proofs by saving a copy of \proof before patching it.

\documentclass[12pt,letterpaper]{article}

\usepackage{amsthm}
\usepackage{xpatch}
\usepackage{hyperref}

% save \proof
\let\unnumberedproof\proof
\newenvironment{proof*}
 {\renewcommand\proofname{Proof}\unnumberedproof}
 {\endproof}

\newcounter{proof}[section]
\renewcommand{\theproof}{\thesection.\arabic{proof}}
\renewcommand{\proofname}{Proof \theproof}

\xpatchcmd{\proof}{\trivlist}{\trivlist\refstepcounter{proof}}{}{}

\newtheorem{definition}{Definition}[section]

\begin{document}
\section{One}
\begin{proof}
\label{p1}
this is proof one.
\end{proof}

This should reference proof \ref{p1}, which should display as proof 1.1.
\begin{definition}
\label{d1}
this is definition one.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d1}.

\section{Two}
\begin{proof}
\label{p2}
this is proof two.
\end{proof}
This should reference proof \ref{p2}, which should display as proof 2.1.

\begin{definition}
\label{d2}
this is definition two.
\end{definition}
As we can see, this is not a problem when I reference definition \ref{d2}.

\begin{proof*}
An unnumbered proof.
\end{proof*}

\end{document}