Macros – How to Modify the \question Command in Exam Class for Custom Questions

bookmarksexamhyperrefmacros

I create math exams using \documentclass[legalpaper, 12pt]{exam} and \usepackage[bookmarks]{hyperref}.

Each question is created by inserting \question[points] in the document and the enumeration of the questions is automatically created.

In order to see markers of each question in Texmaker, I use \label{Q-number} and in order to create bookmarks in the pdf for each question I use \pdfbookmark[1]{Q-number}{Q-number}.

So for example, for Question 4 which has 9 points I would write:

\label{Q4}
\pdfbookmark[1]{Q4}{Q4}
\question[9] Solve the equation $x+1=3$.

Question 1: Do you know how to create a macro like the one below?

\def\macroquestion #1{\label{Q-number}
    \pdfbookmark[1]{Q-number}{Q-number}
    \question[#1] }

so then I just have to write

\macroquestion{9} Solve the equation $x+1=3$.

where the counter Q-number gets automatically generated (depending only on the place where the command appears in the document).

Also, when a question has parts (3 and 4 points respectively) I usually type something like:

\question Solve the following equations:
\begin{parts}
\part[3] \label{Q4(a)}
\pdfbookmark[2]{Q4(a)}{Q4(a)} Solve the equation $x+1=3$.
\part[4] \label{Q4(b)}
\pdfbookmark[2]{Q4(b)}{Q4(b)} Solve the equation $x+9=8$.
\end{parts}

Question 2: Do you know how to create a \macropart that can take care of the counters Q4(a) and Q4(b), the labels and the bookmarks automatically similarly to \macroquestion?

Best Answer

The following definitions of \newquestion and \newpart seems to achieve what you're looking for. The difference between what you suggested (as \macroquestion) is that it still takes an optional argument and rearranges the setting of the \question, followed by the \label and \pdfbookmark. In your original setup there may have been a incorrect hypertarget set for the label or bookmark.

enter image description here

\documentclass{exam}

\usepackage[bookmarks]{hyperref}

\NewDocumentCommand{\newquestion}{ o }{%
  \IfValueTF{#1}
    {\question[#1]}
    {\question}%
  \label{Q\thequestion}%
  \pdfbookmark[1]{Q\thequestion}{Q\thequestion}%
}
\NewDocumentCommand{\newpart}{ o }{%
  \IfValueTF{#1}
    {\part[#1]}
    {\part}%
  \label{Q\thequestion(\thepartno)}%
  \pdfbookmark[2]{Q\thequestion(\thepartno)}{Q\thequestion(\thepartno)}%
}

\begin{document}

\begin{questions}
  \newquestion[9] Solve the equation $x+1=3$.

  \newquestion Solve the following equations:
  \begin{parts}
    \newpart[3] Solve the equation $x+1=3$.
    \newpart[4] Solve the equation $x+9=8$.
  \end{parts}
\end{questions}

\end{document}
Related Question