3

I am trying to create a simple document that contains questions with their respective answers "hidden" behind a mouse click. The problem is when I try to implement this with a list with labels other than default (via enumitem). My MWE is below:

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
\usepackage{ocgx}
\usepackage{enumitem}
\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem{ex}{\bf Example}[section]
\newenvironment{mysolution}{\begin{ocg}{OCG\theex\theenumi}{ocg\theex\theenumi}{0}\leavevmode\marginpar{SOLUTION}}{\end{ocg}\hfill\switchocg{ocg\theex\theenumi}{$\Box$}}
\begin{document}
\section{Math}
\begin{ex}
A math question
\begin{enumerate}
\item
What is $1+1$?
\begin{mysolution}
$1+1=2$
\end{mysolution}
\end{enumerate}
\end{ex}
\end{document}

I would like to have [label=\bfseries(\alph*)] as an argument for enumerate, but this causes an issue as \theenumi renames to {\theenumi} (which you can see in the PDF) and ocgx cannot read this; this results in a string of errors. This only seems to be an issue with the enumitem package as it works with alpha labels no problem, but I can't get them bolded. Any suggestions as to what I should try?

1
  • 1
    Welcome to TeX.SE.
    – Mico
    Dec 13 '20 at 7:49
3

Environment option [label=\bfseries(\alph*)] redefines \theenumi from its original meaning \number\c@enumi to \protect \bfseries (\alph {enumi}), which is no more expandable because it introduces a formatting instruction (\bfseries).

However, the ocg environment's name and reference arguments must be expandable. The name is used in the PDF file and appears in the PDF viewer's "Layers" navigation tab, while the reference is internally used for identifying the OCG during the LaTeX run.

In order to ensure its expandability in the ocg arguments, we strip the formatting from the enumeration item counter by using (\alph{enumi}) instead of \theenumi. Furthermore, a checkmark is added to the example to be shown when the solution is unveiled. For this, an ocmd environment (pkg ocgx2) is used in the \switchocg link text argument.

\documentclass{book}

\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb}
\usepackage{ocgx2}
\usepackage{enumitem}
\usepackage{amsthm}

\theoremstyle{definition}
\newtheorem{ex}{\bf Example}[section]
\newenvironment{mysolution}{%
  \begin{ocg}{Example \theex\ (\alph{enumi})}{ocg\theex.\alph{enumi}}{off}
  \marginpar{SOLUTION}%
}{%
  \end{ocg}\hfill%
  \switchocg{ocg\theex.\alph{enumi}}{%
    \begin{ocmd}{\AllOn{ocg\theex.\alph{enumi}}}
      \makebox[0pt][l]{\large\checkmark}
    \end{ocmd}$\Box$%
  }%
}

\begin{document}
\section{Math}
\begin{ex}
A math question
\begin{enumerate}[label=\bfseries(\alph*)]
\item
What is $1+1$?
\begin{mysolution}
$1+1=2$
\end{mysolution}
\end{enumerate}
\end{ex}
\end{document}
1
  • It works, thanks a lot!
    – UBCosmos
    Dec 15 '20 at 5:24

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.