1

I am writing a tutorial, and each section has some exercises. I also have an appendix which contains additional exercises for each section.

But I would like the additional exercises for each section to be numbered so they continue the numbering of the exercises in each section.

Mico suggested a solution, which partially works. But when I click on Exercise 2.3, it takes me to Exercise 1.3 instead.

I included all packages that I use in the header, probably not all are needed.

\documentclass[11pt]{article}
\newcounter{counterE} % for use in Appendix B
\usepackage{amsthm}

\usepackage[pagebackref=true, colorlinks=true, linkcolor=blue, citecolor=blue, linkcolor=blue, anchorcolor=red, urlcolor=blue]{hyperref}
\usepackage{url, amsmath,enumerate,fancyhdr,amssymb, amsthm, 
    pstricks, epsf, lscape, ifpdf, graphicx, float}
    
    \usepackage[capitalize]{cleveref}

\newtheorem{Exercise}{Exercise}[section]

\begin{document}


\section{First section} 
\label{first-section} 

\begin{Exercise}
    This is the first exercise in this section.   
\end{Exercise}

\begin{Exercise}
    This is the second exercise in this section. 
\end{Exercise}

Additional exercises for this section are in \cref{exer-app-first}. 

\section{Second section} \label{second-section} 

\begin{Exercise}
    This is the first exercise in this section.
\end{Exercise}

\begin{Exercise}
    This is the second exercise in this section. 
\end{Exercise}

Additional exercises for this section are in  \cref{exer-app-second}. 

        Referencing Exercise \ref{exer-13}.   
    
    Referencing Exercise \ref{exer-23}. 
    
    
    
    \newpage 
\appendix
\renewcommand\theExercise{\arabic{counterE}.\arabic{Exercise}} 
% change the prefix variable to "counterE"


\section{Supplemental materials} 

\section{Additional exercises}

\subsection{Additional exercises for \cref{first-section}}  
\label{exer-app-first}

\setcounter{counterE}{1} % number of first section
\setcounter{Exercise}{2} % number of last exercise in first section   

\begin{Exercise} \label{exer-13} 
    This exercise should be called ``Exercise 1.3.''
\end{Exercise}

\begin{Exercise}  \label{exer-14}  
    This exercise should be called ``Exercise 1.4.''
\end{Exercise}


    \newpage 
    
    
\subsection{ Additional exercises for \cref{second-section}}  
\label{exer-app-second}

\setcounter{counterE}{2} % number of second section
\setcounter{Exercise}{2} % number of last exercise in second section
  
\begin{Exercise}    \label{exer-23} 
    This exercise should be called ``Exercise 2.3.''
\end{Exercise}

\begin{Exercise}  \label{exer-24} 
    This exercise should be called ``Exercise 2.4.''
\end{Exercise}

\end{document}

1 Answer 1

2

Something like this?

enter image description here

\documentclass[11pt]{article}
\newcounter{counterE} % for use in Appendix B
\usepackage{amsthm}
\theoremstyle{definition} % upright lettering
\usepackage[capitalize]{cleveref}

\newtheorem{Exercise}{Exercise}[section]

\begin{document}

\section{First section} 
\label{first-section} 

\begin{Exercise}
This is the first exercise in this section.   
\end{Exercise}
    
\begin{Exercise}
This is the second exercise in this section. 
\end{Exercise}
    
Additional exercises for this section are in \cref{exer-app-first}. 

\section{Second section} \label{second-section} 
    
\begin{Exercise}
This is the first exercise in this section.
\end{Exercise}

\begin{Exercise}
This is the second exercise in this section. 
\end{Exercise}
    
Additional exercises for this section are in  \cref{exer-app-second}. 


\appendix
\renewcommand\theExercise{\arabic{counterE}.\arabic{Exercise}} 
   % change the prefix variable to "counterE"


\section{Supplemental materials} 
    
\section{Additional exercises}

\subsection{Additional exercises for \cref{first-section}}  
\label{exer-app-first}
   
\setcounter{counterE}{1} % number of first section
\setcounter{Exercise}{2} % number of last exercise in first section   

\begin{Exercise}
This exercise should be called ``Exercise 1.3.''
\end{Exercise}
    
\begin{Exercise}
This exercise should be called ``Exercise 1.4.''
\end{Exercise}
    
\subsection{ Additional exercises for \cref{second-section}}  
\label{exer-app-second}

\setcounter{counterE}{2} % number of second section
\setcounter{Exercise}{2} % number of last exercise in second section
    
\begin{Exercise}
This exercise should be called ``Exercise 2.3.''
\end{Exercise}
    
\begin{Exercise}
This exercise should be called ``Exercise 2.4.''
\end{Exercise}
    
