Math & ScienceHow to define a new counter in gather environment

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
bcpn
Posts: 3
Joined: Fri Sep 05, 2014 4:19 pm

How to define a new counter in gather environment

Post by bcpn »

Hi everyone!

I have been trying to create a new counter to clearly identify a specific subset of equations. I use the following code to overwrite the equation environment:

Code: Select all

\newcounter{newcounter}
\setcounter{newcounter}{0}
\newenvironment{NEWequation}{
\addtocounter{equation}{-1}
\refstepcounter{newcounter}
\renewcommand\theequation{NEW.\thenewcounter}
\begin{equation}}
{\end{equation}}
This is working as I want it to, but then I want to use and increment the same counter for gather environments, and I fail to increase the counter within the environment. So far I've tried the following:

Code: Select all

\NewEnviron{NEWgather}{
\addtocounter{equation}{-1}
\refstepcounter{newcounter}
\renewcommand\theequation{NEW.\thenewcounter}
\begin{gather}
\BODY
\end{gather}
}
It properly updates the counter from NEWgather/NEWequation environment to the next NEWgather/NEWequation environment, but fails to do so in the body.

Does anyone know how to overcome this issue? Thank you for your help! :)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: How to define a new counter in gather environment

Post by Johannes_B »

Can you extend your code to a compilable example showing not only the definition but an example of use as well?
I think a bullet-proof solution may already exist in a package.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
bcpn
Posts: 3
Joined: Fri Sep 05, 2014 4:19 pm

How to define a new counter in gather environment

Post by bcpn »

Yes of course! Here is a compilable code of what I would like to obtain..
Thanks! :D

Code: Select all

\documentclass[a4paper1]{article}
\usepackage[fleqn]{amsmath}
\usepackage[english]{babel}
\usepackage{environ}

%pour les equations de linearisation
\newcounter{newcounter}
\setcounter{newcounter}{0}
\newenvironment{NEWequation}{%
\addtocounter{equation}{-1}
\refstepcounter{newcounter}
\renewcommand\theequation{NEW.\thenewcounter}
\begin{equation}}
{\end{equation}}

\NewEnviron{NEWgather}{%
\addtocounter{equation}{-1}
\refstepcounter{newcounter}
\renewcommand\theequation{NEW.\thenewcounter}
\begin{gather}
 \BODY
\end{gather}
}

\begin{document}

First equation I want to rename (NEW.1): it works
\begin{NEWequation}
\hat{r}_{t}^{K,i}+\hat{K}^i_{t-1} = \widehat{RW}^i_t+\frac{\bar{\nu}^{w,i}}{1-\bar{\nu}^{w,i}}\hat{\nu}_{t}^{w,i}+\frac{\bar{\nu}^{C,i}}{1+\bar{\nu}^{C,i}}\hat{\nu}_{t}^{C,i}+\hat{L}^i_t
\end{NEWequation}

A group of equations I also want to rename. I would like the first one to be (NEW.2) (that's okay), but the second one to be (NEW.3)
\begin{NEWgather}
\widehat{RPC}^1_t=\alpha^1\hat{T}_t\\
\widehat{RPC}^2_t=-\alpha^2\hat{T}_t
\end{NEWgather}

And a again a single equation that properly increments the counter
\begin{NEWequation}
\hat{T}_t=\hat{T}_{t-1}+\hat{\Pi}^2_t-\hat{\Pi}^1_t.
\end{NEWequation}

\end{document}
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to define a new counter in gather environment

Post by Johannes_B »

Not really predefined by a package nor bullet-proof but it seems to work.

Code: Select all

%\documentclass{book}
\documentclass{article}
\usepackage[fleqn]{amsmath}
\usepackage[english]{babel}

%\usepackage{chngcntr}
%\counterwithin{equation}{chapter}
\newcounter{resumeq}
\newcommand{\switchNumbering}[1]{%
	\let\theresumeeq\theequation%
	\setcounter{resumeq}{\value{equation}}
	\setcounter{equation}{0}%
	\renewcommand{\theequation}{#1.\arabic{equation}}%
}
\newcommand{\switchBack}{
	\setcounter{equation}{\value{resumeq}}
	\let\theequation\theresumeeq
}

\begin{document}

\begin{equation}
	a
\end{equation}
\begin{gather}
	b \\
	c \\
	f
	\end{gather}

Now introducing a new scheme using an argument:
\switchNumbering{A}
\begin{equation}
	a
\end{equation}
\begin{gather}
	b \\
	c \\
	f \\ l \\ i
	\end{gather}

And now switching back again
	\switchBack

\begin{equation}
	a
\end{equation}
\begin{gather}
	b \\
	c \\
	f
\end{gather}

switching again:
\switchNumbering{B}
\begin{equation}
	t
\end{equation}

\begin{gather}
	u \\ q
\end{gather}

\begin{equation}
	i
\end{equation}

and again switching back
\switchBack
\begin{equation}
	q
\end{equation}

\begin{gather}
	f \\l
\end{gather}

\begin{equation}
	r
\end{equation}
\end{document}
SwitchNumbering saves the appearance of the tag and its current numerical value and then resets the numbering. It expects one argument of you to put in front to describe the new numbering scheme.

switchBack later resumes both the appearance and the last number to go on as nothing has ever happened.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
bcpn
Posts: 3
Joined: Fri Sep 05, 2014 4:19 pm

Re: How to define a new counter in gather environment

Post by bcpn »

Thank you very much, that should do the work!
Post Reply