7

I'm making pdf bookmarks with hyperref and cleveref packages. However, the pdf bookmarks show the label I defined, rather than the theorem name the label refers to. Here's the code:

\documentclass{article}

\usepackage{hyperref}
\usepackage{cleveref}

\newtheorem{theorem}{Theorem}

\begin{document}

\section{Section 1}
\begin{theorem}\label{some label}
    This is a theorem.
\end{theorem}

\section{Proof to \Cref{some label}}

\end{document}

And here's the pdf: enter image description here

Any idea how to change the label to "Proof to Theorem 1" instead of "Proof to some label"?

Erika L
  • 205

3 Answers3

8

I'm afraid there is no solution for this, apart from using the command \texorpdfstring, that is you have to specify what has to go in the text and what has to go in the bookmarks.

Hence, substitute

\section{Proof to \Cref{some label}}

with

\section{Proof to \texorpdfstring{\Cref{some_label}}{Theorem \ref{some_label}}}

Note that it isn't a good idea to have label names with spaces, so I've substituted some label with some_label.

MWE:

\documentclass{article}

\usepackage{hyperref}
\usepackage{cleveref}

\newtheorem{theorem}{Theorem}

\begin{document}

\section{Section 1}
\begin{theorem}\label{some_label}
    This is a theorem.
\end{theorem}

\section{Proof to \texorpdfstring{\Cref{some_label}}{Theorem \ref{some_label}}}

\end{document} 

Output:

enter image description here

karlkoeller
  • 124,410
5

The \Cref command cannot be used in \section when bookmarks are being produced; however, \autoref works (it's less powerful, though). For a simple application like this you can do as follows:

\documentclass{article}

\usepackage{hyperref}
\usepackage{cleveref}

\pdfstringdefDisableCommands{\let\Cref\autoref}

\newtheorem{theorem}{Theorem}

\begin{document}

\section{Section 1}

\begin{theorem}\label{some_label}
This is a theorem.
\end{theorem}

\section{Proof to \Cref{some_label}}

\end{document}

enter image description here

egreg
  • 1,121,712
  • Thanks. I did try [tag:autoref], but I do have a bunch of lemmas/propositions/corollaries which makes [tag:cleveref] more suitable. – Erika L Mar 28 '15 at 20:43
  • @ErikaL Yes, but as you see, I let \Cref to \autoref only in bookmarks where super precision is not really necessary. – egreg Mar 28 '15 at 20:44
  • It does work for the MWE. However I found another strange behavior using this method, see [http://tex.stackexchange.com/questions/235634/discrepancy-between-thm-and-theorem] – Erika L Mar 28 '15 at 21:12
  • This answer deserves to be upvoted more. It is an automatic solution relative to the manual solution that the accepted answer proposes. The virtues are obvious. If a figure number being referred to in the bookmark/section heading changes, then hand-coding a string quickly becomes dangerously out of date & misleading. – Dr Krishnakumar Gopalakrishnan Jan 08 '19 at 20:37
2

Based on Phelype Oleinik's solution: https://tex.stackexchange.com/a/485979/128042 one can use the crossreftools package together with some custom code.

\documentclass{article}

\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{crossreftools}

\pdfstringdefDisableCommands{%
    \let\Cref\crtCref
    \let\cref\crtcref
}

\newtheorem{theorem}{Theorem}

\begin{document}

\section{Section 1}

\begin{theorem}\label{some_label}
This is a theorem.
\end{theorem}

\section{Proof to \Cref{some_label}}

\end{document}

This automatically creates the correct pdf bookmarks (in TexXStudio one has to compile it two times). (If you are also interested in removing the color of the links, see Phelype Oleinik's solution: https://tex.stackexchange.com/a/485979/128042.)

PS: This answer was also improved by Ulrike Fischer's comment on https://tex.stackexchange.com/a/504970/128042 (answering the equivalent question automatic \texorpdfstring for \ref in pdf bookmarks (section titles)).

Jakob
  • 993