12

I know that this question was asked almost 80 times and that the best solution is probably to use \texorpdfstring{...}{...} but I don't want to a mess inside the document so I was thinking of this solution.

\pdfstringdefDisableCommands{%
  \def\${}%
  \def\alpha{alpha}%
}

But this doesn't change the fact that

\section{$\alpha$}

gives the warning

Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding): (hyperref) removing `math shift' [...]

So the question is: Can I use \pdfstringdefDisableCommands{...} to avoid using \texorpdfstring{...}{...} when handling math in sections?

2 Answers 2

13

Here I use \(...\) in lieu of $...$. Warnings avoided.

\documentclass{article}
\usepackage{hyperref}
\pdfstringdefDisableCommands{%
%  \def${}%
  \def\alpha{alpha}%
  \def\({}%
  \def\){}%
  \def\texttt#1{<#1>}%
}
\begin{document}
\section{\texttt{xyz}\(\alpha\)}
\end{document} 

enter image description here

or get a bit more descriptive in the replacement:

\documentclass{article}
\usepackage{hyperref}
\pdfstringdefDisableCommands{%
  \def\alpha{alpha}%
  \def\({Math:[}%
  \def\){]}%
  \def\texttt#1{<#1>}%
}
\begin{document}
\section{\texttt{xyz}\(\alpha\)}
\end{document} 

enter image description here

1
  • 5
    \alpha is supported by option psdextra and the bookmark needs to be in Unicode: \usepackage[pdfencoding=auto, psdextra]{hyperref}. Without option psdextra the macro \textalpha can be used (option unicode or pdfencoding=auto is still needed). May 9, 2018 at 20:20
1

Since I’m using unicode-math package, I actually do this in my works:

\documentclass{article}
\usepackage[unicode]{hyperref}
\usepackage{unicode-math}
\newcommand\pdfmath[1]{\texorpdfstring{$#1$}{#1}}
\begin{document}
\section{\pdfmath{α}}
\end{document}

Or, as Heiko Oberdiek proposed if you’re not into unicode-math, you can do this:

\documentclass{article}
\usepackage[unicode,psdextra]{hyperref}
\newcommand\pdfmath[1]{\texorpdfstring{$#1$}{#1}}
\begin{document}
\section{\pdfmath{\alpha}}
\end{document}

It’s slightly better than plain \texorpdfstring though not ideal. So all of that is only if like me you don’t like \( and \) as delimiting your math (but also see https://tex.stackexchange.com/a/513/56823 and especially comments below about this), else you would better go with Steven + Heiko answers. ;)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .