# how to add commands before and after each citation item (number) when using natbib with bibtex

I am writing a thesis where I use natbib and bibtex for my citations which are displayed as numbered items with square brackets: e.g. [1], [2,3,4]

Here we go with a MWE of what I currently have:

\documentclass[10pt]{article}
\setlength\parindent{0pt}
\usepackage{hyperref}
\usepackage[square,numbers]{natbib}

\begin{document}

A single citation: \cite{M} \\
Multiple citations: \cite{M,K,T} \\
Page ref. example: \citep[p. 10]{B} \\
As shown by \citet{M,K} or also by \citet{B} \\

\begin{thebibliography}{9}
\bibitem[Me(2006)]{M} Me: My book related to the problem (2006), 145 p.
% The new line is important!

\bibitem[Karl(2005)]{K} Karl: Karl's paper published in some minor proceedings,
a local conferrence organized by his university (2005), 23--25.
% The new line is important!

\bibitem[Test(2013)]{T} Test. A test paper,
Conf. of testing (2013), 99.
% The new line is important!

\bibitem[Someone(2013)]{B} Someone, a nice book,
A good publisher (2013).
% The new line is important!

\end{thebibliography}
\end{document}


Apart from the reference section this generates the following output:

A single citation: [1]
Multiple citations: [1, 2, 3]
Page ref. example: [4, p. 10]
As shown by Me [1], Karl [2] or also by Someone [4]


In view of adding the capability to show a popup with bibliographic information upon hovering with the mouse over each of the reference items (see description here), I am looking for a way to modify the citation commands in order to add this popup functionality via the fancytooltips package.

For commands where I cite a single reference this is easy:

\let\oldcite\cite
\renewcommand{\cite}[1]{\tooltip{\oldcite{#1}}{#1}}


However, when I cite multiple items in one single cite command (or citet, etc.) my approach fails. What I would need is to get access to the command which generates each individual reference (i.e. the number itself with the corresponding link to the references section) and modify it accordingly, i.e.

\let\oldCommandInQuestion\CommandInQuestion
\renewcommand{\CommandInQuestion}{\tooltip{\oldCommandInQuestion{#1}}{#1}}


Where \CommandInQuestion would be the command generating each number inside the square brackets and would pass the citekey as first argument. If at all this exists?!?

I've been trying to decipher what is inside natbib.sty but without success. Further I was looking at the etoolbox package on how I could modify certain commands but my primary issue is that I don't know which is the command in question...if at all it exists.

Thanks a lot for your inputs!

Fab

PS: I am looking for a specific solution for my configuration (natbib and bibtex).

I could partly solve my issue after having found the solution in the fancytooltips.sty package (around line 172). My solution is sightly modified (not using \tooltip*{} but \tooltip{}) as the other didn't properly link the number:

\makeatletter
\def\NAT@citea@mbox{%
\@citea\mbox{\tooltip{\NAT@hyper@{{\citenumfont{\NAT@num}}}}{fancy:cite:\@citeb}}}
\makeatother


However, this does only work when citing using the \cite or \citep commands but not the \citet command...

Edit: This seems to work much better, as it also works for the \citet and the other commands:

\usepackage{etoolbox}
\makeatletter
\patchcmd{\NAT@hyper@}%
{#1}%
{\tooltip{#1}{fancy:cite:\@citeb\@extra@b@citeb}}%
{}{}
\makeatother


Yet, the issue remains that the reference number is not clickable anymore. If I use \tooltip* instead of \tooltip it is again clickable but the area to perform the mouseover is very small.

Edit 2: Finally, to make the button (which is inserted for the mouseover popup functionality) clickable and point to the right location (in the bibliography section), I've done the following (see code below). It basically modifies guts of the fancytooltips package by changing the javascript action executed when pressing the button this package adds to the PDF. On mouseclick it simply jumps to the corresponding reference in the bibliography (this.gotoNamedDest("cite.[ReplaceThisWithCiteKey]";)). While there must be more elegant solutions it works fine in my particular case.

\makeatletter
\patchcmd{\tooltip@NoStar}%
{{\fancy@a}{\dp0}}%
{{\fancy@a}{\dp0}{#2}}%     % passes tooltip key as additional argument
{}{}
\def\fancy@pushButton#1#2#3#4#5#6#7{%
\lower #6\hbox to 0 pt{\hss\expandafter\pushButton\expandafter[\fancy@tooltip@options{#1}{#2}\A{\JS{%
var str = "#7"; this.gotoNamedDest(str.replace("fancy:cite:", "cite."));    % javascript
}}]{#3}{#4}{#5}}}%
\makeatother