LaTeX forum ⇒ Math & ScienceHow to define a new counter in gather environment Topic is solved

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

How to define a new counter in gather environment

Postby bcpn » Fri Sep 05, 2014 4:30 pm

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:
  1. \newcounter{newcounter}
  2. \setcounter{newcounter}{0}
  3. \newenvironment{NEWequation}{
  4. \addtocounter{equation}{-1}
  5. \refstepcounter{newcounter}
  6. \renewcommand\theequation{NEW.\thenewcounter}
  7. \begin{equation}}
  8. {\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:

  1. \NewEnviron{NEWgather}{
  2. \addtocounter{equation}{-1}
  3. \refstepcounter{newcounter}
  4. \renewcommand\theequation{NEW.\thenewcounter}
  5. \begin{gather}
  6. \BODY
  7. \end{gather}
  8. }


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! :)

Tags:

User avatar
Johannes_B
Site Moderator
Posts: 3587
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Fri Sep 05, 2014 8:31 pm

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

Postby bcpn » Tue Sep 09, 2014 6:02 pm

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

  1. \documentclass[a4paper1]{article}
  2. \usepackage[fleqn]{amsmath}
  3. \usepackage[english]{babel}
  4. \usepackage{environ}
  5.  
  6. %pour les equations de linearisation
  7. \newcounter{newcounter}
  8. \setcounter{newcounter}{0}
  9. \newenvironment{NEWequation}{%
  10. \addtocounter{equation}{-1}
  11. \refstepcounter{newcounter}
  12. \renewcommand\theequation{NEW.\thenewcounter}
  13. \begin{equation}}
  14. {\end{equation}}
  15.  
  16. \NewEnviron{NEWgather}{%
  17. \addtocounter{equation}{-1}
  18. \refstepcounter{newcounter}
  19. \renewcommand\theequation{NEW.\thenewcounter}
  20. \begin{gather}
  21. \BODY
  22. \end{gather}
  23. }
  24.  
  25. \begin{document}
  26.  
  27. First equation I want to rename (NEW.1): it works
  28. \begin{NEWequation}
  29. \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
  30. \end{NEWequation}
  31.  
  32. 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)
  33. \begin{NEWgather}
  34. \widehat{RPC}^1_t=\alpha^1\hat{T}_t\\
  35. \widehat{RPC}^2_t=-\alpha^2\hat{T}_t
  36. \end{NEWgather}
  37.  
  38. And a again a single equation that properly increments the counter
  39. \begin{NEWequation}
  40. \hat{T}_t=\hat{T}_{t-1}+\hat{\Pi}^2_t-\hat{\Pi}^1_t.
  41. \end{NEWequation}
  42.  
  43. \end{document}

User avatar
Johannes_B
Site Moderator
Posts: 3587
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Wed Sep 10, 2014 10:36 am

Not really predefined by a package nor bullet-proof but it seems to work.
  1. %\documentclass{book}
  2. \documentclass{article}
  3. \usepackage[fleqn]{amsmath}
  4. \usepackage[english]{babel}
  5.  
  6. %\usepackage{chngcntr}
  7. %\counterwithin{equation}{chapter}
  8. \newcounter{resumeq}
  9. \newcommand{\switchNumbering}[1]{%
  10. \let\theresumeeq\theequation%
  11. \setcounter{resumeq}{\value{equation}}
  12. \setcounter{equation}{0}%
  13. \renewcommand{\theequation}{#1.\arabic{equation}}%
  14. }
  15. \newcommand{\switchBack}{
  16. \setcounter{equation}{\value{resumeq}}
  17. \let\theequation\theresumeeq
  18. }
  19.  
  20. \begin{document}
  21.  
  22. \begin{equation}
  23. a
  24. \end{equation}
  25. \begin{gather}
  26. b \\
  27. c \\
  28. f
  29. \end{gather}
  30.  
  31. Now introducing a new scheme using an argument:
  32. \switchNumbering{A}
  33. \begin{equation}
  34. a
  35. \end{equation}
  36. \begin{gather}
  37. b \\
  38. c \\
  39. f \\ l \\ i
  40. \end{gather}
  41.  
  42. And now switching back again
  43. \switchBack
  44.  
  45. \begin{equation}
  46. a
  47. \end{equation}
  48. \begin{gather}
  49. b \\
  50. c \\
  51. f
  52. \end{gather}
  53.  
  54. switching again:
  55. \switchNumbering{B}
  56. \begin{equation}
  57. t
  58. \end{equation}
  59.  
  60. \begin{gather}
  61. u \\ q
  62. \end{gather}
  63.  
  64. \begin{equation}
  65. i
  66. \end{equation}
  67.  
  68. and again switching back
  69. \switchBack
  70. \begin{equation}
  71. q
  72. \end{equation}
  73.  
  74. \begin{gather}
  75. f \\l
  76. \end{gather}
  77.  
  78. \begin{equation}
  79. r
  80. \end{equation}
  81. \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

Postby bcpn » Fri Sep 12, 2014 11:14 am

Thank you very much, that should do the work!


Return to “Math & Science”

Who is online

Users browsing this forum: No registered users and 4 guests