4

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 – Martin Apr 24 '17 at 8:04
4

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.

Open in ShareLaTeX

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

\begin{document}
\begin{frame}
  \makebox[0pt][l]{%
    \begin{ocg}[radiobtngrp=myBullets]{Bul01r}{ocg01r}{1}\showocg{ocg01g}{\textcolor{red}{\textbullet}}\end{ocg}%
  }%
  \makebox[0pt][l]{%
    \begin{ocg}[radiobtngrp=myBullets]{Bul01g}{ocg01g}{0}\showocg{ocg01b}{\textcolor{green}{\textbullet}}\end{ocg}%
  }%
  \makebox[0pt][l]{%
    \begin{ocg}[radiobtngrp=myBullets]{Bul01b}{ocg01b}{0}\showocg{ocg01x}{\textcolor{blue}{\textbullet}}\end{ocg}%
  }%
  \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}" – Martin 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? – AlexG Apr 24 '17 at 14:32
  • I am using MikTeX – Martin 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. – AlexG Apr 24 '17 at 14:40
  • I added a link to ShareLaTeX, where you can download the PDF. – AlexG Apr 24 '17 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.