hyperref
hyperref copied to clipboard
hyperref redefinition of `\contentsline` adds one level of braces if `linktoc=page`
In the definition of \contentsline, there is the following
8636 \def\Hy@temp{#3}%
8637 \ifx\Hy@temp\ltx@empty
8638 \csname l@#1\endcsname{#2}{#3}%
8639 \else
8640 \csname l@#1\endcsname{{#2}}{%
8641 \hyper@linkstart{link}{\Hy@tocdestname}{#3}\hyper@linkend
8642 }%
8643 \fi
This is triggered by linktoc=page option.
The issue (if there is one) is the {{#2}} at code line 8640. This is the only location in \contentsline definition where #2 ends up wrapped this way.
This makes it harder to see if #2 starts with \numberline or variants (depending on document class). In fact this causes a bug in etoc (a bug which was there since initial release of etoc), which does not detect the \numberline and leaves it in \etocname, thus it delayed expansion (which depends on then current setting of \@tempdima) causes varying effect. This surfaced in an issue in yathesis.
Is there any reason for the added braces?
Sorry for my initial posting with empty description, I hit the return key accidentally on keyboard.
Pasting here the code lines from repo (my line numbers in description were pasted from hyperref.pdf)
https://github.com/ho-tex/hyperref/blob/de4100b047195a1451163a8fd3c149eedd2b709b/hyperref.dtx#L12557-L12564
I can't think of a reason (nor is one documented) although test coverage is rather sketchy...
I guess, because in the two other cases (section, all) #2 is inside a group by \hyper@linkstart...\hyper@linkend.
@touhami actually \l@section in article class adds it own group scope (cf my comment to the tex.sx question linked-to next), which allows some abuse like \section{A \color{red} B} although this causes problems with hyperref (see this tex.sx question). Checking now:
-
article class:
\begingroup/\endgroupfor\l@section,{...}for\@dottedtocline. -
book class:
\begingroup/\endgroupfor\l@chapter,{...}for\@dottedtocline,
but the page number will be contaminated by the color for article/section and book/chapter but not in other cases due to a \normalcolor. It could be that hyperref's intent could have been to correct this, but the example of \color is dubious due to the aspects recalled in the tex.sx question linked to above, and \textcolor should be used, for which no extra group is needed.
@touhami besides, at https://github.com/ho-tex/hyperref/blob/de4100b047195a1451163a8fd3c149eedd2b709b/hyperref.dtx#L12549-L12552
there is no hyperlink added, and despite this the #2 is not enhanced with braces.
BTW, regardnig etoc I added a workaround at release v1.08p [2018/07/04]
@jfbu as i said this is just a guess (sorry). In fact, what i mean is the two other cases (section and all) that's mean L12552->12556 for section and L12565->12571 for all. In these two cases #2 is inside a group by \hyper@linkstart...\hyper@linkend, so may be to keep things in the same way the explicits braces were added in the case of page.