2

I just noticed something weird. Take the following example

\documentclass{article}

\usepackage{hyperref} \hypersetup{hidelinks}

\begin{document}

\tableofcontents

\addcontentsline{toc}{section}{``xxx''} \addcontentsline{toc}{section}{Here---and \emph{there}} \addcontentsline{toc}{section}{1--2 vs. 1-{}-2} \addcontentsline{toc}{section}{\LaTeX}

\end{document}

which results in the following PDF

Output

The printed contents is perfect, of course. The PDF bookmarks (on the left) are actually almost correct, too: various LaTeX specifics get converted into something meaningful (formatting commands get removed, \LaTeX becomes LaTeX, double and triple hyphens correctly become en- and em-dashes, etc.). I was surprised at first when I developed this example because I had just noticed the opening and closing double quotes which are left verbatim as pairs of single (back-)quotes, and thus I thought all these examples would be wrong. But no, only the double quotes are an example where the LaTeX input is not converted into the typographically correct PDF bookmark—or, at least, the only one among those which I thought of testing....

Is there anything to be done about this? I have thought of cloning the hyperref.sty definition of \addcontentsline and using \StrSubstitute from xstring to replace pairs of single quotes with double quotes when \Hy@writebookmark is used, but it looks a pretty clumsy and fragile solution. Any better idea?

Paolo
  • 61
  • 6
  • Maybe related: https://tex.stackexchange.com/questions/251491/math-symbol-in-section-heading – Dr. Manuel Kuehner Apr 11 '21 at 00:32
  • 1
    Maybe look into the command \texorpdfstring. – Dr. Manuel Kuehner Apr 11 '21 at 00:32
  • 1
    this has been discussed here: https://github.com/latex3/hyperref/issues/112. As I wrote there, while it would be possible to add another substituation, it is imho more sensible if the input uses real quotes or \enquote, if you really care about how the bookmarks look (personally I don't think that many people will notice). – Ulrike Fischer Apr 11 '21 at 09:17
  • @Dr. Manuel Kuehner, thanks for pointing me to \texorpdfstring, but it turns out to be unnecessary: rather than saying \texorpdfstring{``}{“} (which works) it is easier to forget about the LaTeX representation of opening quotes and write directly. – Paolo Apr 11 '21 at 09:46
  • @Ulrike Fischer, thanks for the link. I will follow your suggestion. Yet, I believe this should be considered a bug of hyperref: I do not see why --- would be correctly substituted and '' not as both are standard LaTeX (and, if you allow me, whether people notice or not is not really the point). – Paolo Apr 11 '21 at 09:52
  • Well you have to balance. What about <<? Or fi? Such substituations slows down the compilation--even if there are no quotes hyperref would still have to look for all of them. Obviously who ever added them in the first place choose to do only the really visible ones and personally I think that is fine. – Ulrike Fischer Apr 11 '21 at 09:58

1 Answers1

2

Based on the discussion of this hyperref issue mentioned by Ulrike Fischer in a comment and on her code in another post, here is what I think works best:

\documentclass[german,french,english]{article}

\usepackage[autostyle=true]{csquotes} \usepackage{babel} \usepackage{hyperref} \hypersetup{hidelinks}

% you need the following only if you want the quotes in bookmarks to % reflect the specific style used in the document, otherwise they are % simple plain quotes \makeatletter \DeclarePlainStyle{\csq@thequote@oopen}{\csq@thequote@oclose}{\csq@thequote@iopen}{\csq@thequote@iclose} \pdfstringdefDisableCommands{\csq@resetstyle} \makeatletter

\begin{document}

\tableofcontents

\addcontentsline{toc}{section}{Plain \LaTeX\ does not work because hyperref does not substitute quotes''} \addcontentsline{toc}{section}{\texorpdfstring{}{“}texorpdfstring works but is pointlessly verbose\texorpdfstring{''}{”}} \addcontentsline{toc}{section}{“UTF-8 quotes work fine and are a good straightforward solution”} \addcontentsline{toc}{section}{\enquote{enquote is the perfect multilingual solution}{}} \selectlanguage{german} \addcontentsline{toc}{section}{\enquote{enquote is the perfect multilingual solution}{}} \selectlanguage{french} \addcontentsline{toc}{section}{\enquote{enquote is the perfect multilingual solution}{}}

\end{document}

The result is this:

enter image description here

I do not understand why I had to add {} after the \enquote, but it was necessary to make it work (and does not appear to have negative side effects).

Paolo
  • 61
  • 6