# Tikz & OCG: make button cycling through different layers

I would like to have an OCG button which, when pressed, cycles through a set of OCG layers.

I've tried using self-referencing OCG buttons, where each toggled the other (I tried with and without radiobtngrp). The main problem is that even when toggled off, the clickable areas still persist, so clicking can only toggle the uppermost button. Here is an example with 2 layers (but I'd like to have more if possible):

\documentclass{article}

\usepackage[tikz]{ocgx2}
\tikzstyle{button}=[minimum width=15mm, rounded corners,  draw=white!50!black!100,  bottom color=white]

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{ocg}[radiobtngrp=myRadioButtons]{OCG 0}{1}{1}
\node[button, hide ocg=1, show ocg=2] (n1) at (0,0) {$Button 1$};
\end{ocg}
\begin{ocg}[radiobtngrp=myRadioButtons]{OCG 1}{2}{0}
\node[button, hide ocg=2, show ocg=1] (n2) at (0.5,0){$Button 2$};
\end{ocg}
\end{tikzpicture}
\end{center}
\end{document}


Is it possible to make this work? Alternatively, would it be possible to create a single button with a counter associated to it that increments each time it is clicked, each time toggling on a particular layer?

• I didn't quite understand your question. What should happen when you click on button 1? What about button 2? – AndréC Dec 4 '19 at 17:55
• @Svalorzen It works as intended in Acrobat Reader: the clickable area is hidden together with the button caption, such that you can place both buttons at the same coordinate. All other PDF viewers are defective in this regard. Which one do you use? – AlexG Dec 4 '19 at 20:51
• @AndréC Ideally, you click on button1, which disappears leaving in its place button2; when you click on that it would disappear and show button3, and so on; until some "last" button which when clicked disappears leaving button1 in its place. – Svalorzen Dec 5 '19 at 11:04
• @AlexG I'm using evince 3.28.4, as I am on linux. Tried the Firefox pdf viewer as well but there ocg does not even work unfortunately. – Svalorzen Dec 5 '19 at 11:05
• This particluar example of toggling between two OCGs is indeed possible for Evince as PDF viewer. See second code block in my answer. – AlexG Dec 11 '19 at 15:35

## 1 Answer

Acrobat Reader is currently the only PDF viewer that correctly hides the mouse-sensitive area of an interactive element (link, form field, ...) together with its typeset content which are placed on a PDF Layer that is about to be disabled.

According to the PDF specification, PDF Annotations are made Layer-aware by adding an /OC <ocg object ref> entry to the Annotation dict. Pkg ocgx2 does this automatically for its Layer-switching commands that are placed on PDF Layers. All PDF viewers except AR seem to ignore this entry, which is clearly a bug of those viewers.

Correct implementation of this feature in PDF viewers allows one to cycle through multiple PDF Layers by means of Layer-switching links that are located at the same place on the page, as in the following example:

\documentclass[margin=5]{standalone}

\usepackage[tikz]{ocgx2}
\tikzstyle{button}=[minimum width=15mm, rounded corners,  draw=white!50!black!100,  bottom color=white]

\begin{document}

\begin{tikzpicture}
\begin{scope}[ocg={name=OCG 1, ref=1, visibility=on, opts={radiobtngrp=myRadioButtons}}]
\node[button, show ocg=2] (n0) at (0,0) {Button 1};
\end{scope}
\begin{scope}[ocg={name=OCG 2, ref=2, visibility=off, opts={radiobtngrp=myRadioButtons}}]
\node[button, show ocg=3] at (n0) {Button 2};
\end{scope}
\begin{scope}[ocg={name=OCG 3, ref=3, visibility=off, opts={radiobtngrp=myRadioButtons}}]
\node[button, show ocg=4] at (n0) {Button 3};
\end{scope}
\begin{scope}[ocg={name=OCG 4, ref=4, visibility=off, opts={radiobtngrp=myRadioButtons}}]
\node[button, show ocg=1] at (n0) {Button 4};
\end{scope}
\end{tikzpicture}

\end{document}


For other viewers than AR, such as Evince, at most 2 layers could be toggled by a single button. This button would not need to be placed on a layer itself:

% example with 2 PDF Layers that works in Evince
\documentclass[margin=5]{standalone}

\usepackage[tikz]{ocgx2}
\tikzstyle{button}=[minimum width=15mm, rounded corners,  draw=white!50!black!100,  bottom color=white]

\begin{document}

\begin{tikzpicture}
\node[button, switch ocg={1, 2}] (n0) at (0,0) {\phantom{Button 1}};
\begin{scope}[ocg={name=OCG 1, ref=1, visibility=on}]
\node[minimum width=15mm] at (n0) {Button 1};
\end{scope}
\begin{scope}[ocg={name=OCG 2, ref=2, visibility=off}]
\node[minimum width=15mm] at (n0) {Button 2};
\end{scope}
\end{tikzpicture}

\end{document}

• +1 Beautiful :-) – AndréC Dec 11 '19 at 14:33
• Do you think it would be possible to do if I removed the requirements to have the button themselves be the OCG? As in, is it possible to have a button that cycles through some OCG layers, which are separate from the button (and somewhere else)? – Svalorzen Dec 11 '19 at 14:45
• Without resorting to JavaScript, a single button (without the need to be located on a Layer itself) could toggle between two layers at most. I try to construct an example. For multiple Layers you would have to place the buttons at different locations (to work in other PDF viewers); but this would undermine your original intent of a visually single button. – AlexG Dec 11 '19 at 15:01
• @Svalorzen Ok, simplified example with two layers for Evince added. – AlexG Dec 11 '19 at 15:16