# Toggle colors with ocgx

I want to use the ocgx package to create a pdf with a button that changes its color when clicking on it from red to green to invisble to red (and so on). I tried the following:

\documentclass{beamer}
\usepackage{xcolor}
\usepackage{ocgx}
\begin{document}
\begin{frame}
\begin{ocg}{Bul01r}{ocg01r}{1}\actionsocg{ocg01g ocg01r}{}{}{\textcolor{red}{\textbullet}}\end{ocg} \hspace{-0.32cm}
\begin{ocg}{Bul01g}{ocg01g}{0}\actionsocg{ocg01g}{}{}{\textcolor{green}{\textbullet}}\end{ocg}
\end{frame}
\end{document}


But this only gives the changing from red to green and back to red. How can I get a button with three states?

• Do you want the buttons side by side or on the same place?
– knut
Apr 24 '17 at 7:53
• I want them on the same place Apr 24 '17 at 8:04

This is conveniently done with the ocgx2 package which provides Radio Button Groups (ocgenvironment option radiobtngrp=<name>). From all OCGs (PDF Layers) which belong to the same Radio Button Group, only one can be enabled at a time, automatically hiding the previously enabled OCG. This allows building clickable buttons with cyclic dependency of arbitrary cycle length.

The following example enchains red, green, blue, no-colour states.

\documentclass{beamer}
\usepackage{xcolor}
\usepackage{ocgx2}

\begin{document}
\begin{frame}
\makebox[0pt][l]{%
}%
\makebox[0pt][l]{%
}%
\makebox[0pt][l]{%
}%
\begin{ocg}[radiobtngrp=myBullets]{Bul01x}{ocg01x}{0}\showocg{ocg01r}{$\circ$}\end{ocg}%
\end{frame}
\end{document}


(It may be necessary to delete the *.aux first, if the ocgx package was used before.)

The same effect could also be achieved without a radio button group, using the \actionsocg{}{}{}{} command instead of the simpler \showocg{}{}, as in the original post. But again, this requires the ocgx2 package, which ensures that the switching links residing on the different layers are enabled/disabled correctly according to the visibility of the OCG they are associated with:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% version without radio button group, using \actionsocg
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass{beamer}
\usepackage{xcolor}
\usepackage{ocgx2}

\begin{document}
\begin{frame}
\makebox[0pt][l]{%
\begin{ocg}{Bul01r}{ocg01r}{1}\actionsocg{}{ocg01g}{ocg01r}{\color{red}\textbullet}\end{ocg}%
}%
\makebox[0pt][l]{%
\begin{ocg}{Bul01g}{ocg01g}{0}\actionsocg{}{ocg01b}{ocg01g}{\color{green}\textbullet}\end{ocg}%
}%
\makebox[0pt][l]{%
\begin{ocg}{Bul01b}{ocg01b}{0}\actionsocg{}{ocg01x}{ocg01b}{\color{blue}\textbullet}\end{ocg}%
}%
\begin{ocg}{Bul01x}{ocg01x}{0}\actionsocg{}{ocg01r}{ocg01x}{$\circ$}\end{ocg}%
\end{frame}
\end{document}

• I tried both versions but both gave several errors (even after deleting the *.aux file), the first of which is "Undefined control sequence: \sys_if_engine_xetex:T{\bool_gset_true:N\g_ocgxii_dvipdfmx_bool}" Apr 24 '17 at 14:30
• You should update the installed LaTeX packages of your TeX installation. Which one are you using, MikTeX or TeX-Live? Apr 24 '17 at 14:32
• I am using MikTeX Apr 24 '17 at 14:34
• Open the Package Manager. Then select "Update Package Database" or so from the menu. After this, update the packages themselves, selecting the corresponding item from the menu. Apr 24 '17 at 14:40
• I updated MikTeX and everything works fine, thx. I also have another question related to this: Is it possible to place two (or more dots) such that they influence each other (i.e. if Button1 is red and Button2 is green and one of the buttons is clicked Button1 turns green and Button2 turns blue; and likewise for any other combination)? Apr 24 '17 at 15:21