带有答案链接的练习(使用计数器的问题)

带有答案链接的练习(使用计数器的问题)

抱歉,网站不允许我以评论的形式提出详细问题(也不显示格式工具栏),因此添加了一个新问题。

问题:尝试创建一个练习列表(带有相应答案的链接)。所有答案都在文档末尾。但是,我不想手动标记每个问题。所以,我想我会使用问题编号作为标签,但显然它使用问题总数作为标签。任何帮助表示感谢。感谢@CarLaTeX回答原始问题这里

\documentclass{article}
\usepackage{hyperref}% you must load it before the exercise package
\usepackage[answerdelayed]{exercise}

\begin{document}

    \begin{Exercise}[label={exerciseNumber}]
        What is the value of 2+5? (To see the answer click here: \refAnswer{\ExerciseLabel})
    \end{Exercise}
    \begin{Answer}[ref=\ExerciseLabel]
        7
    \end{Answer}

    \begin{Exercise}[label={exerciseNumber}]
        What is the value of 4-1? (To see the answer click here: \refAnswer{\ExerciseLabel})
    \end{Exercise}
    \begin{Answer}[ref=\ExerciseLabel]
        3
    \end{Answer}

     \begin{Exercise}[label={exerciseNumber}]
        What is the value of 2*4? (To see the answer click here: \refAnswer{\ExerciseLabel})
    \end{Exercise}
    \begin{Answer}[ref=\ExerciseLabel]
        8
    \end{Answer}
    \shipoutAnswer
\end{document}

输出(错误):

在此处输入图片描述

答案1

您正在寻找的“问题编号”存储为计数器,适当地命名为Exercise(至少对于练习环境而言)。您可以使用以下方式访问计数器:

\the\value{Exercise}

因此,您需要做的就是在将其作为选项传递时将其用作标签\begin{Exercise}。请参阅下面的示例。

梅威瑟:

\documentclass{article}
\usepackage{hyperref}% you must load it before the exercise package
\usepackage[answerdelayed]{exercise}

\begin{document}
  \begin{Exercise}[label={\the\value{Exercise}}]
    What is the value of $2+5$? (To see the answer click here:~\refAnswer{\ExerciseLabel})
  \end{Exercise}
  \begin{Answer}[ref=\ExerciseLabel]
    7
  \end{Answer}

  \begin{Exercise}[label={\the\value{Exercise}}]
    What is the value of $4-1$? (To see the answer click here:~\refAnswer{\ExerciseLabel})
  \end{Exercise}
  \begin{Answer}[ref=\ExerciseLabel]
    3
  \end{Answer}

  \begin{Exercise}[label={\the\value{Exercise}}]
    What is the value of $2\times 4$? (To see the answer click here:~\refAnswer{\ExerciseLabel})
  \end{Exercise}
  \begin{Answer}[ref=\ExerciseLabel]
    8
  \end{Answer}
  \shipoutAnswer
\end{document}

防爆型


附加信息

你可以让输入标签变得不那么麻烦,如果

(选项1)你定义一个宏如下\blah

% Preamble:    
\newcommand*{\blah}{\the\value{Exercise}}
% Main document:
\begin{Exercise}[label=\blah]

或者

(选项 2)如果你觉得自己特别懒,只需简单地定义一个新环境:

% In Preamble
\newcounter{Ex}
\newenvironment{Ex}{\begin{Exercise}[name={Exercise},
    counter={Ex},
    label=\the\value{Ex}]}
{\end{Exercise}}

然后在主文档中,像这样调用你的练习:

% In Main document
\begin{Ex}
    What is the value of $2+5$? (To see the answer click here:~\refAnswer{\ExerciseLabel})
\end{Ex}
\begin{Answer}[ref=\ExerciseLabel]
    7
\end{Answer}

相关内容