4

First, apologies if this is a duplicate: I have genuinely searched for the question, but haven't been able to find it!

I am using the same counter for my theorems, lemmas, propositions, etc. I want to use \autoref (or another package) in the following way: upon calling \autoref{result1}, I get back Theorem 1, but upon calling \autoref{result2} I get Lemma 2. This probably isn't particularly clear, so I've put in a MWE, using autoref and cleveref. In both cases, I always get the word 'Theorem' where I want 'Lemma'.

\documentclass[]{article}
\usepackage[UKenglish]{babel}
\usepackage{hyperref, cleveref}

\newtheorem{thm}     {Theorem}
\newtheorem{lem}[thm]{Lemma}
\def\thmautorefname{Theorem}
\def\lemautorefname{Lemma}

\begin{document}

\begin{thm} \label{T1}  Theorem 1... \end{thm}
\begin{lem} \label{L2}  Lemma 2... \end{lem}

\autoref{T1} \autoref{L2}

\Cref{T1} \Cref{L2}

\end{document}

I want the output to be "Theorem 1 Lemma 2", but at the moment it's "Theorem 1 Theorem 2".

  • For cleveref this works if you use e.g. the amsthm package for your theorems. – Andrew Swann Dec 13 '17 at 13:36
  • If I add the package amsthm to the above preamble, then this doesn't solve my issue... would you care to elaborate? – Sam T Dec 13 '17 at 14:32
  • I have added an example below. – Andrew Swann Dec 13 '17 at 20:30
2

Elaborating on my comment, for cleveref the names in \cref's are correct if you use the amsthm package:

Sample output

\documentclass{article}

\usepackage[UKenglish]{babel}

\usepackage{amsthm,hyperref,cleveref}

\newtheorem{thm}     {Theorem}
\newtheorem{lem}[thm]{Lemma}

\begin{document}

\begin{thm} \label{T1}  Theorem 1\dots \end{thm}
\begin{lem} \label{L2}  Lemma 2\dots \end{lem}

\Cref{T1} \Cref{L2}

\end{document}

You also get correct names if you use the thmtools or the ntheorem package instead of amsthm. Even better the thmtools package also corrects hyperrefs \autoref command:

Second sample

\documentclass{article}

\usepackage[UKenglish]{babel}

\usepackage{thmtools,hyperref,cleveref}

\newtheorem{thm}     {Theorem}
\newtheorem{lem}[thm]{Lemma}

\begin{document}

\begin{thm} \label{T1}  Theorem 1\dots \end{thm}
\begin{lem} \label{L2}  Lemma 2\dots \end{lem}

\autoref{T1} \autoref{L2}

\Cref{T1} \Cref{L2}

\end{document}

When experimenting remember to compile twice to see the effect of a package change, as a first run of latex puts information about these labels in the .aux file, which is then read on the second run for use in the referencing commands.

  • See, that is confusing, because I'm sure that I'd used amsthm before you suggested it, and then again when you suggested it, and neither worked. Yet now I've just tried it on my home computer (instead of office), and it works! \\ I'll try it tomorrow on my office computer, see if there was something I was doing wrong... otherwise this is perfect: Christian's solution took me ages to put in my preamble (needed to do quite a bit for quite a few things -- yours is slicker, I feel... providing I can make it work!) – Sam T Dec 13 '17 at 22:25
  • You need to make sure you run latex twice so the changes to the .aux file on the first run can be used in the second run to generate the references including their names. – Andrew Swann Dec 14 '17 at 9:35
  • That does indeed work for me. I think maybe I was using only \autoref not cleveref. I wish I'd seen your commented before implementing Christian's: I had 10 things (lemma, proposition, claim, ...) that I had to put the stuff in for; took ages =P – Sam T Dec 14 '17 at 9:46
3

Using shared counters for the lemma etc. can be tricky -- neither \cref nor \autoref know which counter is refstepped internally, so both assume thm here.

With the package aliascnt a name alias for an existing counter can be defined, say \newaliascnt{lem}{thm}.

In conjunction with \newtheorem \aliascntresetthe{lem} must be used.

\documentclass[]{article}
\usepackage[UKenglish]{babel}
\usepackage{aliascnt}
\usepackage{hyperref, cleveref}

\newtheorem{thm}{Theorem}
\newaliascnt{lem}{thm}
\newtheorem{lem}[lem]{Lemma}
\aliascntresetthe{lem}
\def\thmautorefname{Theorem}
\def\lemautorefname{Lemma}

\begin{document}

\begin{thm} \label{T1}  Theorem 1... \end{thm}
\begin{lem} \label{L2}  Lemma 2... \end{lem}

\autoref{T1} \autoref{L2}

\Cref{T1} \Cref{L2}

\end{document}

enter image description here

  • That's perfect, thank you :) -- took quite a while to write all that stuff in, but it's done now! – Sam T Dec 13 '17 at 14:30
  • Can I ask, is there a way to use a plural version? For example \Cref{thm1,thm2} outputs Theorems 1 and 2? – Sam T Dec 13 '17 at 14:30
  • Add \crefname{lem}{lemma}{lemmas} \Crefname{Lem}{Lemma}{Lemmas} in order to make the corresponding names available for cleveref. I'm unsure about the plural of lemma in English. In German, it would be Lemmata – user31729 Dec 13 '17 at 18:56
  • Perfect, thanks :) -- "Lemmas" is fine for English :) – Sam T Dec 13 '17 at 22:20
  • Thank you, Christian, for your answer, but I've actually accepted Andrew's now. (I hope that's ok!) Your suggestion, while working perfectly, meant adding quite a lot to my preamble: I had 10 things (lemmas, propositions, claims, etc) that I had to add it to. I feel Andrew's is slicker, so I've accepted it, but you definitely still get my +1! -- I also like learning more about tex stuff, so yours is helpful for that too! – Sam T Dec 14 '17 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.