Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have created an environment named hwp with custom headers and footers that works otherwise very much like the native section environment, in which I can set equations, add figures, etc. What I would like to do is to use the \numberwithin{equation}{section} for hwp. In other words, the numbering of equations should be reset whenever a new hwp is created.

As it appears that I have to work around the \numberwithin command, I have been working on setting up my own counter that would serve the purpose, yet to no avail. The following is the code related to my goal. Any advice would be much appreciated.

\newcounter{hwpcounter}
\newcounter{eqcounter}
\renewcommand{\theeqcounter}{\addtocounter{eqcounter}{1}}

\newenvironment{hwp}[1][Prob \arabic{hwpcounter}]
{
    \stepcounter{hwpcounter}
    \renewcommand{\theequation}{\arabic{hwpcounter}.\theeqcounter}
}
share|improve this question
    
Welcome to TeX.SX!](meta.tex.stackexchange.com/q/1436) You don't have to sign with your name since it automatically appears in the lower right corner of your post. –  Jubobs Feb 4 at 16:01

1 Answer 1

up vote 4 down vote accepted

I provided a dummy definition for the hwp environment, since the one given in the question is not complete:

\documentclass{article} 
\usepackage{amsmath}

\newcounter{hwpcounter}

\newenvironment{hwp}[1][Prob~\arabic{hwpcounter}]
  {\refstepcounter{hwpcounter}\par#1\par\nobreak}
  {\par}

\numberwithin{equation}{hwpcounter}

\begin{document}

\begin{hwp}
test
\begin{equation}
a=b.
\end{equation}
\begin{equation}
a=b.
\end{equation}
\end{hwp}

\begin{hwp}
test
\begin{equation}
a=b.
\end{equation}
\begin{equation}
a=b.
\end{equation}
\end{hwp}

\end{document}

enter image description here

share|improve this answer
    
Thank you, Gonzalo! This worked like a charm. –  mgu Feb 4 at 16:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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