0

How can I reset the exercise counter within the same exercise type. I don’t want to create different exercise types as I want to create question bank and use collection option. I have attached MWE and required output. I am beginner and I am open even if some one can improve my code by using XSIM.

My MWE

\documentclass[a4paper,12pt]{article}
\usepackage{xsim}
\usepackage{tasks}


\settasks{
label = (\theexercise\arabic*) ,
label-width = 5ex,
item-indent=6ex
}

\DeclareExerciseType{question}{
  exercise-env = question ,
  solution-env = answer ,
  exercise-name = Question ,
  solution-name = Answer ,
  exercise-template = item ,
  solution-template = solution ,
%   exercise/the-counter = \arabic{exercise}. ,
  counter = exercise, % shares a counter with the `exercise' type
}

\DeclareExerciseEnvironmentTemplate{item}
  {\item[\GetExerciseProperty{counter}]
  \GetExercisePropertyT{points}
    {%
    \marginpar
      {%
      \par\vspace{2.5ex plus .2ex}
      [\printgoal{\PropertyValue}]%
      }%
    }%
  }%
  {}

     
  \DeclareExerciseEnvironmentTemplate{solution}
  {\item[\GetExerciseProperty{counter}]}
  {}


\newenvironment{questions}
   {\itemize}
   {\enditemize}

   \newenvironment{answers}
   {\itemize}
   {\enditemize}

\usepackage{lipsum}
    


\xsimsetup{
    exercise/the-counter = \arabic{exercise}.,
    % exercise/within = section,
    % answer/print = true,
}

\begin{document}

\section*{Section A}

\begin{questions}

  \begin{question}[points=4]
    \lipsum[66]
  \end{question}
  \begin{answer}
    \lipsum[66]
  \end{answer}

  \begin{question}
    Answer the following questions.
    \begin{tasks}
      \task \lipsum[75]
      \task question.
    \end{tasks}
  \end{question}

  \begin{question}
    \lipsum[66]
  \end{question}
  \begin{answer}
    \lipsum[75]
  \end{answer}



\end{questions}

\section*{Section B}


\begin{questions}

  \begin{question}[points=4]
    \lipsum[66]
  \end{question}
  \begin{answer}
    \lipsum[66]
  \end{answer}

  \begin{question}
    Answer the following questions.
    \begin{tasks}
      \task \lipsum[75]
      \task question.
    \end{tasks}
  \end{question}

  \begin{question}
    \lipsum[66]
  \end{question}
  \begin{answer}
    \lipsum[75]
  \end{answer}


\end{questions}



% \begin{answers}
%   \item[] \printsolutions
% \end{answers}

\end{document}

Required output:

enter image description here

4
  • IMHO best would be to use numbered section \section{…} instead of \section*{…} and to use \DeclareExerciseType option within=section.
    – cabohah
    Mar 17 at 9:18
  • You use starred \section* macros which don't increase the section counter. Therefore linking the exercise counter to this counter has no effect at \section*. You could add \usepackage{xpatch} \xpretocmd{\section}{\setcounter{exercise}{0}}{}{} to your preamble, but it is probably indeed better to use unstarred \section macros (as suggested in the comment above) and maybe hide the section number in some way. Mar 17 at 9:24
  • Even if I use like this @JasperHabicht I am getting zero.Question number(0.1). I need to print only question number without section number.
    – Krishna
    Mar 17 at 12:46
  • @cabohah I have write an answer with your hint. I hope it's OK.
    – quark67
    Mar 17 at 17:11

1 Answer 1

1

The user cabohah has given the right direction to the solution in the comments.

After adding the setting within=section to \DeclareExerciseType, all you have to do is to redefine the \section command so that it doesn't display the number section at front of it. This can be do with the titlesec package, as showed.

In this solution, you are using the \section command, not the starred version \section*.

So, with this code:

\documentclass[a4paper,12pt]{article}
\usepackage{xsim}
\usepackage{tasks}

\settasks{
label = (\theexercise\arabic*) ,
label-width = 5ex,
item-indent=6ex
}

\DeclareExerciseType{question}{
  exercise-env = question ,
  solution-env = answer ,
  exercise-name = Question ,
  solution-name = Answer ,
  exercise-template = item ,
  solution-template = solution ,
%   exercise/the-counter = \arabic{exercise}. ,
  counter = exercise, % shares a counter with the `exercise' type
  within=section %% ADDED via comment by "cabohah"
}

\DeclareExerciseEnvironmentTemplate{item}
  {\item[\GetExerciseProperty{counter}]
  \GetExercisePropertyT{points}
    {%
    \marginpar
      {%
      \par\vspace{2.5ex plus .2ex}
      [\printgoal{\PropertyValue}]%
      }%
    }%
  }%
  {}

     
  \DeclareExerciseEnvironmentTemplate{solution}
  {\item[\GetExerciseProperty{counter}]}
  {}


\newenvironment{questions}
   {\itemize}
   {\enditemize}

   \newenvironment{answers}
   {\itemize}
   {\enditemize}

\usepackage{lipsum}
    
\xsimsetup{
    exercise/the-counter = \arabic{exercise}.,
    %exercise/within = section,
    % answer/print = true,
}

% Modifying the \section command
\usepackage{titlesec}
\titleformat{\section}[hang]%
{}%
{}%
{0pt}%
{\Large\textbf}%

\begin{document}

\section{Section A}

\begin{questions}

  \begin{question}[points=4]
    \lipsum[66]
  \end{question}
  \begin{answer}
    \lipsum[66]
  \end{answer}

  \begin{question}
    Answer the following questions.
    \begin{tasks}
      \task \lipsum[75]
      \task question.
    \end{tasks}
  \end{question}

  \begin{question}
    \lipsum[66]
  \end{question}
  \begin{answer}
    \lipsum[75]
  \end{answer}

\end{questions}


\section{Section B}

\begin{questions}

  \begin{question}[points=4]
    \lipsum[66]
  \end{question}
  \begin{answer}
    \lipsum[66]
  \end{answer}

  \begin{question}
    Answer the following questions.
    \begin{tasks}
      \task \lipsum[75]
      \task question.
    \end{tasks}
  \end{question}

  \begin{question}
    \lipsum[66]
  \end{question}
  \begin{answer}
    \lipsum[75]
  \end{answer}

\end{questions}

\end{document}

you obtain that:

enter image description here

2
  • Thank you very much for your answer. It worked perfectly. When I am using in xsimsetup I am not getting. I don't know why?
    – Krishna
    Mar 23 at 13:36
  • @Krishna I don't know enough about xsim and its internal mechanism to know if it is possible to get the same result with \xsimsetup. Moreover at the beginning of the xsim user manual it is stated that xsim is in an experimental state and actively developed, until it reaches a stable version.
    – quark67
    Mar 23 at 14:31

You must log in to answer this question.

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