3

I sometimes change something from "\begin{theorem}" to "\begin{lemma}" (I have my own commands: \newtheorem{theorem}{Theorem} and \newtheorem{lemma}{Lemma}).

But if I just write \ref{theorem1}, then I have to manually write "Theorem" before it, which won't change if I turn theorem1 into a lemma.

How do I get this to automatically write "Theorem 1" instead?

moewe
  • 175,683
user56834
  • 463
  • 7
    cleveref's \cref command could be extremely helpful here. – moewe Apr 28 '19 at 16:37
  • See also Cross-reference packages: which to use, which conflict? The \autoref macro (provided by the hyperref package) and especially the \cref and \crefrange macros (provided by the cleveref package) can do exactly what you're looking to accomplish. – Mico Apr 28 '19 at 16:58
  • @Mico maybe close it as a duplicate of the question you linked, or maybe another one like https://tex.stackexchange.com/questions/5767/how-to-get-more-complete-references ? That would also make the question answered. – Marijn May 27 '19 at 15:06
  • @Marijn - Thanks. I also thought about closing this question as a duplicate. However, the earlier question I referenced in my comment is far more general than what's asked in the question; one would have to read through a lot before getting to the part about the cleveref package. Plus, I think that buried within the OP's question is an issue related to how one would cross-reference items that share a common counter; assuming that one wishes to use the cleveref package, the solution lies in specifying the \newtheorem directives after loading cleveref. – Mico May 27 '19 at 16:28
  • As an aside, you shouldn't label something theorem1, because then it could end up being Lemma 2. You should use a semantic label like BernoulliConvergence. That way, the label will still be correct when you rearrange your document (and you'll have a better chance of remembering the label when you're in a different part of the document). – Teepeemm Jun 07 '22 at 14:43

1 Answers1

4

(posted this answer so that the question can be considered to have received an answer)

You should familiarize yourself with the cleveref package and its user commands \cref and \crefrange. The \cref macro (and its start-of-sentence variant, \Cref) can take multiple arguments, which will be the arguments of \label instructions placed elsewhere in the document. The list of arguments needn't be sorted, and the list can contain labels associated with equation, theorem, section, figure, table, etc. objects.

Below is a compilable example that creates one instance each of theorem and lemma envirnonments. Note that cleveref is clever enough to distinguish between the two environments even though they share a common counter. It so happens that cleveref "knows" the names of these two environments (viz., "Theorem" and "Lemma"); if your document featured numbered environments of a type not already known to cleveref, you would need to use \crefname instructions to back-fill the missing name information. E.g., if you have an object called map (with an associated counter variable also called map), then \crefname{map}{map}{maps} informs cleveref how singular and plural forms of the object should be displayed in a cross-reference.

enter image description here

\documentclass{article}
\usepackage{amsthm} % or: \usepackage{ntheorem}
\usepackage[colorlinks,allcolors=blue]{hyperref} % optional
\usepackage[noabbrev,capitalize,nameinlink]{cleveref}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}

\begin{document} \begin{theorem} \label{thm:major} \dots \end{theorem} \begin{lemma} \label{thm:minor} \dots \end{lemma} \noindent Cross-references to \cref{thm:major,thm:minor}. \end{document}

Mico
  • 506,678