\label-\ref consecutively numbered theorems using varioref

As far as I can see varioref does not know that there's a different theorem in action -- all use the shared counter named definition, \refstepcounter increases the definition counter, leaving \@currentlabel to be definition, which leads to the 'wrong' label format then.

I am applying \DeclareCoupledCounters from xassoccnt package to get coupled counters, which is similar to the concept of shared counters but allows separate labels for each of them and thereby giving varioref the opportunity to grab the correct labelformat then. Caveat here: There are four counters now...

As an alternative solution cleveref can be used, by defining crefname for individual entities, using \cref{...} instead of \ref and overwritting the label type with \label[lemma]{l1} for example, but this can get tedious for a lot of definitions/theorems etc.

\documentclass[a4paper]{article}

\usepackage{xassoccnt}

\newtheorem{definition}{definition}
\newtheorem{theorem}{theorem}
\newtheorem{lemma}{lemma}
\newtheorem{corollary}{corollary}

\DeclareCoupledCounters{definition,lemma,theorem,corollary}  

\usepackage{varioref}

\labelformat{definition}{definition~#1}
\labelformat{theorem}{theorem~#1}
\labelformat{lemma}{lemma~#1}
\labelformat{corollary}{corollary~#1}

\begin{document}

\begin{definition}\label{d1}
$i^2=-1$.
\end{definition}

\begin{theorem}\label{t2}
$e^{i\theta}=\cos\theta+i\sin\theta$.
\end{theorem}

\begin{lemma}\label{l3}
$a+b=b+a$.
\end{lemma}

\begin{theorem}\label{t4}
$a(b+c)=ab+ac$.
\end{theorem}

\begin{corollary}\label{c5}
$e^{i\pi}+1=0$.
\end{corollary}

We use \ref{d1} and \ref{l3} get \ref{t2}, \ref{t4}, \ref{c5}.
\end{document}

Alternate version with cleveref

\documentclass[a4paper]{article}


\newtheorem{definition}{definition}
\newtheorem{theorem}[definition]{theorem}
\newtheorem{lemma}[definition]{lemma}
\newtheorem{corollary}[definition]{corollary}

\usepackage{cleveref}

% Not really necessary, since cleveref defines them already, but for demonstration only 
%\crefname{definition}{definition}{definitions}
%\Crefname{definition}{Definition}{Definitions}  % for upper case 
%\crefname{lemma}{lemma}{lemmata}
%\Crefname{lemma}{Lemma}{Lemmata}
%\crefname{theorem}{theorem}{theorems}
%Crefname{theorem}{Theorem}{Theorems}
%\crefname{corollary}{corollary}{corollaries}
%\Crefname{corollary}{Corollary}{Corollaries}


\begin{document}

\begin{definition}\label{d1}
$i^2=-1$.
\end{definition}

\begin{theorem}\label[theorem]{t2}
$e^{i\theta}=\cos\theta+i\sin\theta$.
\end{theorem}

\begin{lemma}\label[lemma]{l3}
$a+b=b+a$.
\end{lemma}

\begin{theorem}\label[theorem]{t4}
$a(b+c)=ab+ac$.
\end{theorem}

\begin{corollary}\label[corollary]{c5}
$e^{i\pi}+1=0$.
\end{corollary}

We use \cref{d1} and \cref{l3} get \cref{t2}, \cref{t4}, \cref{c5}.
\end{document}

enter image description here


I would like to suggest that you use the power of the cleveref package. This package provides a macro called \cref, which not only handles the label-assigning job but can even take multiple arguments. (To ease the job of determining which label should be used with which theorem-like environment, I further recommend you load either the ntheorem or the amsthm package.)

enter image description here

\documentclass[a4paper]{article}
\usepackage{ntheorem,varioref,cleveref}

\newtheorem{definition}{definition}
\newtheorem{theorem}[definition]{theorem}
\newtheorem{lemma}[definition]{lemma}
\newtheorem{corollary}[definition]{corollary}

\begin{document}

\begin{definition}\label{d1} $i^2=-1$. \end{definition}    
\begin{theorem}\label{t2} $e^{i\theta}=\cos\theta+i\sin\theta$. \end{theorem}
\begin{lemma}\label{l3} $a+b=b+a$. \end{lemma}
\begin{theorem}\label{t4} $a(b+c)=ab+ac$. \end{theorem}
\begin{corollary}\label{c5} $e^{i\pi}+1=0$. \end{corollary}

\noindent
We use \cref{d1,l3} and get \cref{t2,t4,c5}.
\end{document}