# How to get rid of white space between two subequations enviroments?

I have a question, how to remove these white spaces between two subequations environments (marked with red '?'). I'm looking for the solution without any \vspace{} commands. I want to fix it automatically.

Here is the MWE:

 \documentclass[preview,border={10pc 2pc 10pc 2pc}]{standalone}
\usepackage{amsmath}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
\begin{subequations}
\begin{flalign}
& f_1(x) = x^2 &\\
& f_2(x) = x^2 &
\end{flalign}
\end{subequations}
\begin{subequations}
\begin{flalign}
& g_1(x) = x^2 &\\
& g_2(x) = x^2 &
\end{flalign}
\end{subequations}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

\end{document}


Here is a variant of subequations that allows for stepping the parent equation counter.

\documentclass{article}
\usepackage{amsmath}
\usepackage{etoolbox}

\AtBeginDocument{\let\standardtheequation\theequation}
\newenvironment{subequations*}
{%
\subequations
\counterwithin*{equation}{parentequation}%
\let\theequation\standardtheequation
\patchcmd{\theequation}{equation}{parentequation}{}{}%
\apptocmd{\theequation}{\alph{equation}}{}{}%
\newcommand{\substep}{\stepcounter{parentequation}}%
}
{\endsubequations}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
do eiusmod tempor incididunt ut labore et dolore magna aliqua.
\begin{subequations*}
\begin{align}
f_1(x) &= x^2 \\
f_2(x) &= x^2 \\
\substep
g_1(x) &= x^2 \\
g_2(x) &= x^2
\end{align}
\end{subequations*}
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

\end{document}


With some twisting you can also refer to the global numbers. The label for the \substep number is input as an optional argument to \substep.

\documentclass{article}
\usepackage{amsmath}
\usepackage{etoolbox}

\AtBeginDocument{\let\standardtheequation\theequation}
\makeatletter
\newenvironment{subequations*}
{%
\subequations
\counterwithin*{equation}{parentequation}%
\let\theequation\standardtheequation
\patchcmd{\theequation}{equation}{parentequation}{}{}%
\apptocmd{\theequation}{\alph{equation}}{}{}%
\let\theparentequation\standartheequation
\newcommand{\substep}[1][]{%
\refstepcounter{parentequation}%
\if\relax\detokenize{##1}\relax\else
\ltx@label{##1}%
\fi
}%
}
{\endsubequations}
\makeatother

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
do eiusmod tempor incididunt ut labore et dolore magna aliqua.
\begin{subequations*}\label{first}
\begin{align}
f_1(x) &= x^2 \label{first-a} \\
f_2(x) &= x^2 \label{first-b} \\
\substep[second]
g_1(x) &= x^2 \label{second-a} \\
g_2(x) &= x^2 \label{second-b}
\end{align}
\end{subequations*}
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

\ref{first} \ref{first-a} \ref{first-b}

\ref{second} \ref{second-a} \ref{second-b}

\end{document}


• Thank you for the answer, in yours solution can I easily \ref to just (2) without letters? – andywiecko Jan 10 '19 at 12:35
• @andywiecko Added – egreg Jan 10 '19 at 13:59

That looks wrong to me to have no vertical space because that are different equations. However:

\documentclass[preview,border={10pc 2pc 10pc 2pc}]{standalone}
\usepackage{amsmath}
\newcommand\IncCnt{\refstepcounter{parentequation}\setcounter{equation}{0}%
\gdef\theparentequation{\arabic{parentequation}}}
\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua.
\begin{subequations}
\begin{flalign}
& f_1(x) = x^2 &\\
& f_2(x) = x^2 &\\\IncCnt
& g_1(x) = x^2 &\\
& g_2(x) = x^2 &\label{foo}
\end{flalign}
\end{subequations}

foo

\begin{align}
f_1(x) &= x^2
\end{align}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua.~\ref{foo}

\end{document}


• Thank you for the answer. I agree with you about having no space is wrong. However, sometimes we do not have any influence on the orders :) – andywiecko Jan 10 '19 at 12:19
• ... but how to \ref{} only for (2) without letters? – andywiecko Jan 10 '19 at 12:26
• That is only possible for (1). Otherwise it needs some more important changes. – user2478 Jan 10 '19 at 12:44

I wouldn't use multiple math environments but change the actual numbering inside of the environment. The following code solves your problem, but it does not work automatically yet. If you figure out how to increase the counter of the subequation environment, you might find an automatic solution.

\documentclass[preview,border={10pc 2pc 10pc 2pc}]{standalone}
\usepackage[fleqn]{amsmath}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor     incididunt ut labore et dolore magna aliqua.
\begin{subequations}
\begin{gather}
f_1(x) = x^2 \\
f_2(x) = x^2 \\
g_1(x) = x^2 \tag{2a}\\
g_2(x) = x^2 \tag{2b}
\end{gather}
\end{subequations}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

\end{document}