0

I am trying to number equations of a subequations environment in subgroups (similar to the example) using the answers of this, this, and this questions, but I could not reach to a results!

\documentclass{article}
\usepackage{amsmath}
\newcommand\StepSubequations{
  \stepcounter{parentequation}
   \stepcounter{equation}
    \gdef\theparentequation{\arabic{parentequation}\Alph{equation}}
  \setcounter{equation}{0}
  \setcounter{parentequation}{1}
}
\begin{document}
    \begin{subequations}
        \begin{align}
            1   &=  1\\
            2   &=  2\\
            \StepSubequations
            3   &=  3\\% from here a new equation number
            4   &=  4\\
            5   &=  5\\
            6   &=  6\\
            \StepSubequations
            7   &=  7\\
            8   &=  8\\
            \StepSubequations
            9   &=  9\\
        \end{align}
      \end{subequations}

\end{document}

Expected results:

(1aA)

(1aB)

(1bA)

(1bB)

(1bC)

(1bD)

(1cA)

(1cB)

(1d)

al b
  • 5

1 Answers1

0

I am not sure what you intend to do exactly and I also don't know what should happen in a subequations environment where you don't use \StepSubequations ... maybe it would be a good idea to create a new environment (as a copy of the oroginal one) that handles things as you want, so you can keep the original behaviour.

