2

Please see the following MWE.

\documentclass{exam}

\usepackage{etoolbox}

% The following does not have any effect on the output
\AtBeginEnvironment{questions}{\setcounter{question}{4}}

\begin{document}

\begin{questions}
  % The following works fine
  % \setcounter{question}{4}
\question What is the answer?
\end{questions}

\end{document}

When only the \AtBeginEnvironment command is retained, the question number in the output does not change.

enter image description here

However, it works fine when the \setcounter{question}{4} is explicitly used inside the environment.

enter image description here

As per the manual of etoolbox, the first approach was supposed to work.

\AtBeginEnvironment{<environment>}{<code>} Appends arbitrary <code> to a hook executed by the \begin command at the beginning of a given <environment>, immediately before \<environment>, inside the group opened by \begin.

1
  • As you quote mmediately before \<environment>, that is not the same as inside the questions env. It might be doable using the kernel env hooks (haven't checked)
    – daleif
    Commented Sep 12, 2023 at 7:10

1 Answer 1

2

Just add the command

\renewcommand{\questionshook}{\setcounter{question}{4}}

to your preamble instead of trying to use \AtBeginEnvironment

4
  • Thanks. Do you want to add a bit more explanation to make the answer self-contained?
    – Masroor
    Commented Sep 12, 2023 at 2:37
  • @Masroor I don't know how AtBeginEnvironment is supposed to work; it was just clear that using \questionshook wou;d do the job. Commented Sep 12, 2023 at 2:48
  • Your answer was definitely helpful, but I will want to add a bit before I click Accept. Why the original approach does not work will augment the answer.
    – Masroor
    Commented Sep 12, 2023 at 3:01
  • 1
    @Masroor On further thought, \AtBeginEnvironment probably had no hope because the actual list of questions is begun by a \list command that doesn't appear until after a lot of other commands that begin the questions environment, and the \list command sets the question counter to zero; any change in the question counter before the \list command is thus cancelled. Commented Sep 12, 2023 at 3:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .