As I said in the comment, the issue is that cleveref
's machinery is not expandable (the \@ifnextchar
that hyperref
complains about, for example, isn't expandable), so it can't be turned into a PDF string.
The crossreftools
package provides an expandable alternative for cross referencing commands. For cleverref
you can replace \cref
by \crtcref
and \Cref
by \crtCref
.
However, as you noted in your comment, only using that breaks badly. The reason is that cleverref
redefines some internals of the theorems and inserts some \MakeUppercase
and \MakeLowercase
(which aren't expandable). The issue boils down to the fact that these commands end up in the cross reference code, which is expected to contain only text. When hyperref
tries to expand that, chaos ensues.
I tried to find a proper fix, but most of them led to a can of worms of code which would then be incompatible with everything. An easier way out, which preserves cleveref
's functionality and leaves the cross reference text expandable, is to use expl3
's case changing functions. I used etoolbox
's \patchcmd
to replace the two occurrences of \MakeUppercase
and one of \MakeLowercase
by \text_uppercase:n
and \text_uppercase:n
, respectively, from the commands \@othm
, \@xnthm
, and \@ynthm
. After patching that, the expandability problem is no more :-)
The pdf:
and the pdf bookmark file:
\BOOKMARK [1][-]{section.1}{Proof of Theorem 1}{}% 1
Code:
\documentclass{article}
\usepackage{expl3,etoolbox}
\usepackage{hyperref,cleveref}
\usepackage{crossreftools}
% This patch must be after loading cleverref and before defining a new theorem
\ExplSyntaxOn
\makeatletter
\clist_map_inline:nn { \@othm, \@xnthm, \@ynthm }
{
\patchcmd #1 { \MakeUppercase } { \text_uppercase:n } { } { \FAILED: }
\patchcmd #1 { \MakeUppercase } { \text_uppercase:n } { } { \FAILED: }
\patchcmd #1 { \MakeLowercase } { \text_lowercase:n } { } { \FAILED: }
}
\makeatother
\ExplSyntaxOff
\newtheorem{thm}{Theorem}
\begin{document}
\begin{thm}\label{good}
Cleveref is good. expl3 is awesome :D
\end{thm}
\section{Proof of \crtCref{good}}
It is trivial.
\end{document}
cleveref
's machinery is not expandable (\@ifnextchar
, for example, isn't), so it can't be turned into a PDF string. Thecrossreftools
package provides an expandable alternative. Possible duplicate? tex.stackexchange.com/q/381513/134574Use of \refstepcounter@optarg doesn't match its definition
when I use\crtCref
.cleveref
inserts some non-expandable\MakeUppercase
in the reference to the theorem, and whencrossreftools
tries to use it in an expansion-only context, hell breaks loose. I'll investigate :-)