1
%-------------------------------------------------------------------------- %
\documentclass[english]{book}
%-------------------------------------------------------------------------- %
%
\usepackage[fleqn]{amsmath} %
\usepackage{amsthm,amssymb} %
\usepackage[explicit]{titlesec} %
\usepackage{refcount} %
\usepackage{xcolor}
\usepackage{colortbl} %
\usepackage{relsize} %
\usepackage[paperwidth=170mm,paperheight=240mm,textwidth=132mm,lmargin=12.5mm,rmargin=12.5mm,bindingoffset=12.5mm,noheadfoot,nomarginpar,showframe,showcrop]{geometry}
\usepackage[a4,frame,cam,center]{crop}
\usepackage{stackengine} %
\usepackage[skins,theorems,most]{tcolorbox}
\usepackage{ifpdf}
\usepackage{cancel}
\usepackage{times}
\usepackage{exercise}
\usepackage{ifthen}\newboolean{firstanswerofthechapter}
\usepackage{hyperref}
%
% --------------------------------------- START DECLARATIONS --------------------------------------------- %
%
\setlength{\unitlength}{1mm} % 1mm=2.834646pt; 1pt=0.3515mm

 \DeclareMathSizes{10}{10.2}{8.0}{7.2}
 \DeclareMathSizes{10.95}{11.20}{8.8}{8.0} % 10.95 11.20 8.8 8.0

\definecolor{ThemeColor}{rgb}{0.45,0.00,0.45} %
\definecolor{DarkBlue}{rgb}{0.00,0.08,0.45} % #000086
\definecolor{DarkOrange}{rgb}{0.55,0.22,0.05} % #FB9902
\definecolor{DarkRed}{rgb}{0.45,0.06,0.06} % #880000
\definecolor{LightGrey}{rgb}{0.96,0.96,0.96}
\definecolor{LightOrange}{rgb}{1.00,0.87,0.64} % #FB9902
\definecolor{LightYellow}{rgb}{1.00,1.00,0.85} %


\newcommand\Hrule{\noindent\color{ThemeColor}{\rule{\linewidth}{3pt}}} %

\titleformat{\chapter}[display]
{\color{ThemeColor}\bfseries\itshape\fontsize{12}{14}\selectfont} %
{\Hrule\vspace*{6pt}\\ {\color{black}\,\chaptername\hspace{2pt}\normalfont\fontsize{12}{14} %
\selectfont{\thechapter}}} %
{0pt}{\bfseries\normalfont\fontsize{12}{14}\selectfont{#1}}

\titlespacing{\chapter}{0pt}{12pt}{6pt}[0pt] %
\renewcommand{\thechapter}{\arabic{chapter}} % make-up of the numbering

\tcbset{myinnerbox/.style={no shadow,shrink tight,arc=1.0pt,extrude by=2mm,colframe=black,
  boxrule=0.7pt,frame style={opacity=0.25},interior style={opacity=0.5},colback=black!5!white}}
%
\renewcommand{\AtBeginExercise}{\vspace{0pt}} % before the first question
%
% to here
%
\counterwithin{Exercise}{chapter}
\counterwithin{Answer}{chapter}
\newcounter{problem}[Exercise] %

\renewcommand{\ExerciseName}{Exercise}
\renewcommand{\QuestionNB}{\arabic{Question}\hspace{0.7pt}.\hspace{1.5pt}} %
\renewcommand{\subQuestionNB}{\alph{subQuestion}\hspace{0.7pt})\hspace{1.5pt}} %

% ------------------------------------------------ Spacings --------------------------------------------- %

\setlength{\QuestionBefore}{1pt} % before a new question was 2pt
\setlength{\subQuestionBefore}{-1pt} % space between two subquestions was -0.5pt
\setlength{\QuestionIndent}{20pt} % indents whole
\setlength{\subQuestionIndent}{15.5pt} % indent of a) further was 16pt
\setlength{\Exesep}{0pt} % OK
\setlength{\QuestionIndent}{18pt} % 16pt - for double-digits
%
\renewcommand{\ExerciseHeader}{\ifthenelse{\boolean{firstanswerofthechapter}} %
{\newpage\noindent\hspace{1.5pt}\vspace*{8pt}
{\fontsize{12}{14}\selectfont{\text{Questions based on \hspace{1.2pt}}\text{\,CHAPTER \thechapter\,}}\;}\newline\newline % stop
\noindent\def\stackalignment{l}\stackunder[3.5pt]{\colorbox{LightOrange} %
{\fontsize{11.5}{14}\selectfont{\textcolor{ThemeColor}{Exercise~\ExerciseHeaderNB}}\smallskip}}
{\textcolor{DarkOrange}{\rule{\linewidth}{1.2pt}}}} % else
    {\noindent\def\stackalignment{l}\stackunder[3.5pt]{\colorbox{LightOrange} %
        {\fontsize{11.5}{14}\selectfont{\textcolor{ThemeColor}{Exercise~\ExerciseHeaderNB}}\smallskip}}
{\textcolor{DarkOrange}{\rule{\linewidth}{1.2pt}}}}}

