7

This question already has an answer here:

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"?

marked as duplicate by Stefan Pinnow, vonbrand, schtandard, Sebastiano, Phelype Oleinik Aug 20 at 20:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Mar 28 '15 at 6:40
  • \Cref is not really expandable, I fear, since it's defined to be a robust command – user31729 Mar 28 '15 at 6:47
7

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

  • That solves the problem (with a bit more work), thank you! – Erika L Mar 28 '15 at 16:01
4

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

  • Thanks. I did try autoref, but I do have a bunch of lemmas/propositions/corollaries which makes 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 [tex.stackexchange.com/questions/235634/… – 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. – Krishna Jan 8 at 20:37
1

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)).

Not the answer you're looking for? Browse other questions tagged or ask your own question.