# Is there an easy way to add hover text to all incidents of math mode where the hover text would contain the LaTeX code?

Adding hover text (I believe the technical term is a tooltip) to any part of any LaTeX document can be accomplished using cooltooltips.sty, as I have recently learned.

I would like to do something a little more large scale. I would like to compile my documents so that every instance of math mode or math display mode automatically puts the defining LaTeX code as a tooltip (text hovering over it). So for example, my nicely typeset $\frac{a}{b}$ can also show the user that very same code: "\frac{a}{b}".

Ideally, I'd also like this to apply to math mode environments like align.

I have two reasons for wanting to do this:

1. Math accessibility to the visually impaired is becoming a big issue. Some universities have been sued in the last few years for not complying with the Americans with Disabilities act because visually impaired students have little or no way to read math - be it LaTeX, MathML, MS Equation Editor, or whatever. Rather than wait 20 years for a screen reader that can accurately verbalize math on a pdf, I want to let screen readers verbalize the LaTeX code and let the visually disabled student interpret that code. It is an effective way to communicate math to sight-affected students as long as we're not talking about something intense like nested fractions.
2. For teaching others what code was used to typeset something, this would be a nice tool to have.

It doesn't look like there is a package for doing this after a search on CTAN. Does anyone have any ideas?

• +1: I really am happy to see any efforts directed towards improving accessibility. I am working in the field and at the time being the support for screen readers, etc. is paltry. It is a real shame, because it is not a question of missing technology, but simply of good-will... Oct 22, 2011 at 10:02
• A related question http://tex.stackexchange.com/questions/19279/latex-accessibility I also wonder if anyone knows anything about converting TeX to braille Oct 22, 2011 at 19:34
• The axessibility package generates hidden comments from which you can copy and paste the LaTeX source. Unfortunately it doesn't work for math enclosed in dollar signs, but they provide scripts for dealing with this and for macro expansion and input commands. Apr 29, 2019 at 13:07

The fancytooltip package provides a bit more advanced capabilities than does the cooltooltips package. As an example, the second paragraph of the package documentation shows the inclusion of regular (fixed) tooltip text, as well as animated tooltip text on mouseover.

The documentation states that it is limited to the (free) Adobe Reader or Adobe Acrobat suite. This restriction may be prohibitive, but Adobe's integration with web browsers counts towards it's abundant usage. Additionally, since the tooltips may be "fancy", they are typically created in a separate document from your source.

Another alternative would be to use the pdfcomment package that provides similar functionality within your source document. Here is a minimal example of how it may be used within an align environment of amsmath:

\documentclass{article}
\usepackage{pdfcomment}% http://ctan.org/pkg/pdfcomment
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{mathtools}% http://ctan.org/pkg/mathtools
\begin{document}
% \pdftooltip{<text>}{<tooltip>}
\begin{align*}
\mathrlap{\pdftooltip[mathstyle=\displaystyle]{\phantom{\sum_{i=1}^n i=\frac{n}{2}(n+1)}}{sum\string_\{i=1\}\string^n i=tfrac\{n\}\{2\}(n+1)}}%
\sum_{i=1}^n i &= \tfrac{n}{2}(n+1) \\
\mathrlap{\pdftooltip[mathstyle=\displaystyle]{\phantom{E=mc^2}}{E=mc\textasciicircum 2}}%
E &= mc^2
\end{align*}
\end{document}


Since the internal align structure cannot be broken across & within the \pdfcomment, it is possible to use some mathtools overlap magic (via \mathrlap).

For completeness, here is an elaboration on the functionality provided by the cooltooltips package, since it may be useful to others. It provides

\cooltooltip[<popup color>][<link color>]{<subject>}{<message>}{<url>}{<tooltip>}{<text>}


that prints a box of colour <link color> around <text>. Additionally, a popup of colour <popup color> is displayed with a title <subject> and text <message> Hovering over <text> also brings up the tooltip <tooltip> and clicking the link takes you to <url>. Here's a very minimal example illustrating this:

\documentclass{article}
\usepackage{cooltooltips}% http://ctan.org/pkg/cooltooltips
\begin{document}
\section{Introduction}
Have you ever wondered what is \cooltooltip[0 0 1]{Einstein}{E=mc^2}%
{http://en.wikipedia.org/wiki/Albert_Einstein}{Einstein on the web}{$E=mc^2$}?
\end{document}


The implementation of cooltooltips is fairly limited and fragile. For example, movement of the popup is not possible since it is activated/deactivated by means of the hover. Also, depending on the resolution of view, it may actually impede viewing. Popups can be disabled by the viewer by incorporating a "toggle button" (say <text>)in the document by using

\cooltooltiptoggle{<text>}


Pressing <text> suppresses all popups in the document. Pressing it again re-enables popups. Some control over the display of border in the actual text is possible by setting the lengths \fboxrule and \fboxsep via a \setlength command.

• Is the "no-movement" a limitation of pdf or of the package? Oct 22, 2011 at 5:24
• In my elementary investigations using cooltooltips, I think it is of the package. I haven't looked at the code to understand how the popups are rendered. However, when viewing the pdfcomment package documentation, similar popup boxes are moveable since they do not appear/disappear on moveover.
– Werner
Oct 22, 2011 at 5:35
• @Werner Thanks for your response. I did not know about pdfcomment. Am I right to think that I would still need to write some sort of script to achieve what I want most? That is, to take a preexisting tex file and convert it so that all instances of math mode will have the tooltip text without manually changing each line of math mode code? I am now imagining a process tex=>access_tex=>pdf where access_tex is some kind of conversion script. Oct 22, 2011 at 17:06
• @alex.jordan: As far as I can tell, yes. The use of annotations in PDF is very fragile, and something that is completely different from the commands used in (La)TeX. That's why you cant merely include it in align due to the "column alignment" &. Moreover, some trickery (as in my example with \mathrlap from mathtools) might be involved to parse align code into pdfcomment examples. However, if the rules of the document structure are well-defined, access_tex may work in some general cases as well.
– Werner
Oct 22, 2011 at 17:20

This does not directly answer your question, but it is a possible (partial) solution of the accessibility problem that you list as one of your reasons for wanting tooltips.

I simply provide two versions of each document: I run pdflatex on my document to produce a pdf version, and htlatex to produce an xhtml version with mathml mathematics. As far as I know, screen readers should be able to handle the xhtml+mathml version. For the students whose browser does not support the mathml, or who do not have the appropriate math fonts installed on their system, I provide link to the pdf file using the following macro:

\newcommand{\pdflink}[1][Printable version of this document]{%
\ifpdf\relax\else\HCode{<a href="\jobname.pdf">#1</a>}\fi}


Then on the top of the document I put \pdflink. When processed by pdflatex, this will be ignored, but it will include a link to the pdf version when processed by htlatex.

• Thank @Jan. Our accessibility staff is running experiments with different screen readers and documents produced in different ways. So far I haven't seen a decent reading of mathematical content, but I think they haven't gotten to an xhtml+mathml version yet. Oct 25, 2011 at 0:20
• You may also use tex4ht with jsmath option - this converts into html, renders mathematics and if you hover the formula, you can see the TeX code and copy it to clipboard. May 13, 2012 at 18:14