[Tex/LaTex] Beautiful table of contents with hyperref

hyperreftable of contents

I really don't like the red box which indicates a link. I would like to have some filled rectangle rendered which shows up when someone hovers. Obviously before the text gets rendered as you cannot see whats written there then.
To get a better sight into my idea I want something like this per link if you hover it:

But a clean white if you do not hover it.
How can I make this happen?

Best Answer

This is only a partial answer, but it is kind of a roadmap how this could be achieved.

First, note that the hyperref package does not offer a "standard way" to do such a thing. It only offers border styles for links (including underlining a link as explained here: How can I have colored and underlined links with hyperref?). One of the reasons for this might be that the PDF standard offers such border styles directly for link annotations, but to obtain a non-border style, you need to use appearance streams and form XObjects instead. The documentation of such streams and objects can be found here: http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf (start at page 612 for appearance streams and page 355 for form XObjects)

What needs to be done is the following: Find out how the PDF output must look like for a link to have a background color (fortunately, appearance streams already offer three different modes, including one for being inactive and one for hovering over the object; thus, the "action logic" you need is already implemented within the PDF standard and you only need to provide the attributes implementing your desired look).

Then change the corresponding part of the hyperref package by redefining some of its commands as in the "automatic" part of this answer: How can I have colored and underlined links with hyperref?

You can find the source code of the hyperref package here: http://www.ctan.org/pkg/hyperref

Since both the PDF standard and the hyperref package are quite huge, it might involve a significant amount of time just to find the right attributes and commands where to plug these things together, though.

In any case, such a solution might not work with every PDF viewer as most of them do not implement everything that is written in the PDF standard. As long as you only use background color, however, it might work for many viewers (but no guarantees given).