\end{document}

Addendum to address the OP's follow-up query. First off, let's note that the issue raised in the OP's follow-up query rests importantly on following two conditions, neither of which figured in the original query. First, the hyperref package is being used. Second, cross-references to exercise environments in Appendix B are being created.

The OP's issue, then, is related to the well-known occasional inability of the hyperref package to involved the relevant counter variable(s) when it creates the hyperlinks. To fix this, it's necessary to (re)define the hidden H counter that's automatically associated with the counter at the heart of the cross-reference. The following code block shows how this may be done for the sample code at hand.

enter image description here

\documentclass[11pt]{article}
\usepackage{amsthm}
\theoremstyle{definition} % upright lettering
\usepackage[colorlinks,allcolors=blue]{hyperref}
\usepackage[capitalize,nameinlink]{cleveref}

\newcounter{counterE} % for use in Appendix B

\newtheorem{Exercise}{Exercise}[section]
\crefname{Exercise}{Exercise}{Exercises}

\begin{document}

\noindent
Cross-references to Exercises \labelcref{ex_B_aa}, \labelcref{ex_B_ab}, \labelcref{ex_B_ba}, and \labelcref{ex_B_bb}.

\section{First section} 
\label{first-section} 

\begin{Exercise}
This is the first exercise in this section.   
\end{Exercise}
    
\begin{Exercise}
This is the second exercise in this section. 
\end{Exercise}
    
Additional exercises for this section are in \cref{exer-app-first}. 

\section{Second section} \label{second-section} 
    
\begin{Exercise}
This is the first exercise in this section.
\end{Exercise}

\begin{Exercise}
This is the second exercise in this section. 
\end{Exercise}
    
Additional exercises for this section are in  \cref{exer-app-second}. 


\appendix
\renewcommand\theExercise{\arabic{counterE}.\arabic{Exercise}} % change the prefix variable to "counterE"
\renewcommand\theHExercise{\arabic{subsection}.\arabic{Exercise}}
   


\section{Supplemental materials} 
    
\section{Additional exercises}

\subsection{Additional exercises for \cref{first-section}}  
\label{exer-app-first}
   
\setcounter{counterE}{1} % number of first section
\setcounter{Exercise}{2} % number of last exercise in first section   

\begin{Exercise} \label{ex_B_aa}
This exercise should be called ``Exercise 1.3.''
\end{Exercise}
    
\begin{Exercise} \label{ex_B_ab}
This exercise should be called ``Exercise 1.4.''
\end{Exercise}
    
\subsection{Additional exercises for \cref{second-section}}  
\label{exer-app-second}

\setcounter{counterE}{2} % number of second section
\setcounter{Exercise}{2} % number of last exercise in second section
    
\begin{Exercise} \label{ex_B_ba}
This exercise should be called ``Exercise 2.3.''
\end{Exercise}
    
\begin{Exercise} \label{ex_B_bb}
This exercise should be called ``Exercise 2.4.''
\end{Exercise}
    
\end{document}
8
  • 1
    This works, thank you. One gripe is that I have to do this: \setcounter{Exercise}{2} by hand.
    – district9
    Dec 4, 2023 at 22:35
  • @district9 - Setting an appropriate value for counterE in each subsection of Appendix B is presumably straightforward, and it has to be done only once. Don't bother setting the "correct" values of the Exercise counter at the start of each subsection in Appendix B until your document is more or less finished and you can thus be pretty sure that the number of exercises in the main body of the document won't change again. That way, you'll have to set the correct values of the Exercise counter at the start of each subsection of Appendix B only once as well.
    – Mico
    Dec 4, 2023 at 22:57
  • Now I Iran into an issue when I click on an exercise number. I am referencing Exercise 1.3, and when I click on it, it takes me there. But when I click on Exercise 2.3, it still takes me. to Exercise 1.3. I added a header to include url packages. Probably I do not need to add all of them, but these are the packages I use. Thanks again! I include the code below.
    – district9
    Dec 15, 2023 at 20:41
  • @district9 - Your follow-up query introduces two entirely new conditions that were not present, or even hinted at, in your initial query: (a) You want to create cross-references to exercises in the appendix, and (b) you employ the hyperref package. I've posted an addendum to show how these additiona condtions may be handled. In general, it would be much better if you posted a new query, instead of editing and partially clobbering an existing query, when your formatting problems are based on entirely new information.
    – Mico
    Dec 16, 2023 at 10:13
  • many thanks, this works. Next time I will post a new query, I am still a bit new to stack exchange.
    – district9
    Dec 21, 2023 at 19:19

You must log in to answer this question.

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