Here is a generic solution, implementing the command
\hyperlinkWithTip{<destination>}[<linkcolour>]{<linktext>}[<tipboxcolour>]{<tiptext>}
It is similar to hyperref
's
\hyperlink{<destination>}{<linktext>}
command, except that the new command has an additional 3rd mandatory argument {<tiptext>}
which provides the tip text to be shown when the mouse hovers over the link.
Optional arguments [<linkcolour>]
and [<tipboxcolour>]
do what they are supposed to do.
The example contains two hyperlinks with tip text. One of them makes use of \parbox
to produce multiline tip text.
\documentclass[twoside,a5paper]{book}
\usepackage{hyperref}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% usage:
%
% \hyperlinkWithTip{<destination>}[<linkcolour>]{<linktext>}[<tipboxcolour>]{<tiptext>}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{pdfbase,ocgbase,xparse}
\usepackage{xcolor}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{calc}
\ExplSyntaxOn
\let\tpPdfLink\pbs_pdflink:nn
\let\tpPdfAnnot\pbs_pdfannot:nnnn\let\tpPdfLastAnn\pbs_pdflastann:
\def\tpPdfXform{\pbs_pdfxform:nnnnn{1}{1}{}{}}\let\tpPdfLastXform\pbs_pdflastxform:
\let\tpPdfObj\pbs_pdfobj:nnn\let\tpPdfLastObj\pbs_pdflastobj:
\let\tpPdfAppendToFields\pbs_appendtofields:n
\ExplSyntaxOff
\makeatletter
\NewDocumentCommand{\hyperlinkWithTip}{mO{blue}mO{yellow!20}m}{{%
\leavevmode%
\ocgbase@new@ocg{tipOCG.\thetcnt}{/Print<</PrintState/OFF>>/Export<</ExportState/OFF>>}{off}%
\tpPdfLink{%
/Subtype/Screen%
/AA<<%
/E<</S/SetOCGState/State [/ON \ocgbase@last@ocg]>>%
/X<</S/SetOCGState/State [/OFF \ocgbase@last@ocg]>>%
/U<</S/SetOCGState/State [/OFF \ocgbase@last@ocg]/Next<</S/GoTo/D (#1)>>>>%
>>%
}{{\color{#2}#3}}%
\sbox\tiptext{\fcolorbox{black}{#4}{#5}}%
\edef\twd{\the\wd\tiptext}%
\edef\tht{\the\ht\tiptext}%
\edef\tdp{\the\dp\tiptext}%
\measureremainder{\whatsleft}\tipshift=0pt%
\ifdim\whatsleft<\twd\setlength\tipshift{\whatsleft-\twd}\fi%
\tpPdfXform{\tiptext}%
\raisebox{\heightof{#3}+\tdp}[0pt][0pt]{\makebox[0pt][l]{\hspace{\tipshift}%
\tpPdfAnnot{\twd}{\tht}{\tdp}{%
/Subtype/Widget/FT/Btn/Ff 65537/T (tip:\thetcnt)%
/AP<</N \tpPdfLastXform>>%
/MK<</TP 1/I \tpPdfLastXform/IF<</S/A/FB true/A [0.0 0.0]>>>>%
/OC \ocgbase@last@ocg
}%
\tpPdfAppendToFields{\tpPdfLastAnn}%
}}%
\stepcounter{tcnt}%
}}
\makeatother
\newsavebox\tiptext\newcounter{tcnt}
\newlength{\whatsleft}\newlength{\tipshift}
\newcommand{\measureremainder}[1]{%
\begin{tikzpicture}[overlay,remember picture]
\path let \p0 = (0,0), \p1 = (current page.east) in
[/utils/exec={\pgfmathsetlength#1{\x1-\x0}\global#1=#1}];
\end{tikzpicture}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
Lots of text. \hyperlinkWithTip{test}{Test}{To be explained on the next page} this if you can.
Lots of text. Another \hyperlinkWithTip{testB}{test}{\parbox[b]{0.5\linewidth}{This is a longer text. Don't miss the explanation given on the next page.}} if you wish.
\newpage
\hypertarget{test}{Here's my link destination.}
\hypertarget{testB}{Yet another link destination.}
\end{document}
The MWE is a good starting point. From there I could simplify it further. frontmatter
sets the title in the optional argument of \twocolumn
. Thus \twocolumn[]
is sufficient. The text can be replaced by vertical spaces.
The class can be replaced by the standard article
class.
MWE:
\documentclass{article}
\usepackage{hyperref}
\begin{document}
\twocolumn[]
\null\vfill\newpage
\null\kern.95\textheight
\href{http://tex.stackexchange.com}{This is a very very
very very very very very very very very very very very very very
very very very very very very very very long link.}
\end{document}
If \twocolumn
is called with the optional argument, then macro \@topnewpage
is called, which puts the stuff of the optional argument in a double column float object for the top of the page. Thus also the following triggers the problem instead of \twocolumn[]
:
\twocolumn
\begin{figure*}\end{figure*}
At some later time in the output routine, macro \@combinedblfloats
combines the top double float object with the normal page contents:
\setbox\@outputbox \vbox to\textheight{%
\unvbox\@tempboxa
\vskip-\dblfloatsep
\ifnum \@dbltopnum>\m@ne
\dblfigrule
\fi
\vskip \dbltextfloatsep
\box\@outputbox
}%
As can be seen, \@outputbox
is boxed again in an additional \vbox
.
Thus the box level of the first part of the link is higher by one in comparison to the second page without the double float object.
The following workaround increases the boxing level except for the first page:
\documentclass{article}
\usepackage{hyperref}
\AtBeginShipout{%
\ifnum\value{page}>1 %
\typeout{* Additional boxing of page `\thepage'}%
\setbox\AtBeginShipoutBox=\hbox{\copy\AtBeginShipoutBox}%
\fi
}
\begin{document}
\twocolumn[]
\null\vfill\newpage
\null\kern.95\textheight
\href{http://tex.stackexchange.com}{This is a very very
very very very very very very very very very very very very very
very very very very very very very very long link.}
\end{document}
The workaround also works with the original MWE of the question.
In real documents, it is not trivial to know, which pages would need
how many additional box levels.
A manual way would be using the following snippet, the first two lines quite early, even before \documentclass
, the remaining part should be executed late, here via \AtBeginDocument
.
\RequirePackage{atbegshi}
\AtBeginShipoutInit
\AtBeginDocument{%
\AtBeginShipout{%
\begingroup
\showboxdepth=\maxdimen
\showboxbreadth=\maxdimen
\tracingonline=1 %
\edef\restoreinteractionmode{\interactionmode=\the\interactionmode}%
\nonstopmode
\showbox\AtBeginShipoutBox
\restoreinteractionmode
\endgroup
}%
}
Then the box listings of the output pages can be analyzed to identify the start part of the link and the end part on the next page. Then the number of dots at the line starts need to be compared.
The next example tries a more automatic solution:
\documentclass[5p]{elsarticle}
\usepackage{kantlipsum}
\usepackage{hyperref}
\usepackage{etoolbox}
\makeatletter
\newcount\c@additionalboxlevel
\setcounter{additionalboxlevel}{0}
\newcount\c@maxboxlevel
\setcounter{maxboxlevel}{1}
\patchcmd\@combinedblfloats{\box\@outputbox}{%
\stepcounter{additionalboxlevel}%
\box\@outputbox
}{}{\errmessage{\noexpand\@combinedblfloats could not be patched}}
\AtBeginShipout{%
\ifnum\value{additionalboxlevel}>\value{maxboxlevel}%
\typeout{Warning: maxboxlevel might be too small, increase to %
\the\value{additionalboxlevel}%
}%
\fi
\@whilenum\value{additionalboxlevel}<\value{maxboxlevel}\do{%
\typeout{* Additional boxing of page `\thepage'}%
\setbox\AtBeginShipoutBox=\hbox{\copy\AtBeginShipoutBox}%
\stepcounter{additionalboxlevel}%
}%
\setcounter{additionalboxlevel}{0}%
}
\makeatother
\begin{document}
\begin{frontmatter}
\end{frontmatter}
\kant*[1-4]
\kant[5]
\href{http://tex.stackexchange.com}{This is a very very
very very very very very very very very very very very very very
very very very very very very very very long link.}
\end{document}
Maybe this issue could also be fixed by replacing \box\@outputbox
by \unvbox\@outputbox
in macro \@combinedblfloats
(maybe the vertical glue assignments might change):
\usepackage{etoolbox}
\makeatletter
\patchcmd\@combinedblfloats{\box\@outputbox}{\unvbox\@outputbox}{}{%
\errmessage{\noexpand\@combinedblfloats could not be patched}%
}%
\makeatother
Best Answer
If
colorlinks
is activated, then the color can be disabled by using the current color.
that is provided by packagexcolor
. The setting forpdfborder
enables or disables the annotation frame: