# \label-\ref consecutively numbered theorems using varioref

I hope all definitions, lemmas, theorems, propositions and corollaries are numbered consecutively. The code as below. My trouble is: when I reference, like \ref{<lemma 4>}, get 'definition 4'.

\documentclass[a4paper]{article}
\usepackage{varioref}

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

\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}


What could I have done wrong?

• varioref does not care about definition being a shared counter. In each case definition is increased by \refstepcounter and thereby the labels are always using definition. – user31729 Apr 17 '16 at 6:00

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}


• Don't forget that \cref can take multiple arguments, letting you write something such as We use \cref{d1,l3} and get \cref{t2,t4,c5}. – Mico Apr 17 '16 at 6:28
• @Mico: Well, I never forget this (oh, my nose grows ;-)) – user31729 Apr 17 '16 at 6:30
• Thank you for your efficient answer! Also to thank the answer @Mico has been deleted. – poorich Apr 17 '16 at 6:35
• @Mico: Please undelete your answer -- it uses cleveref too, of course, but in another way – user31729 Apr 17 '16 at 6:36

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.)

\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}