Once I added \cprotect
around my section titles (I need to do this because I could have math in some of them). Now when I open a PDF file, all bookmarks are lost, and in their place I see something cpt
on each one and section and subsections names do not show up. I got an answer on this before to use \usepackage[bookmarks=false]{hyperref}
, but at the time I did not know what this meant. Now I noticed it causes PDF books marks to get lost, so I can't use such a solution.
MWE
\documentclass[12pt]{book}
\usepackage{cprotect}
\usepackage{hyperref}
\begin{document}
\chapter{A}
\cprotect\section{B}
\cprotect\subsection{C}
stuff
\cprotect\subsection{D}
stuff
\end{document}
Compiled using lualatex
.....
Chapter 1.
(./foo3-1.cpt)
Package hyperref Warning: Token not allowed in a PDF string (Unicode):
(hyperref) removing `\@ifnextchar' on input line 8.
(./foo3-2.cpt)
Package hyperref Warning: Token not allowed in a PDF string (Unicode):
(hyperref) removing `\@ifnextchar' on input line 9.
(./foo3-3.cpt)
Package hyperref Warning: Token not allowed in a PDF string (Unicode):
(hyperref) removing `\@ifnextchar' on input line 12.
And the PDF file bookmarks look like this (Adobe PDF reader)
Removing cprotect
and now it works:
\documentclass[12pt]{book}
\usepackage{hyperref}
\begin{document}
\chapter{A}
\section{B}
\subsection{C}
stuff
\subsection{D}
stuff
\end{document}
gives
I want to use cprotect
, but not lose PDF bookmarks. Is there a way to do this?
TL 2020 on Linux
Update to answer comment
Please edit your posting to give an actual example of a \section command that's causing grief.
Here is an example which fails on LuaTeX, unless I use \cprotect
, and when I use \cprotect
bookmarks are lost. This happens because I am using \usepackage{Baskervaldx}
which I like the font
\documentclass[12pt]{book}
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\setmathfont{Asana Math}
\usepackage{Baskervaldx}
\usepackage{amsmath}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\chapter{A}
\section{$\cos\left( A+B\right) $ and $\sin\left( A+B\right) $}%
\subsection{C}
stuff
\subsection{D}
stuff
\end{document}
Compile using LuaLaTeX gives
Package hyperref Warning: Token not allowed in a PDF string (Unicode):
(hyperref) removing `math shift' on input line 15.
! Improper alphabetic constant.
<to be read again>
\math@bgroup
l.15 \section{$\cos\left( A+B\right) $ and $\sin\left( A+B\right) $}
%
?
But if I use cprotect
it works it compiles with no error, but no bookmarks now
\documentclass[12pt]{book}
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\setmathfont{Asana Math}
\usepackage{Baskervaldx}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{cprotect}
\begin{document}
\tableofcontents
\chapter{A}
\cprotect\section{$\cos\left( A+B\right) $ and $\sin\left( A+B\right) $}%
\subsection{C}
stuff
\subsection{D}
stuff
\end{document}
gives
I have many many such examples. Here is another
\section{ this is $\zeta$ }%
gives
Package hyperref Warning: Token not allowed in a PDF string (Unicode):
(hyperref) removing `math shift' on input line 15.
! Improper alphabetic constant.
<to be read again>
\mitzeta
l.15 \section{ this is $\zeta$ }
%
?
Please note that these all fail, because I am using the font
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\setmathfont{Asana Math}
\usepackage{Baskervaldx}
I could of course not use the above font, and then it will compile OK and bookmarks will remain there (but without the Math which is OK with me). So maybe I have to do this and use the above font, which I like, but having the bookmarks is more important. So this is an option if there is no other solution.
Is it possible to tell hyperref, if it finds something it can put in the bookmark, only for that section it is OK to replace it with .cpt
, but not for everything?
The problem is that I pre-process the whole LaTeX file, and add \cprotect around each section and subsection just in case they have math in them. So now all bookmarks are lost.
I can not do this case by case, since I have 10's of thousands of such entries.
Note on error found and solution
This is too small to write in comment, so I am adding it here.
An error is generated due to wrong order of packages. It has nothing to do with luacode.
This fails
% !TEX TS-program = lualatex
\documentclass{book}
\usepackage{amsmath,mleftright}
\usepackage{unicode-math}
\usepackage{Baskervaldx}
\setmathfont{Asana Math}[Scale=MatchLowercase]
\usepackage{xcolor}
\usepackage[colorlinks,allcolors=blue,linktocpage]{hyperref}
\begin{document}
\section{Solve numerically the ODE $u''''+u=f$ using point collocation method}
test
\end{document}
Compiled using LuaLaTeX gives
t) (./foo3.out)
! Undefined control sequence.
\g__um_prime_font_cmd_tl ->\l__um_font
l.14 \section{Solve numerically the ODE $u''''+u=f$ using point collocation method}
?
The fix is to put \usepackage{Baskervaldx}
after \setmathfont{Asana Math}
, so the order becomes
\usepackage{amsmath,mleftright}
\usepackage{unicode-math}
\setmathfont{Asana Math}[Scale=MatchLowercase]
\usepackage{Baskervaldx}
\usepackage{xcolor}
\usepackage[colorlinks,allcolors=blue,linktocpage]{hyperref}
And now it compiles OK. It has nothing to do with math in section. Here is an example:
% !TEX TS-program = lualatex
\documentclass{book}
\usepackage{amsmath,mleftright}
\usepackage{unicode-math}
\usepackage{Baskervaldx}
\setmathfont{Asana Math}[Scale=MatchLowercase]
\usepackage{xcolor}
\usepackage[colorlinks,allcolors=blue,linktocpage]{hyperref}
\begin{document}
\section{test}
Solve $y''(x)-3 y(x) = -x^2$ over $x=0\ldots1$ with boundary conditions
$x(0)=0$ and $x(1)=0$ using piecewise linear trial functions.
\end{document}
It compiles with an error:
! Undefined control sequence.
\g__um_prime_font_cmd_tl ->\l__um_font
l.17 Solve $y''(
x)-3 y(x) = -x^2$ over $x=0\ldots1$ with boundary conditions
?
Again, changing the order of packages, the error is gone. This is why I was getting some error testing Mico's nice code.
\cprotect
. Having math material in the argument of\section
is not a (valid) reason for needing to use\cprotect
. – Mico Jun 7 '20 at 5:34\cprotect
I get errors on some math constructs. When I add\cprotect
the error goes away, but I lose the bookmarks. I also do not want to use something like\texorpdfstring
to rewrite each section title. So now I automatically add\cprotect
around each section and subsection title for everything, just in case there is some math in them., – Nasser Jun 7 '20 at 5:37\usepackage{Baskervaldx}
with lualatex. That is a pdftex font package, it uses an unsuitable font encoding and it will break all sort of things. Add e.g.Grüße
to see one of the problems. – Ulrike Fischer Jun 7 '20 at 15:22