0

I would like to make a reference to an exercise/theorem which is numbered in my document with my own counters. Something like: as we saw in theorem 6.4.3, ...

My issue is that I don't want to use the section/chapter number but the number of my exercise/theorem. So the \label and \ref commands don't seem to do the job as far as I can see. I found this very interesting link looking down on here:

http://www.ryancreich.info/index.php?page=latex&subpage=rrthm

However, I couldn't adapt this - not good enough I'm afraid - and I precise that I'm not using the AMS-theorem environment because I defined my own.

Here is a short sample:

\documentclass{book}[11pt,a4paper]
\usepackage[utf8]{inputenc}
\usepackage[OT1]{fontenc}
\usepackage{amsmath}
\newcounter{myCounter}
\setcounter{myCounter}{1}
\newcommand{\exercise}[1]{\textbf {Exercise \thechapter.\themyCounter }

    #1 \stepcounter{myCounter}
}
\parindent=0pt

\begin{document}
    \chapter{First chapter}

    \exercise{First exercise}

    \exercise{Second exercise}

    \exercise{Third exercise}

    Here I would like to make a reference to second exercise that would print ”Exercise 2.1" but would change whenever I add/remove an exercise/theorem before the given exercise.

    Something like : as we saw in "Exercise 2.1", the sum of integers from 1 to n is $\dfrac{n(n+1)}{2}$

\end{document}

Hope my question is clear.

  • Is there any reason why you don'tuse a dedicated package like amsthm or ntheorem? – Bernard Jul 9 '18 at 11:39
  • Yes. My theorems/definitions/exercices are mostly inside tcolor boxes, so it's not compatible (I think) – HerculePoivrot Jul 9 '18 at 11:42
  • Including these exercises? – Bernard Jul 9 '18 at 11:57
  • Yeah it's a short sample, the document is much bigger and my results are in tcolorboxes. The exercices aren't in fact, but I mostly need references to theorems, properties or so. For instance to ask in an exercice to make the proof of properties 5.6.16. – HerculePoivrot Jul 9 '18 at 12:00
  • That's not incompatible with theorem-like exercices. – Bernard Jul 9 '18 at 12:05
2

Here is a way to go. However, I'd recoomend to use a theorem-like environment.

\documentclass[11pt, a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[OT1]{fontenc}
\usepackage{amsmath}
\usepackage{chngcntr}
\newcounter{myCounter}
\counterwithin{myCounter}{chapter}
\setcounter{myCounter}{0}
\newcommand{\exercise}[1]{\refstepcounter{myCounter}\textbf {Exercice \themyCounter }
    #1
}
\parindent=0pt

\begin{document}

    \chapter{First chapter}

    \exercise{First exercise\label{exo1}}

    \exercise{Second exercise\label{exo2}}

    \exercise{Third exercise\label{exo3}}

    Here I would like to make a reference to second exercice that would print ”Exercice 2.1" but would change whenever I add/remove an exercice/theorem before the given exercice.

    Something like : as we saw in "Exercice \ref{exo2}", the sum of integers from 1 to n is $\dfrac{n(n+1)}{2}$

\end{document} 

enter image description here

  • Thanks, that looks to do the job. Can you please help me understand what \counterwithin{myCounter}{chapter}is doing in here ? It seems to make the references using this counter instead of chapter ? For doing this with the theorem-like environnment, my choice of doing this with my own environnment was to provide me a way to customize the look. – HerculePoivrot Jul 9 '18 at 12:16
  • It resets myCounter at each new chapter, and prepends the chapter number (with a dot) to \themyCounter. I guess you could make it work with cleveref, which would make typing cross-references simpler. – Bernard Jul 9 '18 at 12:22
1
\documentclass{book}[11pt,a4paper]
\usepackage[utf8]{inputenc}
\usepackage[OT1]{fontenc}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{deflist}

\newcounter{myCounter}[chapter]
\renewcommand\themyCounter{\thechapter.\arabic{myCounter}}
\newcommand\myCounterautorefname{\MyUppercasefork exercise}

\makeatletter
\newif\ifMyUppercase
\MyUppercasefalse
\DeclareRobustCommand\MyUppercasefork[1]{%
  \ifMyUppercase\expandafter\MakeUppercase\else\expandafter\@firstofone\fi{#1}%
}%

\newcommand{\exercise}[1]{%
  \begin{deflist}{\textbf{Exercise 99.999}}%
  \refstepcounter{myCounter}%
  \protected@edef\@currentlabelname{%
    \myCounterautorefname\protect\space\themyCounter
  }%
  \item[{%
    \textbf{{\MyUppercasetrue\@currentlabelname}}%
   }]#1%
  \end{deflist}%
}%
\makeatother

\parindent=0pt

\begin{document}

\tableofcontents

\chapter{First chapter}
\enlargethispage{2cm}

\section*{References with clickable hyperlinks}

\verb|\ref{Ex2.1}|: \ref{Ex2.1}

\verb|\pageref{Ex2.1}|: \pageref{Ex2.1}

\subsection*{\texorpdfstring{\protect\texttt}{}%
             {\textbackslash MyUppercasefalse}:}
\MyUppercasefalse

\verb|\href{\string#myCounter.2.1}{A nice long link to \autoref*{Ex2.1}}|:\\
\href{\string#myCounter.2.1}{A nice long link to \autoref*{Ex2.1}}
\smallskip

\verb|\hyperref[Ex2.1]{A nice long link to \autoref*{Ex2.1}}|:\\
\hyperref[Ex2.1]{A nice long link to \autoref*{Ex2.1}}
\smallskip

\verb|\nameref{Ex2.1}|: \nameref{Ex2.1}

\verb|\autoref{Ex2.1}|: \autoref{Ex2.1}

\subsection*{\texorpdfstring{\protect\texttt}{}%
             {\textbackslash MyUppercasetrue}:}
\MyUppercasetrue

\verb|\nameref{Ex2.1}|: \nameref{Ex2.1}

\verb|\autoref{Ex2.1}|: \autoref{Ex2.1}

\MyUppercasefalse

\section*{References without clickable hyperlinks}

\verb|\ref*{Ex2.1}|: \ref*{Ex2.1}

\verb|\pageref*{Ex2.1}|: \pageref*{Ex2.1}

\subsection*{\texorpdfstring{\protect\texttt}{}%
             {\textbackslash MyUppercasefalse}:}
\MyUppercasefalse

\verb|\nameref*{Ex2.1}|: \nameref*{Ex2.1}

\verb|\autoref*{Ex2.1}|: \autoref*{Ex2.1}

\subsection*{\texorpdfstring{\protect\texttt}{}%
             {\textbackslash MyUppercasetrue}:}
\MyUppercasetrue

\verb|\nameref*{Ex2.1}|: \nameref*{Ex2.1}

\verb|\autoref*{Ex2.1}|: \autoref*{Ex2.1}

\MyUppercasefalse

\chapter{Second chapter}

\exercise{%
  \label{Ex2.1}First exercise of second chapter.
  First exercise of second chapter.
  First exercise of second chapter.
  First exercise of second chapter.
  First exercise of second chapter.
  First exercise of second chapter.
}

\exercise{Second exercise of second chapter.}

\exercise{Third exercise of second chapter.}

\exercise{Fourth exercise of second chapter.}

\exercise{Fifth exercise of second chapter.}

\exercise{Sixth exercise of second chapter.}

\exercise{Seventh exercise of second chapter.}

\exercise{Eighth exercise of second chapter.}

\exercise{Nineth exercise of second chapter.}

\exercise{Tenth exercise of second chapter.}

\end{document}

enter image description here enter image description here

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.