# automatic \texorpdfstring for \ref in pdf bookmarks (section titles) [duplicate]

\section{Proof of Theorem~\ref{thm:main}}


doesn't produce the correct pdf bookmark (using the hyperref package).

\section{Proof of \texorpdfstring{Theorem~\ref{thm:main}}{Theorem 3.1.7}}


would give the wished result (\texorpdfstring and Header setting), but since "3.1.7" is hard coded I have to change this line of code every time when I rearrange my text.

I understand that many mathematical forumalas cannot be used as pdf strings (pdf bookmarks) since they don't have a Unicode representation (Automatically add \texorpdfstring for math-mode in section titles).

But "Theorem 3.1.7" only contains plain ASCII symbols. So at least theoretically it should be possible to implement an automatic solution for that.

Ideally I would like to have code that allows me to just write:

\section{Proof of \Cref{thm:main}}


(by using the cleveref package, but a solution for \ref would already be very helpful too)

Example:

\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}
\newtheorem{theorem}{Theorem}[section]

\begin{document}

\begin{theorem}\label{thm:main}
text
\end{theorem}

many pages

\section{Proof of \Cref{thm:main}}
\end{document}


(this code does not give the correct pdf bookmarks)

(PS: I think this would be a really great to include such a code in the hypperref or in the cleveref package)

• it would be much easier to answer if you would add a complete example ... – Ulrike Fischer Aug 20 '19 at 14:36
• @Phelype Oleinik: Thank you very much! The answer tex.stackexchange.com/a/485979/128042 perfectly solves my problem (even though the questions are different) – Jakob Aug 20 '19 at 14:51

You can use the starred version of \ref:

\documentclass{article}
\newtheorem{theorem}{theorem}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\section{Proof of Theorem~\texorpdfstring{\ref{thm:main}}{\ref*{thm:main}}}
\begin{theorem}
\label{thm:main} some theorem
\end{theorem}
\end{document}


@Phelype Oleinik linked to a very good solution https://tex.stackexchange.com/a/485979/128042

\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}
\newtheorem{theorem}{Theorem}[section]
\usepackage{crossreftools}

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

\begin{theorem}\label{thm:main}
text
\end{theorem}

many pages

\section{Proof of \Cref{thm:main}}
\end{document}


This solution to the different question Hide cleveref links in section title answers this question also perfectly.

Ulrike Fischer solution https://tex.stackexchange.com/a/504969/128042 is a great alternative if you cannot use the crossreftools package.

• well it is not a perfect solution as it patches a hyperref command. But you don't need \hypersetup related part for your problem. – Ulrike Fischer Aug 20 '19 at 15:58
• Yes removing the hypersetup part improves the solution, so I removed it. I think both solutions have advantages. This solutions for example keeps the colors and allows the use of \Cref. Your solution is simpler and less invasive. – Jakob Aug 20 '19 at 16:15