# A problem with thmtools and cleveref

I am trying to use thmtools and cleveref and I am getting a quite peculiar error. Here's a minimal example.

\documentclass{article}

\usepackage{thmtools}
\usepackage{cleveref}

\declaretheorem[name=Lemma,Refname={Lemma,Lemmas}]{lem}
\declaretheorem[name=Proposition,Refname={Proposition,Proposition}]{prop}
\declaretheorem[name=Theorem,Refname={Theorem,Theorems}]{thr}

\begin{document}

\begin{lem}\label{lem1}
A lemma.
\end{lem}

\begin{prop}\label{prop1}
A proposition.
\end{prop}

\begin{thr}\label{thr1}
A theorem.
\end{thr}

\Cref{lem1}, \Cref{prop1} and \Cref{thr1}.

\end{document}


When I run pdflatex (using a fresh default installation of TeXLive 2011) I receive a message:

LaTeX Warning: Cref  reference format for label type prop' undefined on input line 24.


and the final sentence is rendered in the pdf file as "Lemma 1, ?? 1 and Theorem 1." The weird thing is that if I use only two \declaretheorem definitions, then everything is fine. If I change the order of definitions, then it is always the second one that yields a warning.

Any ideas about what is happening here and how to fix it?

-
Bizarre. When I add a fourth \declaretheorem then I get that the first and last work but the middle two don't. Interchanging the order of loading the packages doesn't change things either. – Loop Space Mar 29 '12 at 12:51

There is a bug in thm-autoref.sty. Part of the set-up for cleverefs magic is left to the start of the document. So cleveref maintains a list of all the label types that it has been told about so that when it gets to the \begin{document} hook, it can iterate through them and finish the necessary configuration.

Examining this list for your document (well, my slightly extended version) reveals that it is:

\thmt@envname ,\thmt@envname ,\thmt@envname ,\thmt@envname ,thr,equation,\@nil


That doesn't look right! \thmt@envname is getting processed four times here. In my code, then \thmt@envname is prop which happens to be the last one in my list of \declaretheorems (for your code it would be thr - I experimented with permutations a bit). So something isn't being expanded when it should. After playing a bit of Ultimate Code Frisbee (chasing definitions from one place to another), I tracked it down to a definition in thm-autoref.sty, specifically the definition of \thmt@refnamewithcomma. It reads:

\def\thmt@refnamewithcomma #1#2#3,#4,#5\@nil{%
\@xa\def\csname\thmt@envname #1utorefname\endcsname{#3}%
\ifcsname #2refname\endcsname
\csname #2refname\endcsname{\thmt@envname}{#3}{#4}%
\fi
}


Clearly, we need to expand the \thmt@envname. One way to do this is with two \expandafters:

\def\thmt@refnamewithcomma #1#2#3,#4,#5\@nil{%
\@xa\def\csname\thmt@envname #1utorefname\endcsname{#3}%
\ifcsname #2refname\endcsname
\csname #2refname\expandafter\endcsname\expandafter{\thmt@envname}{#3}{#4}%
\fi
}


So if you put that at the top of your document (within \makeatletter ... \makeatother) it will fix this problem. Warning The same command is used for autoref, I don't know if this will break that (though it really aught not to do so).

Here's a full example:

\documentclass{article}

\usepackage{cleveref}
\usepackage{thmtools}

\makeatletter
\def\thmt@refnamewithcomma #1#2#3,#4,#5\@nil{%
\@xa\def\csname\thmt@envname #1utorefname\endcsname{#3}%
\ifcsname #2refname\endcsname
\csname #2refname\expandafter\endcsname\expandafter{\thmt@envname}{#3}{#4}%
\fi
}
\makeatother

\declaretheorem[name=Theorem,Refname={Theorem,Theorems}]{thr}
\declaretheorem[name=Lemma,Refname={Lemma,Lemmas}]{lem}
\declaretheorem[name=Corollary,Refname={Corollary,Corollaries}]{cor}
\declaretheorem[name=Proposition,Refname={Proposition,Proposition}]{prop}
\begin{document}

\begin{lem}\label{lem1}
A lemma.
\end{lem}

\begin{prop}\label{prop1}
A proposition.
\end{prop}

\begin{thr}\label{thr1}
A theorem.
\end{thr}

\begin{cor}\label{cor1}
A Corollary.
\end{cor}

\Cref{lem1}, \Cref{prop1}, \Cref{thr1}, and \Cref{cor1}.

\end{document}


Result:

(Oh - something doesn't like standalone. Oh dear, back to more traditional tools.)

-
Ultimate code frisbee. I like this! – Seamus Mar 29 '12 at 13:41
+1 great answer- would it be appropriate to share more details of "The ultimate frisbee" for future reference? I'd love to see them! – cmhughes Mar 29 '12 at 17:18
@cmhughes It's far more boring than it sounds. It was a case of tracing definitions and expansions. It felt a bit like golf: hit the ball (expand the macro) and see where it lands, then rinse and repeat. But "code golf" is already taken, so I thought of a game of frisbee with non-expert players: throw frisbee, go pick up frisbee, throw frisbee again, go pick up frisbee again ... – Loop Space Mar 29 '12 at 18:11
This is way above my level of understanding of LaTeX, but if I got it right you have found a bug in thmtools and proposed a fix. Should this be reported to the package's author? – Karol Szumiło Mar 31 '12 at 4:47

The first presented bugfix works for me. I have written to Toby Cubitt, the maintainer of the cleveref package. He agrees, that it is a bug, claims it,however, to be a bug in the thmtools package, which is why I reported the issue to Ulrich M. Schwarz, the maintainer of thmtools, providing the link to this thread. We'll see if there is an update for the thmtools package soon =). In his mail, Toby criticized that way too often people posted bugs and problems in forums like this one (which is great), but seldomly reported bugs directly to the developer him-/herself. So please everyone, if you think you found a bug and you are pretty confident about it, report it to the developer.

Cheers everyone I'll keep you posted.

Hey everyone. Ulrich Schwarz answered. He told me to change line 48 in thm-autoref.sty so that it reads

\csname #2refname\@xa\endcsname\@xa{\thmt@envname}{#3}{#4}%


(two aditional \@xa)

Don't forget to update your TeX database running texhash or mktexlsr or texconfig[-sys] rehash in a terminal, or the change won't be effective. (If you don't know where to find thm-autoref.sty, run locate thm-autoref.sty in a terminal. If you use Windows, I don't know how you update your TeX database, sorry). cheers, hope it helps. David

-

Not a proper fix, but a workaround, largely taken from this Google search:

\crefname{thr}{theorem}{theorems}
\Crefname{thr}{Theorem}{Theorems}


for each of your interior theorem types, after all your \declaretheorem commands. And totally unrelated to the original question, but you may want to use the \cref command for lowercase references when you're in the middle of a sentence. The cleveref manual uses that notation, at least.

\documentclass{article}

\usepackage{thmtools}
\usepackage{cleveref}

\declaretheorem[name=Lemma,Refname={Lemma,Lemmas}]{lem}
\declaretheorem[name=Theorem,Refname={Theorem,Theorems}]{thr}
\declaretheorem[name=Thing,Refname={Thing,Things}]{thing}
\declaretheorem[name=Proposition,Refname={Proposition,Propositions}]{prop}
\crefname{thr}{theorem}{theorems}
\Crefname{thr}{Theorem}{Theorems}
\crefname{thing}{thing}{things}
\Crefname{thing}{Thing}{Things}

\begin{document}

\begin{lem}\label{lem1}
A lemma.
\end{lem}

\begin{thr}\label{thr1}
A theorem.
\end{thr}

\begin{prop}\label{prop1}
A proposition.
\end{prop}

\begin{thing}\label{thing1}
A thing.
\end{thing}

\Cref{lem1}, \cref{prop1}, \cref{thing1} and \cref{thr1}.

\end{document}


This isn't fixed with the latest version of cleveref, but I'm not entirely up to date on all my other packages. Will see if this persists with today's packages, and will wait on a better answer or acknowledgment of a real bug in cleveref.

-
Thanks, this at least solves the most immediate problem. (I thought I tried this workaround at some point, but I must remember incorrectly.) As to the capitalization, I think it is a common practice in mathematical writing to capitalize the names of numbered theorems, lemmas etc. At least people at MO agree. – Karol Szumiło Mar 29 '12 at 13:32
Bug still exists in today's packages, but Andrew has a more proper fix. As for your version of the workaround, I found it didn't work at all unless the \crefname and \Crefname commands came after all the theorem declarations. Putting them right after the interior declarations didn't make any difference. – Mike Renfro Mar 29 '12 at 13:40

An immediate fix to the problem with three cross-references is to replace {prop} with {proposition} throughout your document. I have no explanation for why this fix seems to work.

Please also see @AndrewStacey's detailed answer in which he (i) reports that he's found a bug in the file thm-autoref.sty (part of the thmtools bundle) and (ii) provides a patch for that bug.

However, given that your MWE does not seem to employ the thmtools package's capabilities other than for specifying the singular and plural forms of the respective environments, you might as well just use the stable and well-debugged ntheorem package instead of thmtools. If needed, you can give instructions to cleveref for handling plural forms of the environments (as I do in the MWE below).

\documentclass{article}
\usepackage{ntheorem}
\newtheorem{lem}{Lemma}
\newtheorem{prop}{Proposition} % NOTE: no need to write {proposition}
\newtheorem{thr}{Theorem}

\usepackage{cleveref}
\crefname{lem}{Lemmas}{Lemmas}
\crefname{prop}{Proposition}{Propositions}
\crefname{thr}{Theorem}{Theorems}

\begin{document}
\begin{lem}\label{lem1} A lemma. \end{lem}
\begin{prop}\label{prop1} A proposition. \end{prop}
\begin{lem}\label{lem2} Another lemma. \end{lem}
\begin{prop}\label{prop2} Another proposition. \end{prop}
\begin{thr}\label{thr1} A theorem. \end{thr}

As we stated in \cref{lem1,prop1,thr1,prop2,lem2}, \ldots
\end{document}


-
My example doesn't use too many things from thmtools but my proper code does. I will take a look at ntheorem to see whether it has everything I need. – Karol Szumiło Mar 29 '12 at 14:36
Proposition works because that's already on the list that cleveref processes so the fact that it doesn't get put on there by the definetheorem call doesn't matter. – Loop Space Mar 29 '12 at 16:28
@AndrewStacey: Maybe I should have phrased my statement as follows: What's baffling about this case isn't so much that replacing {prop} with {proposition} works for cleveref but, rather, that {lem} and {thr} work even though they aren't either on the list that cleveref already knows about. – Mico Mar 29 '12 at 16:46
Ah, I see. The last one always works because it is what the macro \thmt@envname` is currently set to. I'm not sure about the first one, but it might be that the way that the stack is built then the first time something is added to it then it is expanded. I'd have to play some more frisbee to know for sure. – Loop Space Mar 29 '12 at 18:09