如何隐藏一些文本直到读者点击它?

如何隐藏一些文本直到读者点击它?

我想创建一个“猜猜我是谁?”。它将包括猜测谁做了一个动作,例如“我喜欢比利时炸薯条”,并且我想在此下方放置一个隐藏的名字。我知道命令\phantom,但我还想将解决方案提供给读者。所以我想知道是否存在类似的命令\hiddenUntilUserClicksOnIt{some text}或其他类似的东西。

我希望我的问题能够被理解,并且我已经感谢你了!

编辑:您的所有解决方案看起来都很棒,但我似乎遇到了另一个问题:我正在使用 Overleaf,当我复制粘贴下面建议的代码时什么也没有发生。您知道 ocg 的包是否适用于 MacTeX 吗?

EDIT2:这是我在下面用作示例的代码,但它不起作用;但是,@AlexG,你的代码运行良好。

\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}

答案1

可以使用 PDF 图层来实现一些有趣的事情,例如测验。请参阅ocgx2:使用 OCG 验证和重置多项选择题以获得更高级的示例。

最基本的设置可能是:

\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}

相关内容