can anybody tell me how to apply a yellow background color on pdf links (especially on hovering the mouse over the link). For example, I would like to have the same yellow background effect that appears for example on the Contents section of this pdf (pdfcomment docs). http://linorg.usp.br/CTAN/macros/latex/contrib/pdfcomment/doc/pdfcomment.pdf
[Tex/LaTex] Getting Yellow Background color on link
colorpdfcomment
Related Solutions
Two types of macros are defined. The \plaintt
breaks at line endings (without any additional hyphen character added). The other one \highlighttt
allows breaks only at dots, and colors the text. No break can occur in between dots.
Update: I have made the thing a bit customizable. Let me comment on the difference between the two macros: \highlighttt
has the disadvantage or advantage that breaks occur only at dots. But it has the big advantage that somewhat arbitrary TeX code in-between the dots can be used. See the first paragraph in the example, where I highlighted one sub-piece in blue.
The second macro \plaintt
accepts basically only letters and punctutations, but anything can be escaped from it by just putting it within braces. This is illustrated in the second paragraph.
A hook is provided so that also \plaintt
can do some highlighting, character per character. See the third paragraph where only \plaintt
is used, but the hook has been set to use the yellow box highlighting method.
The highlighting method for \highlighttt
is specified in the command \myhighlightmethod
.
Only the use of the txtt
font is hardcoded in the two macros, but of course this could be changed.
\documentclass{article}
%\usepackage[T1]{fontenc}
\usepackage[textwidth=12cm]{geometry}
%\usepackage{xcolor}
\usepackage{color}
% ``private macros''
\makeatletter
\def\@highlightttpeeknext{\futurelet\@nexttoken\@highlightttaux}
\def\@highlighttt #1.{%
\def\@highlightttaux{\ifx\@nexttoken\egroup
\myhighlightmethod {#1}\else
\myhighlightmethod {#1.}\linebreak[2]%
\expandafter\@highlighttt\fi}%
\@highlightttpeeknext}
\def\@plaintt {\futurelet\@nexttoken\@plainttaux}
\def\@plainttaux {\ifx\@nexttoken\egroup\else
\ifx\@nexttoken\bgroup
\expandafter\expandafter\expandafter\@plaintta\else
\expandafter\expandafter\expandafter\@plainttb\fi\fi}
\def\@plaintta #1{{#1}\@plaintt}
\def\@plainttb #1{\ifcat\@nexttoken a\penalty\hyphenpenalty \plaintthook
#1\else \plaintthook{#1}\linebreak[2]\fi\@plaintt}
% ``commands''
\newcommand{\highlighttt}[1]{{\fontfamily{txtt}\selectfont
\@highlighttt #1.}}
\newcommand\plaintt{\bgroup\fontfamily{txtt}\selectfont
\afterassignment\@plaintt\let\next= }
\makeatother
% ``customization''
\newcommand{\myhighlightmethod}[1]{\fboxsep0pt\colorbox{yellow}{\strut#1}}
\newcommand{\plaintthook}{}
\begin{document}\thispagestyle{empty}
\begin{description}
\item In \plaintt{./build/org.eclipse.cdt.managedbuilder.core/plugin.xml}
there is an extension \highlighttt{org.eclipse.cdt.{\color{blue}managedbuilder}.language.settings.providers.GCCBuildCommandParser}
corresponding to the name \plaintt{GCCBuildOutputParser.name}. This is
extending at the point
\highlighttt{org.eclipse.cdt.core.LanguageSettingsProvider}
\item In \highlighttt{./build/org.eclipse.cdt.managedbuilder.core/plugin.xml}
there is an extension \plaintt{org.eclipse.cdt.{\color{blue}managedbuilder}.language.settings.providers.GCCBuildCommandParser}
corresponding to the name \highlighttt{GCCBuildOutputParser.name}. This is
extending at the point
\plaintt{org.eclipse.cdt.core.LanguageSettingsProvider}
\renewcommand{\plaintthook}[1]{\myhighlightmethod{#1}}
\item In \plaintt{./build/org.eclipse.cdt.managedbuilder.core/plugin.xml}
there is an extension \plaintt{org.eclipse.cdt.{\color{blue}managedbuilder}.language.settings.providers.GCCBuildCommandParser}
corresponding to the name \plaintt{GCCBuildOutputParser.name}. This is
extending at the point
\plaintt{org.eclipse.cdt.core.LanguageSettingsProvider}
\end{description}
\end{document}
\tooltip[black]{foo}[blue!50]{Hint: What is the definition of foo?}
If the mouse is moved over 'foo' the tip is shown and it disappears immediately when the mouse is moved out.
In order to drag the tip around, 'foo' must be clicked. The tip remains visible. Move the mouse over the tip, click it and move it around. Click the tip once more to pin it down at the current position.
To hide the tip, wipe over 'foo' with the mouse.
Link text and tip text colours are optional. Besides pdftex
, all common drivers (xetex
, dvips
, ...) are supported.
It is recommended that <tip text>
is not wider than \linewidth
, <tiptext>
should be put into a \parbox
if necessary.
(Code by M. Scharrer from https://tex.stackexchange.com/a/17808 is used to compute initial left-shift for wider tip text boxes.)
%\documentclass[a6paper,12pt]{scrbook}
\documentclass[12pt]{scrbook}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% tooltips with LaTeX v. 2017/11/28
%
% \tooltip[*[*[*[*]]]][<link colour>]{<link text>}[<tip box colour>]{<tip text>}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \tooltip --> draggable tip, visible on mouse-over, hidden on mouse-out
%
% \tooltip* --> draggable tip, toggle visiblity on mouse-over
%
% \tooltip** --> NON-draggable tip, visible on mouse-over, hidden on mouse-out
%
% \tooltip*** --> NON-draggable tip, toggle visiblity on mouse-over
%
% \tooltip**** --> NON-draggable tip, toggle visiblity on mouse-click (Evince!)
%
% Default link colour can be set with
%
% \usepackage[linkcolor=<colour>]{hyperref}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{pdfbase}[2017/03/16]
\usepackage{xparse,ocgbase}
\usepackage{xcolor,calc}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\ExplSyntaxOn
\let\tpPdfLink\pbs_pdflink:nn
\let\tpPdfAnnot\pbs_pdfannot:nnnn\let\tpPdfLastAnn\pbs_pdflastann:
\let\tpAppendToFields\pbs_appendtofields:n
\def\tpPdfXform{\pbs_pdfxform:nnnnn{1}{1}{}{}}
\let\tpPdfLastXform\pbs_pdflastxform:
\ExplSyntaxOff
\makeatletter
\NewDocumentCommand{\tooltip}{%
ssssO{\ifdefined\@linkcolor\@linkcolor\else blue\fi}mO{yellow!20}m%
}{{%
\leavevmode%
\IfBooleanT{#2}{%
%for variants with two and more stars, put tip box on a PDF Layer (OCG)
\ocgbase@new@ocg{tipOCG.\thetcnt}{%
/Print<</PrintState/OFF>>/Export<</ExportState/OFF>>%
}{false}%
\xdef\tpTipOcg{\ocgbase@last@ocg}%
%prevent simultaneous visibility of multiple non-draggable tooltips
\ocgbase@add@ocg@to@radiobtn@grp{tool@tips}{\ocgbase@last@ocg}%
}%
\tpPdfLink{%
\IfBooleanTF{#4}{%
/Subtype/Link/Border[0 0 0]/A <</S/SetOCGState/State [/Toggle \tpTipOcg]>>
}{%
/Subtype/Screen%
/AA<<%
\IfBooleanTF{#3}{%
/E<</S/SetOCGState/State [/Toggle \tpTipOcg]>>%
}{%
\IfBooleanTF{#2}{%
/E<</S/SetOCGState/State [/ON \tpTipOcg]>>%
/X<</S/SetOCGState/State [/OFF \tpTipOcg]>>%
}{
\IfBooleanTF{#1}{%
/E<</S/JavaScript/JS(%
var fd=this.getField('tip.\thetcnt');%
if(typeof(click\thetcnt)=='undefined'){%
var click\thetcnt=false;%
var fdor\thetcnt=fd.rect;var dragging\thetcnt=false;%
}%
if(fd.display==display.hidden){%
fd.delay=true;fd.display=display.visible;fd.delay=false;%
}else{%
if(!click\thetcnt&&!dragging\thetcnt){fd.display=display.hidden;}%
if(!dragging\thetcnt){click\thetcnt=false;}%
}%
this.dirty=false;%
)>>%
}{%
/E<</S/JavaScript/JS(%
var fd=this.getField('tip.\thetcnt');%
if(typeof(click\thetcnt)=='undefined'){%
var click\thetcnt=false;%
var fdor\thetcnt=fd.rect;var dragging\thetcnt=false;%
}%
if(fd.display==display.hidden){%
fd.delay=true;fd.display=display.visible;fd.delay=false;%
}%
this.dirty=false;%
)>>%
/X<</S/JavaScript/JS(%
if(!click\thetcnt&&!dragging\thetcnt){fd.display=display.hidden;}%
if(!dragging\thetcnt){click\thetcnt=false;}%
this.dirty=false;%
)>>%
}%
/U<</S/JavaScript/JS(click\thetcnt=true;this.dirty=false;)>>%
/PC<</S/JavaScript/JS (%
var fd=this.getField('tip.\thetcnt');%
try{fd.rect=fdor\thetcnt;}catch(e){}%
fd.display=display.hidden;this.dirty=false;%
)>>%
/PO<</S/JavaScript/JS(this.dirty=false;)>>%
}%
}%
>>%
}%
}{{\color{#5}#6}}%
\sbox\tiptext{%
\IfBooleanT{#2}{%
\ocgbase@oc@bdc{\tpTipOcg}\ocgbase@open@stack@push{\tpTipOcg}}%
\fcolorbox{black}{#7}{#8}%
\IfBooleanT{#2}{\ocgbase@oc@emc\ocgbase@open@stack@pop\tpNull}%
}%
\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%
\IfBooleanF{#2}{\tpPdfXform{\tiptext}}%
\raisebox{\heightof{#6}+\tdp}[0pt][0pt]{\makebox[0pt][l]{\hspace{\tipshift}%
\IfBooleanTF{#2}{\usebox{\tiptext}}{%
\tpPdfAnnot{\twd}{\tht}{\tdp}{%
/Subtype/Widget/FT/Btn/T (tip.\thetcnt)%
/AP<</N \tpPdfLastXform>>%
/MK<</TP 1/I \tpPdfLastXform/IF<</S/A/FB true/A [0.0 0.0]>>>>%
/Ff 65536/F 3%
/AA <<%
/U <<%
/S/JavaScript/JS(%
var fd=event.target;%
var mX=this.mouseX;var mY=this.mouseY;%
var drag=function(){%
var nX=this.mouseX;var nY=this.mouseY;%
var dX=nX-mX;var dY=nY-mY;%
var fdr=fd.rect;%
fdr[0]+=dX;fdr[1]+=dY;fdr[2]+=dX;fdr[3]+=dY;%
fd.rect=fdr;mX=nX;mY=nY;%
};%
if(!dragging\thetcnt){%
dragging\thetcnt=true;Int=app.setInterval("drag()",1);%
}%
else{app.clearInterval(Int);dragging\thetcnt=false;}%
this.dirty=false;%
)%
>>%
>>%
}%
\tpAppendToFields{\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}
\tooltip[black]{foo}[blue!50]{Hint: What is the definition of foo?}
with non-draggable tooltip: \tooltip**[black]{bar}[blue!50]{Hint: What is the definition of bar?}
\end{document}
Best Answer
[Converting Andrew Kepert's comment to a answer]
Interaction with mouseovers (e.g. a hover colour) is done by the pdf viewer, and will vary from viewer to viewer. For example the pdfcomment.pdf pdf linked above will use yellow mouseover highlights within firefox's built-in pdf viewer but not in Adobe Reader or MacOS X Preview. Interaction within Adobe Reader can be done with forms, javascript, etc but this won't work in other viewers.