4

I am trying to display different boxes with a logo in it based on which PDF layers are active. See the image below

enter image description here

In my example I only have 3, but for my actual problem I am guessing I need 3 groups. I want to switch the box based on three settings:

  • Language = {English|Bokmål|Nynorsk|Samisk},
  • Color = {Black, Color},
  • Secret = {True | False}

Only one setting in each group will be active at a time. My problem is that I am not able to stack the layers and they appear in sequential order. Is there a way to make sure every layer is on top of each other, and to construct the three groups in a manageable fashion.

MWE

\documentclass[12pt]{article}

\usepackage[samin, norsk, nynorsk, british]{babel}
\usepackage[utf8]{inputenc} % usually not needed (loaded by default)
\usepackage[T1]{fontenc}

\usepackage[tikz]{ocgx2}

\newcommand{\UNItitlebox}[1]{%
  \begin{center}
    \setlength{\fboxsep}{0pt}
    \fbox{%
      \begin{minipage}{\dimexpr\textwidth-2\fboxrule}
        \vspace{1pt}
        \hrule height 1pt
        \vspace{1.2em}
        \noindent
        \mbox{}\quad\includegraphics[height=1.69cm,width=12.8cm]{#1}\\[5pt]
          \mbox{}\quad MAT-1001\quad Calculus\hfill English\quad\mbox{}%
          \\[5pt]
          \mbox{}\quad Exam\hfill 23/09/2019\quad\mbox{}%
          \vspace{1em}
          \hrule height1pt
          \vspace{1pt}
        \end{minipage}%
      }
    \end{center}%
}

% Hyperlenker og klikkbare lenker.
\RequirePackage{hyperref}

\hypersetup{
  colorlinks=true,        % false: boxed links; true: colored links
  linkcolor=black,        % color of internal links (change box color with linkbordercolor)
}

\begin{document}

\section{Introduction}

\begin{ocg}{OCG 1}{ocg1}{1}
  \noindent
  \actionsocg{}{ocg1}{ocg2 ocg3 ocg4}{\color{green!50!black}English}\hfill%
  \actionsocg{}{ocg2}{ocg1 ocg3 ocg4}{Bokmål}\hfill%
  \actionsocg{}{ocg3}{ocg1 ocg2 ocg4}{Nynorsk}\hfill%
  \actionsocg{}{ocg4}{ocg1 ocg2 ocg3}{Samisk}
\end{ocg}

  \vspace{-\baselineskip}
\begin{ocg}{OCG 2}{ocg2}{0}
  \noindent
  \actionsocg{}{ocg1}{ocg2 ocg3 ocg4}{English}\hfill%
  \actionsocg{}{ocg2}{ocg1 ocg3 ocg4}{\color{green!50!black}Bokmål}\hfill%
  \actionsocg{}{ocg3}{ocg1 ocg2 ocg4}{Nynorsk}\hfill%
  \actionsocg{}{ocg4}{ocg1 ocg2 ocg3}{Samisk}
\end{ocg}

  \vspace{-\baselineskip}
\begin{ocg}{OCG 3}{ocg3}{0}
  \noindent
  \actionsocg{}{ocg1}{ocg2 ocg3 ocg4}{English}\hfill%
  \actionsocg{}{ocg2}{ocg1 ocg3 ocg4}{Bokmål}\hfill%
  \actionsocg{}{ocg3}{ocg1 ocg2 ocg4}{\color{green!50!black}Nynorsk}\hfill%
  \actionsocg{}{ocg4}{ocg1 ocg2 ocg3}{Samisk}
\end{ocg}

  \vspace{-\baselineskip}
\begin{ocg}{OCG 4}{ocg4}{0}
  \noindent
  \actionsocg{}{ocg1}{ocg2 ocg3 ocg4}{English}\hfill%
  \actionsocg{}{ocg2}{ocg1 ocg3 ocg4}{Bokmål}\hfill%
  \actionsocg{}{ocg3}{ocg1 ocg2 ocg4}{Nynorsk}\hfill%
  \actionsocg{}{ocg4}{ocg1 ocg2 ocg3}{\color{green!50!black}Samisk}
\end{ocg}

\bigskip

\begin{ocg}{OCG 1}{ocg1}{1}
  \UNItitlebox{example-image-a}
\end{ocg}
  \vspace{-4.7cm}
\begin{ocg}{OCG 2}{ocg2}{0}
  \UNItitlebox{example-image-b}
\end{ocg}
  \vspace{-4.7cm}
\begin{ocg}{OCG 3}{ocg3}{0}
  \UNItitlebox{example-image-c}
\end{ocg}
  \vspace{-4.7cm}
\begin{ocg}{OCG 4}{ocg4}{0}
  \UNItitlebox{example-image}
\end{ocg}

\end{document}
  • 1
    To align stacked layers precisely, use zero-size mboxes instead of vertical adjustment. Use a radio button group to create mutually concealing OCGs; this simplifies the setup of the switching buttons. Try OCMDs to implement the colour change of the button captions. – AlexG Sep 17 '19 at 13:32
7

ocgx2 comes with two features that simplify the present task: Radio Button Groups and OCMDs.

enter image description here

  1. radiobutton groups

    Adding all OCGs to a common radiobutton group (here: radiobtngrp=languages) lets only one OCG be enabled at a time; when enabling another one, the previous one is automatically disabled.

  2. OCMDs (Optional Content Membership Dictionaries)

    OCMDs are another kind of PDF layers besides OCGs, whose visibility depends on the visibility of other OCGs in the document during viewing time. Instead of being directly set by user interaction, such as mouse clicks, their visibility is calculated based on visibility expressions. They are created with the ocmd environment. Here, OCMDs are used to make layers for the green and black button captions in the top row, denoting the "on" and "off" states.

The code was further simplified by using zero-width mbox-es to accurately overlay typeset material.

\documentclass[12pt]{article}

\usepackage[samin, norsk, nynorsk, british]{babel}
\usepackage[utf8]{inputenc} % usually not needed (loaded by default)
\usepackage[T1]{fontenc}

\usepackage[tikz]{ocgx2}

\newcommand{\UNItitlebox}[2]{% #1: logo, #2: language
  \frame{%
    \begin{minipage}{\textwidth}\centering
      \vspace{1pt}
      \hrule height 1pt
      \vspace{1.2em}
      \includegraphics[height=1.69cm,width=12.8cm]{#1}\\[5pt]
      \makebox[12.8cm]{MAT-1001\quad Calculus\hfill #2}\\[5pt]
      \makebox[12.8cm]{Exam\hfill 23/09/2019}\\[5pt]
      \vspace{1em}
      \hrule height 1pt
      \vspace{1pt}
    \end{minipage}%
  }
}

% Hyperlenker og klikkbare lenker.
\usepackage{hyperref}

\begin{document}

\section{Introduction}

{%
  \noindent\hypersetup{pdfborder=0 0 0}% locally suppress link borders
  \begin{ocmd}{\AllOff{ocg1}}\makebox[0pt][l]{English}\end{ocmd}% black, "on" if ocg1 is "off"
  \showocg{ocg1}{%
    \begin{ocmd}{\AllOn{ocg1}} % green if ocg1 "on"
      \color{green!50!black}English
    \end{ocmd}}\hfill
  %  
  \begin{ocmd}{\AllOff{ocg2}}\makebox[0pt][l]{Bokmål}\end{ocmd}% black, "on" if ocg2 is "off"
  \showocg{ocg2}{%
    \begin{ocmd}{\AllOn{ocg2}} % green if ocg2 "on"
      \color{green!50!black}Bokmål
    \end{ocmd}}\hfill
  %  
  \begin{ocmd}{\AllOff{ocg3}}\makebox[0pt][l]{Nynorsk}\end{ocmd}% black, "on" if ocg3 is "off"
  \showocg{ocg3}{%
    \begin{ocmd}{\AllOn{ocg3}} % green if ocg3 "on"
      \color{green!50!black}Nynorsk
    \end{ocmd}}\hfill
  %  
  \begin{ocmd}{\AllOff{ocg4}}\makebox[0pt][l]{Samisk}\end{ocmd}% black, "on" if ocg4 is "off"
  \showocg{ocg4}{%
    \begin{ocmd}{\AllOn{ocg4}} % green if ocg4 "on"
      \color{green!50!black}Samisk
    \end{ocmd}}%
}  

\bigskip

\begin{center}
%
\begin{ocg}[radiobtngrp=languages]{OCG 1}{ocg1}{on}
  \makebox[0pt][l]{\UNItitlebox{example-image-a}{English}}
\end{ocg}%
%
\begin{ocg}[radiobtngrp=languages]{OCG 2}{ocg2}{off}
  \makebox[0pt][l]{\UNItitlebox{example-image-b}{Bokmål}}
\end{ocg}%
%
\begin{ocg}[radiobtngrp=languages]{OCG 3}{ocg3}{off}
  \makebox[0pt][l]{\UNItitlebox{example-image-c}{Nynorsk}}
\end{ocg}%
\begin{ocg}[radiobtngrp=languages]{OCG 4}{ocg4}{off}
  \UNItitlebox{example-image}{Samisk}
\end{ocg}
\end{center}

\end{document}
  • Excellent answer! Just for curiosity's sake, what is the bug on the CTAN version? Also, how would I go about adding a second (or third OCMD?) group to the document? – N3buchadnezzar Sep 17 '19 at 16:28
  • @N3buchadnezzar Three stupid missing backslashes, causing initial visibility of re-opened OCGs always reset to "off". The diff: gitlab.com/agrahn/ocgx2/commit/… – AlexG Sep 17 '19 at 21:04
  • @N3buchadnezzar You can add as many OCMDs as you desire, with complex visibility logic. Which application do you have in mind? – AlexG Sep 17 '19 at 21:08
  • 1
    I was able to figure it out myself! It works wonderfully – N3buchadnezzar Sep 18 '19 at 7:59

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.