2

I'm writing a book which contains long footnotes and many links, all created with \hyperlink. If a linked text in a footnote wraps to the following page, then the following happens: The color of the linked text in the footnote disappears on the second page, and the whole main text of the second page is now linked to the target. This happens in the book- and the article-class, but not in amdbook- and amsart-class. Is there a possibility to avoid this?

Here is a minimal code:

\documentclass[11pt]{book}
\usepackage{lipsum}
\usepackage[colorlinks]{hyperref}
%
\begin{document}
\lipsum[2-5]\footnote{\hyperlink{target}{\lipsum[2-3]}}
\lipsum[2-3]
\newpage
\hypertarget{target}{THIS IS THE TARGET!}
%
\end{document}

2 Answers 2

1

To improve the color you can load the pdfcolfoot package.

But for the link there is currently no good solution. You are paying the price for the ability of pdftex to break links over lines and pages: To be able to do this it adds everything between start and end point to the link area, and this encompasses the text body.

A current texlive offers commands to interrupt a link, and for the header and footer there is code in the new PDF management which make use of them: https://tex.stackexchange.com/a/578405/2388.

But this can't be used easily for footnotes. In your example one can for example correct the link with

\documentclass[11pt]{book}
\usepackage{lipsum}
\usepackage[colorlinks]{hyperref}
\usepackage{pdfcolfoot}
\usepackage{etoolbox}
\makeatletter
\patchcmd\@makecol
  {\pdfcolfoot@current\unvbox\footins}
  {\pdfcolfoot@current\pdfrunninglinkon \unvbox \footins \pdfrunninglinkoff}
  {}{\fail}
\makeatletter

\begin{document}
\lipsum[2-5]\footnote{\hyperlink{target}{\lipsum[2-3]}}
\lipsum[2-3]
\newpage
\hypertarget{target}{THIS IS THE TARGET!}
%
\end{document}

But if the main text body contains also a link with a page break the link targets gets confused and the second part of the footnote links to the url:

\documentclass[11pt]{book}
\usepackage{lipsum}
\usepackage[colorlinks]{hyperref}
\usepackage{pdfcolfoot}
%
\usepackage{etoolbox}
\makeatletter
\patchcmd\@makecol
  {\pdfcolfoot@current\unvbox\footins}
  {\pdfcolfoot@current\pdfrunninglinkon \unvbox \footins \pdfrunninglinkoff}
  {}{\fail}
\makeatletter
\begin{document}

