[Tex/LaTex] Forming a “spoiler text” environment in LaTeX (via Javascript?)

javascript

I've been writing a mathematical text for some time, and seeing as it is the 21st century, I would like this text to be interactive and dynamic, probably in PDF format. The main function that I am looking to implement is a sort of "spoiler text" environment where the user can click a button to reveal content that is otherwise collapsed and unseen. This could be used to hide answers to exercises or proofs for theorems, etc.

I am led to believe that this is possible by using some form of Javascript embedded in LaTeX. Can somebody point me to a reference that would allow me to implement this, or better yet, explain to me how I should design this "spoiler text" environment?

Best Answer

Ok, this is an ugly hack, but if nothing else works, well, this will work. This is from The PracTEX Journal, 2006, No. 4, Article revision 2006/11/03, Hypertext capabilities with pdfTEX by Federico Garcia.

\pdfannot width w height h depth d { /Subtype /Text
/Contents (htexti)}

This is via pdfTEX primitives, so I have no idea what is going on with it. It makes a postit note in the document that you click on to see the text.

Instructions:

The three dimensions w, h, and d are all LATEX dimensions. But the one that is important is h (the height), because it determines where the note appears in relation to the text baseline. It is also a good idea use a \qquad after it.

Also note: This works fine in Adobe. It doesn't work at all in Sumatra PDF. Well, the note shows up, but you can't click it. So this is a bit risky (So is javascript for that matter, as a lot of PDF readers avoid it for security reasons).

A bit more from the article:

One thing to have in mind with post-it notes is that their exact behavior (color, size, when it opens, how it closes, etc.) is not very much standardized, and tends to change from version to version of Acrobat Reader. That said, the note can be of a different color if, between the brackets of \pdfannot, /C [r g b] is appended; a title for the note is determined with /T (title), and the note can show up open by default with /Open true.

Well, that is one way. A terrible, terrible way, but it would technically work.