Anyways, something like this could work as starting point (but it won't suppress a single "A" as this would require to look ahead):

\documentclass{article}
\usepackage{amsmath}
\usepackage{etoolbox}
\newcounter{equationgroup}
\makeatletter
\patchcmd\subequations
    {\def\theequation{\theparentequation\alph{equation}}}
    {\def\theequation{\theparentequation\Alph{equation}}\ifmeasuring@\else\setcounter{equationgroup}{1}\fi}
    {}{}
\makeatother
\newcommand\StepSubequations{
    \stepcounter{equationgroup}
    \stepcounter{equation}
    \gdef\theparentequation{\arabic{parentequation}\alph{equationgroup}}
    \setcounter{equation}{0}
}

\begin{document} \begin{subequations} \begin{align} 1 &= 1\ 2 &= 2\ \StepSubequations 3 &= 3\% from here a new equation number 4 &= 4\ 5 &= 5\ 6 &= 6\ \StepSubequations 7 &= 7\ 8 &= 8\ \StepSubequations 9 &= 9 \end{align} \end{subequations}

\begin{subequations}
    \begin{align}
        1   &=  1\\
        2   &=  2\\
        \StepSubequations
        3   &=  3\\% from here a new equation number
        4   &=  4\\
        5   &=  5\\
        6   &=  6\\
        \StepSubequations
        7   &=  7\\
        8   &=  8\\
        \StepSubequations
        9   &=  9
    \end{align}
\end{subequations}

\end{document}

enter image description here


You could also place the redefinition into a command that you then need to execute (which would allow you to change between the original and the adjusted behaviour of the subequations environment more easily). So, maybe something like this:

\documentclass{article}
\usepackage{amsmath}
\newcounter{subequationsgroup}
\makeatletter
\AddToHook{env/subequations/after}{%
    \stepcounter{parentequation}%
    \gdef\theequation{\theparentequation}%
}
\newcommand{\InitializeSubequationsGroups}{%
    \ifmeasuring@\else%
        \setcounter{subequationsgroup}{\value{equation}}%
    \fi%
}
\newcommand{\StepSubequationsGroup}{%
    \ifmeasuring@\else%
        \stepcounter{subequationsgroup}%
        \setcounter{equation}{0}%
        \gdef\theequation{\theparentequation\alph{subequationsgroup}\Alph{equation}}%
    \fi%
}
\newcommand{\StepSubequationsSingle}{%
    \ifmeasuring@\else%
        \stepcounter{subequationsgroup}%
        \setcounter{equation}{0}%
        \gdef\theequation{\theparentequation\alph{subequationsgroup}}%
    \fi%
}
\makeatother

\begin{document} \begin{subequations} \begin{align} 1 &= 1\ 2 &= 2 \end{align} \end{subequations}

\begin{subequations}
    \begin{align}
        \InitializeSubequationsGroups
        \StepSubequationsGroup
        1   &=  1\\
        2   &=  2\\
        \StepSubequationsGroup
        3   &=  3\\% from here a new equation number
        4   &=  4\\
        5   &=  5\\
        \StepSubequationsSingle
        6   &=  6\\
        \StepSubequationsGroup
        7   &=  7\\
        8   &=  8\\
        1   &=  1\\
        2   &=  2
    \end{align}
\end{subequations}

\begin{subequations}
    \begin{align}
        1   &=  1\\
        2   &=  2
    \end{align}
\end{subequations}

\end{document}

enter image description here


My last try which should allow you to place more than one align inside of a subequations environment. You only need to take care where to place the \EndSubequationsGroup command if it comes last in the subequations environment:

\documentclass{article}
\usepackage{amsmath}
\newcounter{subequationsgroup}
\makeatletter
\AddToHook{env/subequations/before}{%
    \gdef\theequation{\arabic{equation}}%
}
\newcommand{\BeginSubequationsGroup}{%
    \ifmeasuring@\else%
        \setcounter{subequationsgroup}{\value{equation}}%
        \setcounter{equation}{0}%
        \stepcounter{subequationsgroup}%
        \gdef\theequation{\theparentequation\alph{subequationsgroup}\Alph{equation}}%
    \fi%
}
\newcommand{\EndSubequationsGroup}{%
    \ifmeasuring@\else%
        \setcounter{equation}{\value{subequationsgroup}}%
        \gdef\theequation{\theparentequation\alph{equation}}%
    \fi%
}
\makeatother

\begin{document} \begin{subequations} \begin{align} 1 &= 1\ 2 &= 2 \end{align} \end{subequations}

\begin{subequations}
    \begin{align}
        \BeginSubequationsGroup
        1   &=  1\\
        2   &=  2\\
        \EndSubequationsGroup
        3   &=  3\\
        \BeginSubequationsGroup
        4   &=  4\\
        5   &=  5\\
        \EndSubequationsGroup
        6   &=  6
    \end{align}
\end{subequations}

\begin{subequations}
    \begin{align}
        1   &=  1\\
        2   &=  2
    \end{align}

    \begin{align}
        \BeginSubequationsGroup
        1   &=  1\\
        2   &=  2
        %\EndSubequationsGroup
    \end{align}
\end{subequations}

\begin{subequations}
    \begin{align}
        1   &=  1\\
        \BeginSubequationsGroup
        2   &=  2\\
        3   &=  3\\
        \EndSubequationsGroup
        4   &=  4\\
        5   &=  5\\
        \BeginSubequationsGroup
        6   &=  6\\
        7   &=  7\\
        8   &=  8
    \end{align}

    \begin{align}
        \EndSubequationsGroup
        1   &=  1\\
        2   &=  2
    \end{align}
\end{subequations}

\end{document}

enter image description here

  • 1
    Remember also that align executes the code twice. (use \ifmeasuring@ ... \fi) – daleif Aug 07 '23 at 11:21
  • @daleif Hm, but in this case, I am actually unsure where to put these ... Do you mean \ifmeasuring@\else\setcounter{equationgroup}{1}\fi? – Jasper Habicht Aug 07 '23 at 11:35
  • Something like that. Otherwise it is executed in the measuring stage as well. – daleif Aug 07 '23 at 11:43
  • Thanks for your answer. That is what I'm was trying. But, when I don't use \StepSubequations, the numbers are printed in uppercase, which I expected to be compatible with the default lowercase numbering! How can I fix this? – al b Aug 07 '23 at 13:26
  • I changed the places of \Alph and \alph with each other, but it didn't work! – al b Aug 07 '23 at 13:32
  • @alb This is what I meant with "better create a copy of the original environment". There is no easy solution for both at once. See my edit for a possible workaround. – Jasper Habicht Aug 07 '23 at 13:36
  • @JasperHabicht Thanks so much. Can I ask about your modification? I could not notice! – al b Aug 07 '23 at 13:41
  • @alb You might need to reload the page to see the new code in my answer. I added another slight modification of the code under the original one. It does not patch but defines a new command, but for the most part, the code is the same. – Jasper Habicht Aug 07 '23 at 13:43
  • Also Is there any way to print the equation without group ((1dA) in this example) without the second term (capital letter for representing the group) ((1dA)-->(1d)) – al b Aug 07 '23 at 13:43
  • I want to use two align environments within the subequations, but it continued to number as the previous group. – al b Aug 07 '23 at 14:03
  • @JasperHabicht Thanks so much and so sorry for the broken questions. – al b Aug 07 '23 at 15:57
  • Just \AddToHook{env/subequations/after}{ produces error and when commenting it, it's gotten1 = 1 (2dCa) 2 = 2 (2dCb) for the last set of equations without error (that in your results it shows true (3a) and (3b)! – al b Aug 07 '23 at 16:33
  • could you please solve this little problem within comments, so that there is no need for another question – al b Aug 07 '23 at 16:35
  • @alb Well, it is not completely trivial, because the subequations environment already changes the regular equation counter. Now, in your case, the result needs to be split up, because every equation steps up the equation` counter, but you want them grouped party. I need to think again how to best do that. It is probably not too complicated, but it might be necessary to switch to another approach altogether. – Jasper Habicht Aug 07 '23 at 16:36
  • @JasperHabicht Thanks again. So, do you let me to accept the answer after fixing this issue? – al b Aug 07 '23 at 16:45
  • I think we just need to define a thing similar to \InitializeSubequationsGroups to put at the end of subequations environment to reset the numbering to the default form. Is this true? – al b Aug 07 '23 at 16:47
  • @alb See my latest edit. Feel free to play around with it and improve it ... and it is totally up to you to upvote and/or accept the answer =) – Jasper Habicht Aug 07 '23 at 16:54
  • @alb You are right more or less. My last approach does about this: it just switches between both numbering styles – Jasper Habicht Aug 07 '23 at 17:00
  • Why I got error in the \AddToHook{env/subequations/after}{ line? Also, the results are odd! such as 1 = 1 (2ca) 2 = 2 (2cb) 1 = 1 (2ccA) 2 = 2 (2ccB) 1 = 1 (3cDa) 2 = 2 (3cDbA) 3 = 3 (3cDbB) 4 = 4 (3cDc) 5 = 5 (3cDd) 6 = 6 (3cDeA) 7 = 7 (3cDeB) 8 = 8 (3cDeC) 1 = 1 (3cDf) 2 = 2 (3cDg). Did you exactly run your latest code version? – al b Aug 07 '23 at 17:04
  • There is need to a \newcommand, in which we define a \stepcounter to stop \setcounter and put it at the end of each subequations environment that we define \InitializeSubequationsGroups at the top of it. – al b Aug 07 '23 at 17:09
  • @alb You can be assured that I always run my code and test it with an up-to-date TeX distribution before posting here. I get the result I showed in the picture. This should run fine with TeX Live 2021 or newer (or a distribution of comparable actuality). – Jasper Habicht Aug 07 '23 at 17:12
  • I had not any means, since I just exactly copied and pasted your code in the Overleaf! – al b Aug 07 '23 at 17:16
  • Where do you think the problem is? Since I got error in the \AddToHook{env/subequations/after}{ !. l.5 \AddToHook {env/subequations/after}{% The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g.,\hobx'), type I' and the correct spelling (e.g.,I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined.` – al b Aug 07 '23 at 17:18
  • Switch to a current TeX Live version in Overleaf. Click on "Menu" on the upper left, and then change the "TeX Live version" in the Settings section to the newest (2022 currently). Then, recompile. – Jasper Habicht Aug 07 '23 at 17:23
  • @JasperHabicht It was solved. So thanks. – al b Aug 07 '23 at 17:27