\lipsum[2-3]
\footnote{\hyperlink{target}{\lipsum[1-4]}}%
\href{https://www.latex-project.org}{\lipsum[3-5]}%
\lipsum[2]
\newpage
\hypertarget{target}{THIS IS THE TARGET!}
%
\end{document}

enter image description here

So this is not a stable solution. I doubt that one can repair this in pdftex. In my opinion one will need luatex and new link code based on attributes.

Side remark: I quite loath documents with lots of large link areas. I'm often reading on a touch screen and it is a nuisance if one triggers a link when one only wants to scroll or to zoom.

5
  • Thanks a lot, Ulrike. Unfortunately, I need the package fontspec and I'm using LuLaTeX. Then I get an error: Undefined control sequence \pdfrunninglinkoff Commented Jan 16, 2023 at 20:54
  • with luatex the command is \pdfextension linkstate 0 \unvbox \footins \pdfextension linkstate 1 . With luatex you could also try github.com/zauguin/lua-links Commented Jan 16, 2023 at 21:05
  • If I add this command just after the package hyperref, then the footnote is colored, but only the first line of the footnote is linked. \documentclass[11pt]{book} \usepackage{lipsum} \usepackage{fontspec} \usepackage[breaklinks,colorlinks]{hyperref} \usepackage{pdfcolfoot} \usepackage{etoolbox} \pdfextension linkstate 0 \unvbox \footins \pdfextension linkstate 1 \begin{document} \lipsum[2-5]\footnote{\hyperlink{target}{\lipsum[2-3]}} \lipsum[2-3] \newpage \hypertarget{target}{THIS IS THE TARGET!} \end{document} Commented Jan 18, 2023 at 16:37
  • I'm having troubles with this same problem at the moment, and came here to find a solution. I wonder: would there be a chance to write a command that takes a phrase and applies the hyperlink to every single character that composes the phrase? This way if a few characters end up in the following page nothing happens, because the hyperlink has not been split in between the two footnotes. Commented yesterday
  • Ok, I was able to make something that seems to work, mostly. Check out my answer. Commented yesterday
0

I was having a similar situation and after tinkering for a few hours, mixing solutions that I found on various other threads, I was able to produce something that works for me and that works on OP's MWE. It still will produce some underwhelming results in some situations which I'm not being able to explain, and that I will show you.

In my case, what I was trying to do was to have external hyperlinks, not internal ones. The rest of the situation was the same: if the link text was split amount two pages everything that was in between became an hyperlink.

I was able to solve this problem by applying the hyperlink to every single character of the link text, this way:

\documentclass[a4paper, 10pt, italian]{book}

\usepackage[italian]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[paperwidth=10.77cm, paperheight=15.24cm, heightrounded, text={7.77cm, 11.50cm}, centering]{geometry}
\usepackage[hidelinks]{hyperref}
\usepackage{microtype}

% Latex3 command to add a link to every single character
\ExplSyntaxOn
\NewDocumentCommand{\applylinktoeachchar}{mm}
{
    \group_begin:
    \tl_set:Nn \l_tmpa_tl { #2 } % Set the temporary token list to the input string
    \tl_replace_all:Nnn \l_tmpa_tl { ~ } { \c_space_tl }
    \tl_map_inline:Nn \l_tmpa_tl { \href{#1}{##1}\allowbreak } % Map the command to each character in the token list
    \group_end:
}
\ExplSyntaxOff

% Command used in text to manage URLS
\newcommand{\hlink}[1]{%
    \textsf{\applylinktoeachchar{#1}{#1}}%
}

% Settings to solve the underfull badness errors
\setlength{\headheight}{13.59999pt}
\vbadness=10000

\begin{document}
    
    Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn. Dagon, nog sll'ha nog Shub-Niggurathagl. Hupadgh tharanak k'yarnakyar ron R'lyeh lloig n'gha ngilyaa Nyarlathotep Hastur 'ai fhtagn n'ghft, ngluiyar ilyaa kadishtu hafh'drn nnnAzathoth hlirgh ep Yoggoth geb ch' y'hah. Fm'latgh uln ron kn'a uln Nyarlathotep nglui zhro hlirgh, naflgnaiih h'fhtagn syha'h li'hee kn'a sll'haoth nashagg nnnhafh'drn shagg, hlirgh cehye vulgtm shaggoth hupadgh sll'ha zhrooth. Y-vulgtlagln kadishtu athg y'hah wgah'n, gnaiih shagg athg geb y-Chaugnar Faugn, ron fm'latgh syha'h. Dagon hafh'drn sll'ha nw nafly'hah mnahn' throd nnnshtunggli ngkn'a 'ai, ee mg hai hupadgh r'luh Yoggoth ah grah'n hlirghyar hafh'drn, llll ch' nog bug vulgtm ebunma lloig cShub-Niggurath. 
    
    Zhroog ee Nyarlathotep ee f'Dagon Dagon hafh'drn 'ai fm'latgh, hlirgh Cthulhuoth fm'latghoth n'ghft lw'nafh ah Azathoth naflkadishtu, ya orr'eog vulgtm ee uaaah ooboshuoth fm'latgh bug, mg ron geb n'gha mnahn' y-ooboshu hupadgh. 'bthnk Azathoth Chaugnar Faugn phlegeth r'luh s'uhn cllll ooboshu kn'a naflCthulhu, throd ron hlirgh hai ph'Cthulhu eeagl ngbug bug, naflvulgtm ebunma mg uln Yoggoth hafh'drn ngmg hrii\footnote{\hlink{https://blablablablablablablablablablablablablablablablablablablablablablablabla.com}}. NaflCthulhu n'gha R'lyeh Nyarlathotep tharanak y-Dagon hlirghagl ngftaghu sgn'wahl lw'nafh k'yarnak cnog, kadishtu y'hah Azathoth shagg nnnuln lloig mg Dagon orr'e ep. 'bthnk y'hah nanglui h'k'yarnak wgah'n nw h''ai n'gha vulgtlagln, Chaugnar Faugn s'uhn nilgh'ri ph'ehye ooboshu y'hah nnnuaaah vulgtlagln, fm'latgh vulgtm hupadgh r'luh n'ghft h'shogg nilgh'ri mnahn'nyth, R'lyehagl f'n'gha gof'nnoth zhro nafm'latgh sll'haagl nnngeb. 
    
    Nw Cthulhu throd naflNyarlathotep chtenff ehye h''fhalma Cthulhu zhronyth h'orr'e, throd ngftaghu uh'e phlegeth h'orr'e nnnuln chtenff Hastur ep ya, Dagon ngluior li'hee nog 'bthnk s'uhn shogg mg. Vulgtm ilyaa ph'lw'nafh ee s'uhn, lloig Hastur uaaah. Shoggoth ck'yarnak Chaugnar Faugn lloigor Nyarlathotep Hastur nglui, ftaghu shugg phlegeth shogg ftaghuyar shogg f'vulgtlagln, ch' y-chtenff Shub-Niggurath gnaiihoth lloig. Y-Tsathoggua ron gnaiih kadishtu ftaghu y-orr'e Yoggoth uln hafh'drn chtenff h'sgn'wahl y-'bthnk, nilgh'ri tharanak Dagon ehye nw cR'lyeh Hastur ftaghu ooboshu Cthulhu, mnahn'og shtunggli llll ron ebunma hriior hupadgh y-sll'ha Shub-Niggurath naflshogg.
    
\end{document}

So: I don't use \url; instead I manually format the text so that it becomes sans serif and can be broken anywhere, which was something that I previously was doing via xurl.

Now, with a few modifications this works also for OP's MWE:

\documentclass[11pt]{book}

\usepackage[colorlinks]{hyperref}
\usepackage{microtype}
\usepackage{lipsum}

% Latex3 command to add a link to every single character
\ExplSyntaxOn
\NewDocumentCommand{\applylinktoeachchar}{mm}
{
    \group_begin:
    \tl_set:Nn \l_tmpa_tl { #2 }
    \tl_replace_all:Nnn \l_tmpa_tl { ~ } { \c_space_tl }
    \tl_map_inline:Nn \l_tmpa_tl { \hyperlink{#1}{##1}\allowbreak } % Map the command to each character in the token list
    \group_end:
}
\ExplSyntaxOff

% Command used in the text to write urls
\newcommand{\hlink}[2]{%
    \applylinktoeachchar{#1}{#2}%
}

% Settings to solve the underfull badness errors
\setlength{\headheight}{13.59999pt}
\vbadness=10000

\begin{document}
    \lipsum[2-5]\footnote{\hlink{target}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer condimentum consectetur aliquet. Sed lobortis eleifend sem. Nunc a mattis velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Pellentesque porta ultrices nisi. Praesent in justo nec tortor blandit vehicula. Sed non tortor sit amet mi tempus porta. Mauris eget lorem vel nulla fringilla vulputate. Mauris at est sed nulla cursus maximus. Curabitur interdum est turpis, maximus tempus nisi hendrerit vel. Donec dapibus neque sit amet ex mattis aliquam. Vivamus fermentum, quam ultricies aliquet sodales, sapien arcu feugiat justo, et commodo sem libero ut urna. Vestibulum at lacus elit. Ut blandit massa non libero rutrum, non aliquet tellus ultricies. Sed ultrices felis nisl, eu accumsan neque tempus quis. Nullam non leo et ex tempor tempus vitae vitae elit. Aenean quis velit rhoncus, pharetra ex quis, auctor urna. Nulla sagittis quam justo, nec aliquet urna eleifend a. Nam et tortor vitae nulla auctor vestibulum vitae tincidunt massa. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec urna purus, faucibus id fringilla at, dignissim vel nulla. Pellentesque convallis interdum massa, ut consequat ipsum mattis ac. Nulla in nibh et purus posuere elementum. Nunc ut tortor velit. Mauris egestas mi eget odio fermentum tincidunt. In hac habitasse platea dictumst. Sed tristique feugiat augue. Nunc sollicitudin faucibus felis, at sodales risus ultrices eget. Pellentesque mollis varius condimentum. Suspendisse sit amet odio posuere, egestas felis nec, aliquam ante. Phasellus id lorem eget est dignissim semper. Nulla blandit convallis diam. Fusce a lacinia lorem. Proin vestibulum interdum efficitur. Aenean suscipit, lectus vel gravida pharetra, enim eros maximus libero, eget fermentum tellus metus at lorem. Sed et sagittis erat. Mauris consequat dignissim rutrum. Nam mauris mi, dapibus sed eleifend vitae, interdum vel nibh. Quisque facilisis dui sed arcu ultrices, sit amet efficitur eros consequat. Integer enim tellus, accumsan eu sodales id, condimentum id neque. Nullam fringilla non ligula vitae ultricies. Nunc ut varius ex. Nullam facilisis lobortis ultricies. Maecenas et eleifend purus. Cras eleifend lacus eget nibh tristique malesuada. Donec sit amet eros nulla. Etiam sem tortor, consectetur eu risus vitae, euismod luctus mauris. Nullam aliquam odio nec nisi consectetur aliquam. Nam iaculis imperdiet sagittis. Proin ac dapibus lorem, ut pulvinar ipsum. Suspendisse vitae nisi suscipit, feugiat ex vitae, commodo mauris. Nulla facilisis suscipit enim eget dictum. Aenean ut nisl dui. Nulla at velit augue. Mauris ultrices tellus nisl, at bibendum arcu hendrerit ut. Donec in mollis quam, sed congue justo. Duis tempus enim sit amet est dignissim fermentum. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Fusce malesuada orci id velit pellentesque, ut pretium massa fermentum. Aenean commodo felis et lectus interdum, eget volutpat nulla gravida. Sed ornare lorem nisi, at venenatis libero consectetur vitae. Aenean lobortis enim non ultrices bibendum. Praesent luctus tincidunt libero, quis fermentum ex vehicula quis. Sed quis facilisis nisi, quis malesuada augue.}}
    \lipsum[2-3]
    \newpage
    \hypertarget{target}{THIS IS THE TARGET!}
    %
\end{document}

This apparently works.

There are two situations in which this code still does not work.

The first is when the link text comes from a command. Which was the case of OP's MWE, where it was the result of \lipsum. Even using a custom command the result will be the same. Removing the colorlinks option we can see that the note does not get divided in its characters. I suppose this is something related to expansion, but I tried to solve this by expandafter or expandnext from etextools without any result.

The second situation is when the footnote number is exactly at the end of the line. I have no idea about why this happens, but here's the MWE:

\documentclass[a4paper, 10pt, italian]{book}

\usepackage{hyperref}
\usepackage{microtype}
\usepackage{lipsum}
\usepackage[T1]{fontenc}
\usepackage[paperwidth=10.77cm, paperheight=15.24cm, heightrounded, text={7.77cm, 11.50cm}, centering]{geometry}

% Latex3 command to add a link to every single character
\ExplSyntaxOn
\NewDocumentCommand{\applylinktoeachchar}{mm}
{
    \group_begin:
    \tl_set:Nn \l_tmpa_tl { #2 }
    \tl_replace_all:Nnn \l_tmpa_tl { ~ } { \c_space_tl }
    \tl_map_inline:Nn \l_tmpa_tl { \hyperlink{#1}{##1}\allowbreak } % Map the command to each character in the token list
    \group_end:
}
\ExplSyntaxOff

% Command used in the text to write urls
\newcommand{\hlink}[2]{%
    \applylinktoeachchar{#1}{#2}%
}

% Settings to solve the underfull badness errors
\setlength{\headheight}{13.59999pt}
\vbadness=10000

\newcommand{\foo}{}

\begin{document}
    \lipsum[2-5]\footnote{\hlink{target}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer condimentum consectetur aliquet. Sed lobortis eleifend sem. Nunc a mattis velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Pellentesque porta ultrices nisi. Praesent in justo nec tortor blandit vehicula. Sed non tortor sit amet mi tempus porta. Mauris eget lorem vel nulla fringilla vulputate. Mauris at est sed nulla cursus maximus. Curabitur interdum est turpis, maximus tempus nisi hendrerit vel. Donec dapibus neque sit amet ex mattis aliquam. Vivamus fermentum, quam ultricies aliquet sodales, sapien arcu feugiat justo, et commodo sem libero ut urna. Vestibulum at lacus elit. Ut blandit massa non libero rutrum, non aliquet tellus ultricies. Sed ultrices felis nisl, eu accumsan neque tempus quis. Nullam non leo et ex tempor tempus vitae vitae elit. Aenean quis velit rhoncus, pharetra ex quis, auctor urna. Nulla sagittis quam justo, nec aliquet urna eleifend a. Nam et tortor vitae nulla auctor vestibulum vitae tincidunt massa. Interdum et malesuada fames ac ante ipsum primis in faucibus. Donec urna purus, faucibus id fringilla at, dignissim vel nulla. Pellentesque convallis interdum massa, ut consequat ipsum mattis ac. Nulla in nibh et purus posuere elementum. Nunc ut tortor velit. Mauris egestas mi eget odio fermentum tincidunt. In hac habitasse platea dictumst. Sed tristique feugiat augue. Nunc sollicitudin faucibus felis, at sodales risus ultrices eget. Pellentesque mollis varius condimentum. Suspendisse sit amet odio posuere, egestas felis nec, aliquam ante. Phasellus id lorem eget est dignissim semper. Nulla blandit convallis diam. Fusce a lacinia lorem. Proin vestibulum interdum efficitur. Aenean suscipit, lectus vel gravida pharetra, enim eros maximus libero, eget fermentum tellus metus at lorem. Sed et sagittis erat. Mauris consequat dignissim rutrum. Nam mauris mi, dapibus sed eleifend vitae, interdum vel nibh. Quisque facilisis dui sed arcu ultrices, sit amet efficitur eros consequat. Integer enim tellus, accumsan eu sodales id, condimentum id neque. Nullam fringilla non ligula vitae ultricies. Nunc ut varius ex. Nullam facilisis lobortis ultricies. Maecenas et eleifend purus. Cras eleifend lacus eget nibh tristique malesuada. Donec sit amet eros nulla. Etiam sem tortor, consectetur eu risus vitae, euismod luctus mauris. Nullam aliquam odio nec nisi consectetur aliquam. Nam iaculis imperdiet sagittis. Proin ac dapibus lorem, ut pulvinar ipsum. Suspendisse vitae nisi suscipit, feugiat ex vitae, commodo mauris. Nulla facilisis suscipit enim eget dictum. Aenean ut nisl dui. Nulla at velit augue. Mauris ultrices tellus nisl, at bibendum arcu hendrerit ut. Donec in mollis quam, sed congue justo. Duis tempus enim sit amet est dignissim fermentum. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Fusce malesuada orci id velit pellentesque, ut pretium massa fermentum. Aenean commodo felis et lectus interdum, eget volutpat nulla gravida. Sed ornare lorem nisi, at venenatis libero consectetur vitae. Aenean lobortis enim non ultrices bibendum. Praesent luctus tincidunt libero, quis fermentum ex vehicula quis. Sed quis facilisis nisi, quis malesuada augue.}}
    \lipsum[2-3]
    \newpage
    \hypertarget{target}{THIS IS THE TARGET!}
    %
\end{document}

In this case, as you can see, the hyperlink is set for every single character, but still there is an other hyperlink on every line of the "middle page".

Hopefully somebody with more experience than me can find something useful in my experiments and find a final solution that works for everybody.

2
  • well at first that creates lots of annotations and so a much larger PDF. \lipsum is a protected command, that doesn't expand, so your method will not work with it. I doubt that you have a problem only with footnotes at the end of the line. Something like \hyperlink{#1}{\nobreak##1\nobreak} would probably help, but then you run into problems with the spaces and will get unwanted indentations. Commented yesterday
  • I made now a comparison on my work project between what I was doing before and this method and in both cases the PDF weights 2.0 MB. We are talking about a 102 pages long PDF with 58 footnotes, more or less half of which contains hyperlinks. Commented yesterday

You must log in to answer this question.

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