帶有答案連結的練習(使用計數器的問題)

帶有答案連結的練習(使用計數器的問題)

抱歉,網站不允許我以評論形式提出詳細問題(也不顯示格式工具列),因此添加一個新問題。

問題:嘗試建立一個練習清單(帶有相應答案的連結)。所有答案都在文件末尾。但是,我不想手動標記每個問題。所以,我想我會使用問題編號作為標籤,但顯然它使用問題總數作為標籤。任何幫助表示讚賞。感謝@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}

防爆 v2


附加資訊

如果您可以減少輸入標籤的麻煩,

(選項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}

相關內容