cleveref fails for theorem environments sharing the same counter
Asked Answered
V

4

15

I want to number all theorems and corollaries on the same (section) counter. But when I do this, cleveref names them both 'theorem'. Here is a minimal example:

\documentclass{amsart}

\usepackage{amsthm, cleveref}
%\crefname{theorem}{theorem}{theorems}
%\crefname{corollary}{corollary}{corollaries}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{corollary}[theorem]{Corollary}

\begin{document}
\section{Section title}

\begin{theorem}\label{thm:test}
Here is the theorem.
\end{theorem}

\begin{corollary}\label{cor:test}
Here is the corollary.
\end{corollary}

The theorem reference is given by \cref{thm:test} and the corollary reference is given by \cref{cor:test}.

\end{document}

Here, the corollary reference is 'theorem 1.2'. This problem persists even when explicitly stating the crefname.

Any suggestions?

Vingtetun answered 27/6, 2011 at 22:7 Comment(0)
F
8

You need to load either the ntheorem or the amsthm package if you want cleveref to distinguish different theorem-like environments that use the same counter. Those packages store additional information about theorem-like environments that cleveref makes use of. Note that they must be loaded before cleveref.

Using ntheorem (which has been hyperref-compatible for a long time now via the hyperref option) or amsthm (also hyperref-compatible) together with cleveref is a much better solution than using an optional argument to \label. And there's no need to fall back to \thref either. If you're using ntheorem anyway, then \cref will do everything that \thref does, and more (multi-references, customised formatting, etc.). In fact, cleveref redefines \thref to be an alias for \cref when ntheorem is loaded with the thref option.

(There is a trick - also described in the cleveref manual - that uses the aliascnt package to distinguish theorem-like environments without ntheorem or amsthm. But it takes more work, and frankly why bother when a simple \usepackage{ntheorem} or \usepackage{amsthm} will do the job perfectly?)

Freeforall answered 4/1, 2012 at 11:32 Comment(1)
As you note, amsthm must be loaded before cleveref. Furthermore, it appears that the \newtheorem commands must appear after cleveref is loaded.Theft
V
3

Ok, here's a workaround.

Specify the name to be displayed in the optional label argument, i.e:

\begin{corollary}\label[corollary]{cor:test}
Here is the corollary.
\end{corollary}

This gives the desired output whilst keeping the environment name in the same place (albeit written twice).

Vingtetun answered 5/8, 2011 at 20:39 Comment(0)
J
1

I don't see anything wrong with this. I have run your script both with

%\crefname{theorem}{theorem}{theorems}
%\crefname{corollary}{corollary}{corollaries}

commented and uncommented, and I get what is expected

'The theorem reference is given by theorem 1.1 and the corollary reference is
given by corollary 1.2.'

Prehaps you have an outdated package.

Also make sure that you load cleveref AFTER amsthm (and hyperref), if your using the article class, as this will cause the error that you saw

Janellajanelle answered 20/7, 2011 at 12:35 Comment(0)
M
0

Since you've declared the two kinds of theorems to use the same counter, cleveref can't distinguish them. Consider using the ntheorem package instead.

Macnair answered 30/6, 2011 at 3:51 Comment(1)
I actually need it to work with hyperref and ntheorem does not work with hyperref. There is an option which apparently fixes the compatability problem but I cannot get it to work. I changed to the article document class (because ntheorem is not compatible with amsart), used \usepackage[thref, amsthm, hyperref]{ntheorem} and \usepackage{hyperref}, referred with \thref{thm:test} and \thref{cor:test}, which gave the the output 1.1Section titletheorem.1.1 and 1.2Section titletheorem.1.2.Vingtetun

© 2022 - 2024 — McMap. All rights reserved.