\renewcommand{\AnswerHeader}{\ifthenelse{\boolean{firstanswerofthechapter}} %
{\newpage\noindent\hspace{1.5pt}\vspace*{8pt}
{\fontsize{12}{14}\selectfont{\text{Answers to questions based on \hspace{1.2pt}}\text{\,CHAPTER \thechapter\,}}\;}\newline\newline % stop
\noindent\def\stackalignment{l}\stackunder[3.5pt]{\colorbox{LightOrange} % continue
{\fontsize{11.5}{14}\selectfont{\textcolor{ThemeColor}{Answers to exercise~\ExerciseHeaderNB\hspace{3.4pt}on \hspace{0.6pt}p\hspace{2.0pt}\pageref{\AnswerRef}}\,}\smallskip}}
{\textcolor{DarkOrange}{\rule{\linewidth}{1.2pt}}}} % else
    {\noindent\def\stackalignment{l}\stackunder[3.5pt]{\colorbox{LightOrange} %
        {\fontsize{11.5}{14}\selectfont{\textcolor{ThemeColor}{Answers to exercise~\ExerciseHeaderNB\hspace{3.4pt}on \hspace{0.6pt}p\hspace{2.0pt}\pageref{\AnswerRef}}\,}\smallskip}}
{\textcolor{DarkOrange}{\rule{\linewidth}{1.2pt}}}}}

\begin{document}

\mainmatter

\chapter{} % Chapter 1
\label{chap:chapter_01}

\setboolean{firstanswerofthechapter}{true}
\begin{Exercise}[label={chap_01_exer_01}] %
\Question{Question 1}
\Question{Question 2}
\end{Exercise}

\setboolean{firstanswerofthechapter}{false}
\begin{Exercise}[label={chap_01_exer_02}]
\Question{Question 1}
\Question{Question 2}
\end{Exercise}

\begin{Exercise}[label={chap_01_exer_03}]
\Question{Question 1}
\Question{Qustion 2}
\end{Exercise}

\setboolean{firstanswerofthechapter}{true}
\begin{Answer}[ref={chap_01_exer_01}]
\Question{Answer to question 1}
\Question{Answer to question 2}
\end{Answer}

\setboolean{firstanswerofthechapter}{false}
\begin{Answer}[ref={chap_01_exer_02}]
\Question{Answer to question 1}
\Question{Answer to question 2}
\end{Answer}

\begin{Answer}[ref={chap_01_exer_03}]
\Question{Answer to question 1}
\Question{Answer to question 2}
\end{Answer}

\chapter{} % Chapter 2
\label{chap:chapter_02}

\setboolean{firstanswerofthechapter}{true}
\begin{Exercise}[label={chap_02_exer_01}]
\Question{Question 1}
\Question{Question 2}
\end{Exercise}

\setboolean{firstanswerofthechapter}{false}
\begin{Exercise}[label={chap_02_exer_02}]
\Question{Question 1 }
\Question{Question 2}
\end{Exercise}

\begin{Exercise}[label={chap_02_exer_03}]
\Question{Question 1}
\Question{Question 2}
\end{Exercise}

\setboolean{firstanswerofthechapter}{true}
\begin{Answer}[ref={chap_02_exer_01}]
\Question{Question 1}
\Question{Question 2}
\end{Answer}

\setboolean{firstanswerofthechapter}{false} 
\begin{Answer}[ref={chap_02_exer_02}]
\Question{Answer to question 1}
\Question{Answer to question 2}
\end{Answer}

\begin{Answer}[ref={chap_02_exer_03}]
\Question{Answer to question 1}
\Question{Answer to question 2}
\end{Answer}

\end{document}
4

Sorry this is a bug in 2018-04-01 release it is fixed in patch level 3

https://github.com/latex3/latex2e/issues/38

For now you can do

\makeatletter
\def\counterwithin@x#1#2{%
  \@ifbothcounters{#1}{#2}%
      {\@addtoreset{#1}{#2}%
       \expandafter
       \gdef\csname the#1\expandafter\endcsname\expandafter
            {\csname the#2\expandafter\endcsname\expandafter
             .\expandafter% spot the dot :(
             \@arabic\csname c@#1\endcsname}}}
\makeatother

in the preamble.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.