3

I'd want to create a "Guess who?". It would consist in guessing who did an action, e.g. "I love Belgian fries", and below this I'd like to put a hidden name. I know the \phantom command, but I'd also like to give the solution to the readers. And so I'm wondering if it exists a command like \hiddenUntilUserClicksOnIt{some text} or anything else like that.

I hope my question is understandable and I already thank you!

EDIT: all your solutions look great, but it seems that I've got another problem: I'm working on Overleaf, and nothing happened when I copy-pasted the code suggested below.. Do you know if the ocg's packages work on MacTeX?

EDIT2: This is the code I used as example below, and it didn't work; however, @AlexG, your code worked well.

\documentclass{article}
\usepackage[]{ocgx2}
\usepackage{tikz}
\usepackage{hyperref}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%       OCG latex packages which these examples are based from include:
%       ocgx2 - https://github.com/agrahn/ocgx2     (C) 2015--\today Alexander Grahn under LPPL 1.3c
%       ocgx - https://www.ctan.org/pkg/ocgx        (C) 2012 by Paul Gaborit under LPPL 1.3c
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\title{Layers via OCG: Examples}
For the layers in this document, Overleaf's renderer does not support the layers and hence renders all layers in the preview.  The layers' features can be accessed through the PDF and layer visibility options in your PDF viewer.  If your PDF viewer does not support optional content groups (layers), then layers set to \{0\} or \{off\} are not displayed.  Most of these examples are taken (and modified slightly) from the documentation for the ocg package and the variants thereof. The version used here is ocgx2.  As far as I can tell, OCGtools is not a supported package within Overleaf (yet).

\vspace{2cm}

\begin{ocg}{First layer}{oc1}{on}
The first Layer is visible at start.
\end{ocg}
\begin{ocg}{Second layer}{oc2}{off}
The second layer is not visible at start.
\end{ocg}
\begin{ocg}{Third layer}{oc3}{1}
The third layer is visible at start.
\end{ocg}
This text is not inside of a layer and always visible.

\vspace*{2cm}
\begin{tikzpicture}[node distance=3cm, state/.style={fill=green!20},auto]
\begin{ocg}{grid}{ocgridid}{1}
\draw[black!20] (-1,-1) grid (4,2);
\end{ocg}
\begin{ocg}{states}{ocstatesid}{1}
\node[state] (q_a) {$q_a$};
\node[state] (q_b) [right of=q_a] {$q_b$};
\end{ocg}
\begin{ocg}{edges}{ocedgesid}{1}
\path[->]
(q_a) edge node {0} (q_b)
edge [loop above] node {0} ()
(q_b) edge [loop above] node {1} ();
\end{ocg}
\end{tikzpicture}

\vspace*{2cm}
  \newlength{\textlength}
  \settowidth{\textlength}{This text is written in blue.}
The following text can be toggled:
\switchocg{ocblueid ocredid}{%
\begin{ocg}{red text}{ocredid}{1}
\textcolor{red}{\ This text is written in red.}
\end{ocg}%
\begin{ocg}{blue text}{ocblueid}{0}
\hspace{-\the\textlength}\textcolor{blue}{This text is written in blue.}
\end{ocg}%
}
And now the text continues.

\vspace{2cm}
  \settowidth{\textlength}{\includegraphics[width=2cm]{facebook.png} www.facebook.com}
We also have that images can be toggled as well.
\begin{center}
\switchocg{in fb}{%
\begin{ocg}{facebook}{fb}{on}
\includegraphics[width=2cm]{facebook.png} www.facebook.com
\end{ocg}%
\begin{ocg}{linked in}{in}{off}
\hspace{-\the\textlength}\includegraphics[width=2cm]{linkedin.png} www.linkedin.com
\end{ocg}%
}
\end{center}

    \end{document}
VaSaV
  • 31
  • 1
    It perfectly does! From first shot, look at ocg, ocg-x and ocg2x - it creates "layers" in your pdf and defines buttons. When you click on button, you reveal the text. Simple as that!! ;) – Tomáš Kruliš Dec 14 '19 at 19:03
  • @VaSaV You must download the PDF from Overleaf and open it locally in a good PDF viewer, such as Acrobat Reader or Evince. What you see in the Web browser is only a preview. MacTeX can be used, of course, for making the PDF; MacTeX is derived fom TeXLive. Make sure all packages are up-to-date. Run MacTeX's update utility every now and then. – AlexG Dec 15 '19 at 20:40
  • I've downloaded Acrobat Reader, but even with that the text which should be invisible is visible. I copy-pasted the code from this: https://www.overleaf.com/project/5df6b2f79f011e00014ae986 but ocg isn't working. – VaSaV Dec 15 '19 at 22:48
  • @VaSaV : The link is not accessible for me. Check the permissions. – AlexG Dec 16 '19 at 10:18
  • @VaSaV : I copy-pasted the code of my answer to Overleaf, and it works as advertised (after download and open in Acrobat Reader): https://www.overleaf.com/read/dxmnhdxhkqbj – AlexG Dec 16 '19 at 11:31
  • And btw, thank you a lot for your patience! – VaSaV Dec 16 '19 at 12:56
  • @VaSaV Your code works for me.: https://www.overleaf.com/read/mxkdzrkfqgtr (Though I had to use different image files, for the lack of those used in your example.) – AlexG Dec 16 '19 at 13:37
  • It works! Thank you :D – VaSaV Dec 16 '19 at 13:54

1 Answers1

7

Interesting things, such as quizzes, can be achieved with PDF Layers. See ocgx2: verify and reset of Multiple Choice Questions with OCGs for a more advanced example.

The most basic set-up is perhaps:

\documentclass{article}

\usepackage{ocgx2} %implements PDF Layers
\usepackage{hyperref}

\begin{document}

\switchocg{myLayer}{Click to show/hide PDF Layer} % toggle layer visibility

% "My Layer"->name shown in the "Layers" panel of the PDF viewer
% "myLayer"->label for referencing the layer in the document's TeX source
% "off"->initial visibility
\begin{ocg}{My Layer}{myLayer}{off}
This is the Layer's content.
\end{ocg}

\end{document}
AlexG
  • 54,894