# How to reset a counter to at 1 at each start of chapter?

I want the numbering of definition, lemma and theorem has the form x.y,where x is the chapter and y represents the natural order 1,2,3...I use a counter for the value of y.Here is my code, but something undesired happened. The counter start from 0 at each chapter automatically, however I want it starts from 1. What should I do to deal with this problem? Thanks!

\documentclass{book}
\newcounter{moon}[chapter]
\newtheorem{mydef}{Definition}
\renewcommand{\themydef}{\thechapter.\arabic{moon}}
\def\endmydef{\stepcounter{moon}}
\newtheorem{mylem}{Lemma}
\renewcommand{\themylem}{\thechapter.\arabic{moon}}
\def\endmylem{\stepcounter{moon}}
\newtheorem{mythm}{Theorem}
\renewcommand{\themythm}{\thechapter.\arabic{moon}}
\def\endmythm{\stepcounter{moon}}

\begin{document}
\chapter{AlaAla...}
\begin{mydef}
something
\end{mydef}
\begin{mylem}
something
\end{mylem}
\begin{mythm}
something
\end{mythm}

\chapter{BlaBla...}
\begin{mydef}
something
\end{mydef}
\begin{mylem}
something
\end{mylem}
\begin{mythm}
something
\end{mythm}

\end{document}


• You could use the amsthm package and then this answer. – user121799 May 9 '18 at 0:53
• Do you mean that I should share the counter of definition with lemma and theorem? But if in some chapter I have no definition, then the numbering of theorem or lemma couldn't work.@marmot – user450201 May 9 '18 at 1:41
• I disagree with your statement, see my answer below where I show that this is not the case. – user121799 May 9 '18 at 1:57

## 2 Answers

General answer
This is not (easily) possible. The better way would be to move the stepping of the counter before the use of it. This is how it is done in cases like this in TeX. Because if you use \refstepcounter instead of \stepcounter to make these counters work with \ref this is the only way, to let TeX know from where on the counter is “active” as label.

Howerver in your case the way to go would be to make the three counters “aliases” for each other and use the optional argument of \newtheorem to make the base counter within the chapter counter.

Change your definitions to:

\newtheorem{mydef}{Definition}[chapter] % < counter within chapter counter
\newtheorem{mylem}{Lemma}
\newtheorem{mythm}{Theorem}
\makeatletter
\let\c@mylem\c@mydef % make lem counter an alias for def counter
\let\themylem\themydef % copy format of def counter in lem counter
\let\c@mythm\c@mydef % make thm counter an alias for def counter
\let\themythm\themydef % copy format of def counter in lem counter
\makeatother


by the way: it’s not a good idea to use \def\endmydef{\stepcounter{moon}} since this will overwrite everything else that is don in the \end part usually …

• It worked! Thanks a lot! I am not quite familiar with Latex. For me ,"realize what I want" is in the first consideration. So please forgive the flawness in my code:-) – user450201 May 9 '18 at 1:51

Just spelling out my comment. All I did was to use this answer. When you compile it, you'll see that the lemma and theorem in the second chapter are numbered as they should even though there is no definition in.

\documentclass{book}
\usepackage{amsthm}
\newtheorem{mydef}{Definition}[chapter]
\newtheorem{mylem}[mydef]{Lemma}
\newtheorem{mythm}[mydef]{Theorem}

\begin{document}
\chapter{AlaAla...}
\begin{mydef}
something
\end{mydef}
\begin{mylem}
something
\end{mylem}
\begin{mythm}
something
\end{mythm}

\begin{mydef}
something
\end{mydef}

\chapter{BlaBla...}
\begin{mylem}
something
\end{mylem}
\begin{mythm}
something
\end{mythm}

\end{document}

• Oh, I quite sorry for my comment. It worked also! Thank you so much :) – user450201 May 9 '18 at 2:07