我正在为儿子上大学要参加的能力倾向测验准备一些练习题。有许多示例题。纸质书将问题印在书的前面,将答案印在书的后面。翻页不利于理解问题及其答案。这也使得在限时条件下练习变得困难。
我即将设置一本电子书来练习测试。电子书使用和hyperref
包创建的可选内容组 (OCG) ocgx2
。但是,我无法将正确的多项选择答案的选择与显示解决方案联系起来。MWE 设置如下。
两个问题(Q1 和 Q2)与一段文本相关。每个问题有五个多项选择答案。每个问题都有一个解决方案。每段文本的内容、问题、多项选择答案和解决方案组合都是从外部文件 (DB.csv) 加载的,datatools
并以栅格形式显示在页面上tcolorbox
。多项选择答案与单选按钮相关联,这些单选按钮用于切换特定答案选项是正确还是错误。可以切换选项旁边的单独框以显示解决方案。
目前,解决方案与多项选择题答案的响应是分开切换的。我试图让解决方案在给出正确的多项选择题答案时切换。例如,问题 1 的正确答案是 D。当按下选项 D 的单选按钮时,我希望解决方案显示在相邻的框中。
因此,我的问题是:当该链接需要经过条件测试(这是正确答案)时,如何通过单击一个 ocg(多项选择答案)来显示单独的 ocg(解决方案)。
这是 MWE:
% Adapted from https://tex.stackexchange.com/questions/286280/textfield-and-animateinline
\PassOptionsToPackage{table,x11names,dvipsnames,svgnames}{xcolor}
\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{datatool}
\usepackage{enumitem}
\usepackage{fancyhdr}
\usepackage{filecontents}
\usepackage[a3paper,portrait,left=2cm,right=2cm,top=2cm,bottom=2cm,headheight=30pt]{geometry}
\usepackage{graphics} % \resizebox
\usepackage{ifthen}
\usepackage{ocgx2}
\usepackage[most]{tcolorbox}
\usepackage{xcolor}
\usepackage{hyperref} % \TextField etc.
\hypersetup{
hyperindex=true,
colorlinks=true,
breaklinks=true,
urlcolor= purple,
linkcolor={red!50!black},
citecolor={blue!50!black},
urlcolor={blue!80!black},
linktocpage,
bookmarks=true,
pdftoolbar=true,
pdfmenubar=true,
bookmarksopen=true,
pdftitle={LNAT},
pdfauthor={},
pdfsubject={LNAT},
}
\begin{filecontents*}{DB.csv}
Passage,AQuestion,AChoice1,AChoice2,AChoice3,AChoice4,AChoice5,AAnswer,ASolution,BQuestion,BChoice1,BChoice2,BChoice3,BChoice4,BChoice5,BAnswer,BSolution
"Old MacDonald had a farm. On that farm he had cows, horses, goats and sheep.",How many ruminants did McDonald have on his farm?,0,1,2,3,4,4,"Ruminants have a special stomach for fermentation of plant-based food. A horse is not a ruminant.",How many ungulates did McDonald have on his farm?,0,1,2,3,4,5,"Ungulates are hoofed animals. All McDonald's animals have hooves."
\end{filecontents*}
\newcounter{P}
\setcounter{P}{0}
\newcounter{Q}
\setcounter{Q}{0}
\setlist{label*=\Alph*.,noitemsep,leftmargin=0.9cm}
\setlength\parindent{0pt}
\makeatletter
% patch hyperref's Form producing commands to make them layer-aware
\let\PDFFormRadioOrig\PDFForm@Radio
\def\PDFForm@Radio{\PDFFormRadioOrig\ocgxii@insert@OC}
\makeatother
%alternative check box command for layer switching
\newcommand\layerCheckBox[3]{%
% #1: layer name (as shown in Layers tab),
% #2: layer id,
% #3: initial visibility
\raisebox{-0.6ex}{\resizebox{3ex}{!}{%
\makebox[0pt][l]{\showocg{#2}{$\circ$}}%
\begin{ocg}{#1}{#2}{#3}$\bullet$\end{ocg}%
}%
\hspace{0.1cm}
}}
% This is setting up the header
\chead{}
\cfoot{}
\fancypagestyle{myheader}{
\fancyhead[L]{Passage \arabic{P}}
\fancyhead[R]{\thepage{}}
\renewcommand{\headrulewidth}{0.0pt}
\renewcommand{\headrule}{\hbox to\headwidth{\color{blue}\leaders\hrule height \headrulewidth\hfill}}
}
\pagestyle{myheader}
% Load database
\DTLloaddb{DB}{DB.csv}
\begin{document}
% For each record (line) in database
% Assign field values by name to macros
%\DTLsetseparator{|}
\DTLforeach{DB}{%
\passage=Passage,
\Aquestion=AQuestion,
\Achoicea=AChoice1,
\Achoiceb=AChoice2,
\Achoicec=AChoice3,
\Achoiced=AChoice4,
\Achoicee=AChoice5,
\Aanswer=AAnswer,
\Asolution=ASolution,
\Bquestion=BQuestion,
\Bchoicea=BChoice1,
\Bchoiceb=BChoice2,
\Bchoicec=BChoice3,
\Bchoiced=BChoice4,
\Bchoicee=BChoice5,
\Banswer=BAnswer,
\Bsolution=BSolution%
}{%
\stepcounter{P}
\begin{tcboxeditemize}
[raster rows=5,raster columns=5,raster height=\textheight-2cm,arc=6pt,
raster every box/.style={colframe=red!50!black,colback=red!10!white,coltitle=white,fonttitle=\large\bfseries}] % raster options
%
{colframe=CornflowerBlue!50!white,colback=CornflowerBlue!10!white,arc=6pt} % outer tcolorbox options
\tcbitem[raster multicolumn=2,raster multirow=5,colframe=green!50!black,colback=white,raster height=\tcbtextheight,colbacktitle=white,coltitle=black,title=Passage \arabic{P}]
\passage
%
\tcbitem[raster multicolumn=2,raster multirow=5,blankest,raster height=\tcbtextheight]
%
\begin{tcbitemize}[raster rows=5,raster columns=2,raster height=\tcbtextheight,colbacktitle=white]
% Question 1
\stepcounter{Q}
\tcbitem[raster multicolumn=2,colframe=blue!50!white,colback=white,coltitle=black,fonttitle=\large\bfseries,title=\arabic{Q}. \Aquestion]
\begin{enumerate}
% Question 1 - Choice A
\item [\layerCheckBox{Q\arabic{Q}A}{\arabic{Q}A}{off} A.] \Achoicea \par
\begin{ocg}[radiobtngrp=Choices]{Q\arabic{Q}A}{\arabic{Q}A}{off}%
\ifthenelse{\Aanswer=1}{Correct}{Incorrect}
\end{ocg}%
% Question 1 - Choice B
\item [\layerCheckBox{Q\arabic{Q}B}{\arabic{Q}B}{off} B.] \Achoiceb \par
\begin{ocg}[radiobtngrp=Choices]{Q\arabic{Q}B}{\arabic{Q}B}{off}%
\ifthenelse{\Aanswer=2}{Correct}{Incorrect}
\end{ocg}%
% Question 1 - Choice C
\item [\layerCheckBox{Q\arabic{Q}C}{\arabic{Q}C}{off} C.] \Achoicec \par
\begin{ocg}[radiobtngrp=Choices]{Q\arabic{Q}C}{\arabic{Q}C}{off}%
\ifthenelse{\Aanswer=3}{Correct}{Incorrect}
\end{ocg}%
% Question 1 - Choice D
\item [\layerCheckBox{Q\arabic{Q}D}{\arabic{Q}D}{off} D.] \Achoiced \par
\begin{ocg}[radiobtngrp=Choices]{Q\arabic{Q}D}{\arabic{Q}D}{off}%
\ifthenelse{\Aanswer=4}{Correct}{Incorrect}
\end{ocg}%
% Question 1 - Choice E
\item [\layerCheckBox{Q\arabic{Q}E}{\arabic{Q}E}{off} E.] \Achoicee \par
\begin{ocg}[radiobtngrp=Choices]{Q\arabic{Q}E}{\arabic{Q}E}{off}%
\ifthenelse{\Aanswer=5}{Correct}{Incorrect}
\end{ocg}%
\end{enumerate}
% Question 2
\stepcounter{Q}
\tcbitem[raster multicolumn=2,colframe=blue!50!white,colback=white,coltitle=black,fonttitle=\large\bfseries,title=\arabic{Q}. \Bquestion]
\begin{enumerate}
% Question 2 - Choice A
\item [\layerCheckBox{Q\arabic{Q}A}{\arabic{Q}A}{off} A.] \Bchoicea\par
\begin{ocg}[radiobtngrp=Choices]{Q\arabic{Q}A}{\arabic{Q}A}{off}%
\ifthenelse{\Banswer=1}{Correct}{Incorrect}
\end{ocg}%
% Question 2 - Choice B
\item [\layerCheckBox{Q\arabic{Q}B}{\arabic{Q}B}{off} B.] \Bchoiceb\par
\begin{ocg}[radiobtngrp=Choices]{Q\arabic{Q}B}{\arabic{Q}B}{off}%
\ifthenelse{\Banswer=2}{Correct}{Incorrect}
\end{ocg}%
% Question 2 - Choice C
\item [\layerCheckBox{Q\arabic{Q}C}{\arabic{Q}C}{off} C.] \Bchoicec\par
\begin{ocg}[radiobtngrp=Choices]{Q\arabic{Q}C}{\arabic{Q}C}{off}%
\ifthenelse{\Banswer=3}{Correct}{Incorrect}
\end{ocg}%
% Question 2 - Choice D
\item [\layerCheckBox{Q\arabic{Q}D}{\arabic{Q}D}{off} D.] \Bchoiced\par
\begin{ocg}[radiobtngrp=Choices]{Q\arabic{Q}D}{\arabic{Q}D}{off}%
\ifthenelse{\Banswer=4}{Correct}{Incorrect}
\end{ocg}%
% Question 2 - Choice E
\item [\layerCheckBox{Q\arabic{Q}E}{\arabic{Q}E}{off} E.] \Bchoicee\par
\begin{ocg}[radiobtngrp=Choices]{Q\arabic{Q}E}{\arabic{Q}E}{off}%
\ifthenelse{\Banswer=5}{Correct}{Incorrect}
\end{ocg}%
\end{enumerate}
\end{tcbitemize}
\tcbitem[raster multicolumn=1,raster multirow=5,blankest,raster height=\tcbtextheight]
% These are the solutions
\begin{tcbitemize}[raster rows=5,raster columns=1,raster height=\tcbtextheight]
% Solution to question 1
\tcbitem[colframe=blue!50!white,colback=white]
\hspace{-0.6cm} \layerCheckBox{S\arabic{Q}A}{S\arabic{Q}A}{off}
\begin{ocg}[radiobtngrp=Choices]{S\arabic{Q}A}{S\arabic{Q}A}{off}%
\Asolution
\end{ocg}
% Solution to question 2
\tcbitem[colframe=blue!50!white,colback=white]
\hspace{-0.6cm} \layerCheckBox{S\arabic{Q}B}{S\arabic{Q}B}{off}
\begin{ocg}[radiobtngrp=Choices]{S\arabic{Q}B}{S\arabic{Q}B}{off}%
\Bsolution
\end{ocg}
\end{tcbitemize}
\end{tcboxeditemize}
\clearpage
\stepcounter{P}
}
\end{document}
答案1
为了在单击正确的选择按钮时同时显示,解决方案框中的文本必须与相应的选择按钮放在相同的 OCG 上。在本例中,这些是1D
问题一和2E
问题二的 OCG。
请注意,在原始源代码的\arabic{Q}
“ ”代码部分中,不会扩展为正确的值。因此,所需的值在下面的代码中是硬编码的。% Solution to question ?
此外,应对不同的问题使用单独的单选按钮组(例如radiobtngrp=Choices-1
, ),因为回答问题二不应隐藏对问题一做出的选择。radiobtngrp=Choices-2
\PassOptionsToPackage{table,x11names,dvipsnames,svgnames}{xcolor}
\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{datatool}
\usepackage{enumitem}
\usepackage{fancyhdr}
\usepackage{filecontents}
\usepackage[a3paper,portrait,left=2cm,right=2cm,top=2cm,bottom=2cm,headheight=30pt]{geometry}
\usepackage{graphics} % \resizebox
\usepackage{ifthen}
\usepackage{ocgx2}
\usepackage[most]{tcolorbox}
\usepackage{xcolor}
\usepackage{hyperref} % \TextField etc.
\hypersetup{
hyperindex=true,
colorlinks=true,
breaklinks=true,
urlcolor= purple,
linkcolor={red!50!black},
citecolor={blue!50!black},
urlcolor={blue!80!black},
linktocpage,
bookmarks=true,
pdftoolbar=true,
pdfmenubar=true,
bookmarksopen=true,
pdftitle={LNAT},
pdfauthor={},
pdfsubject={LNAT},
}
\begin{filecontents*}{DB.csv}
Passage,AQuestion,AChoice1,AChoice2,AChoice3,AChoice4,AChoice5,AAnswer,ASolution,BQuestion,BChoice1,BChoice2,BChoice3,BChoice4,BChoice5,BAnswer,BSolution
"Old MacDonald had a farm. On that farm he had cows, horses, goats and sheep.",How many ruminants did McDonald have on his farm?,0,1,2,3,4,4,"Ruminants have a special stomach for fermentation of plant-based food. A horse is not a ruminant.",How many ungulates did McDonald have on his farm?,0,1,2,3,4,5,"Ungulates are hoofed animals. All McDonald's animals have hooves."
\end{filecontents*}
\newcounter{P}
\setcounter{P}{0}
\newcounter{Q}
\setcounter{Q}{0}
\setlist{label*=\Alph*.,noitemsep,leftmargin=0.9cm}
\setlength\parindent{0pt}
% hyperref Form elements not used in this example, un-comment if really needed
%\makeatletter
%% patch hyperref's Form producing commands to make them layer-aware
%\let\PDFFormRadioOrig\PDFForm@Radio
%\def\PDFForm@Radio{\PDFFormRadioOrig\ocgxii@insert@OC}
%\makeatother
%alternative check box command for layer switching
\newcommand\layerCheckBox[3]{%
% #1: layer name (as shown in Layers tab),
% #2: layer id,
% #3: initial visibility
\raisebox{-0.6ex}{\resizebox{3ex}{!}{%
\makebox[0pt][l]{\showocg{#2}{$\circ$}}%
\begin{ocg}{#1}{#2}{#3}$\bullet$\end{ocg}%
}%
\hspace{0.1cm}
}}
% This is setting up the header
\chead{}
\cfoot{}
\fancypagestyle{myheader}{
\fancyhead[L]{Passage \arabic{P}}
\fancyhead[R]{\thepage{}}
\renewcommand{\headrulewidth}{0.0pt}
\renewcommand{\headrule}{\hbox to\headwidth{\color{blue}\leaders\hrule height \headrulewidth\hfill}}
}
\pagestyle{myheader}
% Load database
\DTLloaddb{DB}{DB.csv}
\begin{document}
% For each record (line) in database
% Assign field values by name to macros
%\DTLsetseparator{|}
\DTLforeach{DB}{%
\passage=Passage,
\Aquestion=AQuestion,
\Achoicea=AChoice1,
\Achoiceb=AChoice2,
\Achoicec=AChoice3,
\Achoiced=AChoice4,
\Achoicee=AChoice5,
\Aanswer=AAnswer,
\Asolution=ASolution,
\Bquestion=BQuestion,
\Bchoicea=BChoice1,
\Bchoiceb=BChoice2,
\Bchoicec=BChoice3,
\Bchoiced=BChoice4,
\Bchoicee=BChoice5,
\Banswer=BAnswer,
\Bsolution=BSolution%
}{%
\stepcounter{P}
\begin{tcboxeditemize}
[raster rows=5,raster columns=5,raster height=\textheight-2cm,arc=6pt,
raster every box/.style={colframe=red!50!black,colback=red!10!white,coltitle=white,fonttitle=\large\bfseries}] % raster options
%
{colframe=CornflowerBlue!50!white,colback=CornflowerBlue!10!white,arc=6pt} % outer tcolorbox options
\tcbitem[raster multicolumn=2,raster multirow=5,colframe=green!50!black,colback=white,raster height=\tcbtextheight,colbacktitle=white,coltitle=black,title=Passage \arabic{P}]
\passage
%
\tcbitem[raster multicolumn=2,raster multirow=5,blankest,raster height=\tcbtextheight]
%
\begin{tcbitemize}[raster rows=5,raster columns=2,raster height=\tcbtextheight,colbacktitle=white]
% Question 1
\stepcounter{Q}
\tcbitem[raster multicolumn=2,colframe=blue!50!white,colback=white,coltitle=black,fonttitle=\large\bfseries,title=\arabic{Q}. \Aquestion]
\begin{enumerate}
% Question 1 - Choice A
\item [\layerCheckBox{Q\arabic{Q}A}{\arabic{Q}A}{off} A.] \Achoicea \par
\begin{ocg}[radiobtngrp=Choices-1]{Q\arabic{Q}A}{\arabic{Q}A}{off}%
\ifthenelse{\Aanswer=1}{Correct}{Incorrect}
\end{ocg}%
% Question 1 - Choice B
\item [\layerCheckBox{Q\arabic{Q}B}{\arabic{Q}B}{off} B.] \Achoiceb \par
\begin{ocg}[radiobtngrp=Choices-1]{Q\arabic{Q}B}{\arabic{Q}B}{off}%
\ifthenelse{\Aanswer=2}{Correct}{Incorrect}
\end{ocg}%
% Question 1 - Choice C
\item [\layerCheckBox{Q\arabic{Q}C}{\arabic{Q}C}{off} C.] \Achoicec \par
\begin{ocg}[radiobtngrp=Choices-1]{Q\arabic{Q}C}{\arabic{Q}C}{off}%
\ifthenelse{\Aanswer=3}{Correct}{Incorrect}
\end{ocg}%
% Question 1 - Choice D
\item [\layerCheckBox{Q\arabic{Q}D}{\arabic{Q}D}{off} D.] \Achoiced \par
\begin{ocg}[radiobtngrp=Choices-1]{Q\arabic{Q}D}{\arabic{Q}D}{off}%
\ifthenelse{\Aanswer=4}{Correct}{Incorrect}
\end{ocg}%
% Question 1 - Choice E
\item [\layerCheckBox{Q\arabic{Q}E}{\arabic{Q}E}{off} E.] \Achoicee \par
\begin{ocg}[radiobtngrp=Choices-1]{Q\arabic{Q}E}{\arabic{Q}E}{off}%
\ifthenelse{\Aanswer=5}{Correct}{Incorrect}
\end{ocg}%
\end{enumerate}
% Question 2
\stepcounter{Q}
\tcbitem[raster multicolumn=2,colframe=blue!50!white,colback=white,coltitle=black,fonttitle=\large\bfseries,title=\arabic{Q}. \Bquestion]
\begin{enumerate}
% Question 2 - Choice A
\item [\layerCheckBox{Q\arabic{Q}A}{\arabic{Q}A}{off} A.] \Bchoicea\par
\begin{ocg}[radiobtngrp=Choices-2]{Q\arabic{Q}A}{\arabic{Q}A}{off}%
\ifthenelse{\Banswer=1}{Correct}{Incorrect}
\end{ocg}%
% Question 2 - Choice B
\item [\layerCheckBox{Q\arabic{Q}B}{\arabic{Q}B}{off} B.] \Bchoiceb\par
\begin{ocg}[radiobtngrp=Choices-2]{Q\arabic{Q}B}{\arabic{Q}B}{off}%
\ifthenelse{\Banswer=2}{Correct}{Incorrect}
\end{ocg}%
% Question 2 - Choice C
\item [\layerCheckBox{Q\arabic{Q}C}{\arabic{Q}C}{off} C.] \Bchoicec\par
\begin{ocg}[radiobtngrp=Choices-2]{Q\arabic{Q}C}{\arabic{Q}C}{off}%
\ifthenelse{\Banswer=3}{Correct}{Incorrect}
\end{ocg}%
% Question 2 - Choice D
\item [\layerCheckBox{Q\arabic{Q}D}{\arabic{Q}D}{off} D.] \Bchoiced\par
\begin{ocg}[radiobtngrp=Choices-2]{Q\arabic{Q}D}{\arabic{Q}D}{off}%
\ifthenelse{\Banswer=4}{Correct}{Incorrect}
\end{ocg}%
% Question 2 - Choice E
\item [\layerCheckBox{Q\arabic{Q}E}{\arabic{Q}E}{off} E.] \Bchoicee\par
\begin{ocg}[radiobtngrp=Choices-2]{Q\arabic{Q}E}{\arabic{Q}E}{off}%
\ifthenelse{\Banswer=5}{Correct}{Incorrect}
\end{ocg}%
\end{enumerate}
\end{tcbitemize}
\tcbitem[raster multicolumn=1,raster multirow=5,blankest,raster height=\tcbtextheight]
% These are the solutions
\begin{tcbitemize}[raster rows=5,raster columns=1,raster height=\tcbtextheight]
% Solution to question 1
\tcbitem[colframe=blue!50!white,colback=white]
\hspace{-0.6cm} \layerCheckBox{Q1D}{1D}{off}
\begin{ocg}{Q1D}{1D}{off}%
\Asolution
\end{ocg}
% Solution to question 2
\tcbitem[colframe=blue!50!white,colback=white]
\hspace{-0.6cm} \layerCheckBox{Q2E}{2E}{off}
\begin{ocg}{Q2E}{2E}{off}%
\Bsolution
\end{ocg}
\end{tcbitemize}
\end{tcboxeditemize}
\clearpage
\stepcounter{P}
}
\end{document}