2

At the risk of being attacked for asking one of these questions again:
Ways to avoid 'Package hyperref Warning: Token not allowed in a PDF string' warning
Hyperref warning - Token not allowed in a PDF string
Hyperref - Token not allowed

I decided to add hyperref and of course I'm getting these warnings because I have a document with sections & a table of contents where I've used $...$ symbols to type math in many of the section titles (I understand that the pdf is trying to create bookmark names using these titles, and has trouble with math). However, looking at the submitted answers, I'd rather not have to go in and do anything that involves manually fixing each of the section titles (like add in a \texorpdfstring to each one, and/or change to \(...\) everywhere. Also, I'd like to not have to "remember a different set of rules" for section headers: I would like to keep using $...$ for inline math ubiquitously.

Is there any way I can use hyperref links in my document that doesn't involve changing the math inside each of my section titles?

I have tried (but not really understood) the \pdfstringdefDisableCommands command, but I'm not sure exactly how to ignore the $ symbols. Notice the answer above used \(...\). So if someone can help explain how to do that, I'd appreciate it.

5
  • Can you give an example of an offending title that you're using? Why not just disable the bookmarks?
    – Werner
    Oct 26, 2021 at 3:10
  • ...for example, see How to remove bookmarks from you PDF LaTeX file?
    – Werner
    Oct 26, 2021 at 3:30
  • @Werner Literally anything like \section{$a$} will trigger this. And in my case I don’t want to disable the bookmarks, I use them. But I tried hard and unfortunately @Keshav it does not seems possible to “disable” $. Else it would have been shown in your first link (which I still count your question as a duplicate of).
    – Archange
    Oct 26, 2021 at 5:43
  • Check the bookmarks. If they look ok, simply ignore the hyperref warnings. Oct 26, 2021 at 6:27
  • @Werner, yes Archange represents my issue well. Meanwhile if I try to ignore it: unfortunately only some of the hyperlinks seem to be working, and some references to equations start to disappear once I activate hyperref. Thanks for the help, it seems like there's no way to do it.
    – Keshav
    Oct 27, 2021 at 4:08

1 Answer 1

1

You can set conversions of math symbols (like \alpha) globally via \pdfstringdefDisableCommands and silence hyperref to avoid printing the warning message that contains math shift - an indication that math mode has been activated in the title.

enter image description here

\documentclass{article}

\usepackage{hyperref}

\makeatletter
\let\oldHyPsd@CatcodeWarning\HyPsd@CatcodeWarning% Store original catcode warning
\renewcommand{\HyPsd@CatcodeWarning}[1]{% Update to check for...
  \ifnum\pdfstrcmp{#1}{math shift}=0    % ...the presence of 'math shift'. If so, do nothing.
  \else                                 % Otherwise,
    \oldHyPsd@CatcodeWarning{#1}%       % (if no 'math shift') just print the regular warning.
  \fi
}
\makeatother

\pdfstringdefDisableCommands{%
  \def\alpha{a}% Convert \alpha to a
}

\begin{document}

\tableofcontents

\section{A section}

\section{A section with $math$}

\section{A section with $\alpha$}

\end{document}

This is a very crude approach and I'd rather suggest fixing the problem at the core than to find innovative ways to skirt around it. Core-type fixes would be to use \texorpdfstring{<TeX>}{<PDF>}.

You must log in to answer this question.

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