3

I've been using the answers developed in this question in order to have mixed equations and sub-equation numbering inside of an align environment, MWE:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{etoolbox}
\usepackage[breaklinks=true, linkcolor=blue, citecolor=blue, colorlinks=true]{hyperref}

% subequations inside of align
% https://tex.stackexchange.com/questions/34566/mixed-subequation-numbering-within-an-array
\AtBeginEnvironment{align}{\setcounter{subeqn}{0}}% Reset subequation number at start of align
\newcounter{subeqn} \renewcommand{\thesubeqn}{\theequation\alph{subeqn}}%
\newcommand{\subeqn}{%
  \refstepcounter{subeqn}% Step subequation number
  \tag{\thesubeqn}% Label equation
}
\newcommand{\beginsubeqn}{\setcounter{subeqn}{0}\refstepcounter{equation}\subeqn}

\begin{document}
\begin{align}
 \frac{\partial A }{\partial x } &= a &\;,& \label{e:first} \\
 \frac{\partial A }{\partial y } &= b &\;,& \label{e:second} \\
 \intertext{and:}
 \frac{\partial A}{\partial z } &= c &\text{if type Z,}&\beginsubeqn \\
 \frac{\partial A }{\partial w } &= d &\text{if type W,}&\subeqn
 \label{e:last}
\end{align} 

Eqs.~\ref{e:first}~and~\eqref{e:second} are fine, but I'd like Eq.~\ref{e:last} to show as Eq.~(3).
It's easy to do with a regular `subequations` environment:
\begin{subequations}
 \label{e:goodref}
 \begin{align}
  a&=b \\
  b&=c
 \end{align}
\end{subequations}
See? Eq.~\ref{e:goodref}.
\end{document}

and result:

enter image description here

However, this is a little bit short of my requirements. I need to be able to refer to the sub-equations as a single equation. This is easy to do with a sub-equations environment (see MWE), which unfortunately breaks inside of align.

Is this possible?

edit: I found a hackish solution using cleverref and xstring:

\documentclass[12pt]{article}
\usepackage{etoolbox}
\usepackage{amsmath}
\usepackage{xstring}
\usepackage[breaklinks=true, linkcolor=blue, citecolor=blue, colorlinks=true]{hyperref}
\usepackage[english,capitalise]{cleveref}

% subequations inside of align
% https://tex.stackexchange.com/questions/34566/mixed-subequation-numbering-within-an-array
\AtBeginEnvironment{align}{\setcounter{subeqn}{0}}% Reset subequation number at start of align
\newcounter{subeqn} \renewcommand{\thesubeqn}{\theequation\alph{subeqn}}%
\newcommand{\subeqn}{%
  \refstepcounter{subeqn}% Step subequation number
  \tag{\thesubeqn}% Label equation
}
\newcommand{\beginsubeqn}{\setcounter{subeqn}{0}\refstepcounter{equation}\subeqn}

% remove last character if letter
\newcommand{\removeendalpha}[1]{\expandarg\def\last{\StrRight{#1}{1}}\def\withoutlast{\StrGobbleRight{#1}{1}}\IfInteger{\last}{#1}{\withoutlast}}


% define cref that labels subequations as parent
\crefname{subasparent}{eq.}{eqs.}
\crefformat{subasparent}{(#2\removeendalpha{#1}#3)}

\begin{document}
\begin{align}
 \frac{\partial A }{\partial x } &= a &\;,& \label{e:first} \\
 \frac{\partial A }{\partial y } &= b &\;,& \label{e:second} \\
 \intertext{and:}
 \label[subasparent]{e:last}
 \frac{\partial A}{\partial z } &= c &\text{if type Z,}&\beginsubeqn \\
 \frac{\partial A }{\partial w } &= d &\text{if type W,}&\subeqn
\end{align} 

\cref{e:first,e:second,e:last}, I'd like~\cref{e:last} to show as Eq.~(3).
It's easy to do with a regular `subequations` environment:
\begin{subequations}
 \label{e:goodref}
 \begin{align}
  a&=b \\
  b&=c
 \end{align}
\end{subequations}
See?~\cref{e:goodref}.
\end{document}

enter image description here

But now I can't figure out how to have the "Eq." part of the reference appear and the equations don't properly combine anymore (... Eqs. (1) and (2) and (3) instead of Eqs. (1) to (3))

edit2: Implementing using Werner's suggestion

\documentclass[12pt]{article}
\usepackage{amsmath,amssymb,eqparbox}
\usepackage[breaklinks=true, linkcolor=blue, citecolor=blue, colorlinks=true]{hyperref}
\usepackage[english,capitalise]{cleveref}

\begin{document}
\begin{align}
 \frac{\partial [X]_i }{\partial T } &= - \frac{[C] \alpha_{{N_s},i}}{T} & \eqmakebox[cond][r]{,} \label{e:dci_thd_dt} \\
 \frac{\partial [X]_i }{\partial n_{j} } &= \frac{1}{V} \left(- \alpha_{{N_s},i} + \alpha_{j,i}\right) & \eqmakebox[cond][r]{,} \label{e:dci_thd_dnj}
\end{align}
and:
\begin{subequations}
 \label{e:dci_thd_de}
 \begin{align}
  \frac{\partial [X]_i }{\partial V } &= \frac{1}{V} \left([C] \alpha_{{N_s},i} - [X]_{i}\right) & \eqmakebox[cond][r]{,\qquad\text{for CONP}}\\
  \frac{\partial [X]_i }{\partial P } &= \frac{\alpha_{{N_s},i}}{T R_u} & \eqmakebox[cond][r]{,\qquad\text{for CONV}}
 \end{align}
\end{subequations}
\end{document}

I have slightly broken alignment in the equals sign (a bit more pronounced in my actual document):

enter image description here

  • It seems like your main concern here is that you want to have an align with \intertext and then mixed in some subequations. The reason for wanting to stick to align is because you want the two blocks with partial derivatives to remain aligned across the \intertext. Correct? – Werner Dec 22 '17 at 17:19
  • Correct, I know I can get around it with some \hphantoms and splitting the align into two separate ones, but that's a pretty ugly solution as well (e.g., if you ever change the longest entry, you have to update all the \hphantoms as well) – arghdos Dec 22 '17 at 17:47
2

I'd split the mixed align-and-\intertext into two separate aligns, with the latter using a subequations environment. Then, of course, you're stuck trying to fiddle with lengths to ensure the aligns still align horizontally across your original \intertext. For this, eqparbox can help. Set the content of your construction in boxes that automatically change to the size of the largest element.

enter image description here

\documentclass{article}

\usepackage{amsmath,eqparbox,xparse}
\usepackage{hyperref}

% https://tex.stackexchange.com/a/34412/5764
\makeatletter
\NewDocumentCommand{\eqmathbox}{o O{c} m}{%
  \IfValueTF{#1}
    {\def\eqmathbox@##1##2{\eqmakebox[#1][#2]{$##1##2$}}}
    {\def\eqmathbox@##1##2{\eqmakebox{$##1##2$}}}
  \mathpalette\eqmathbox@{#3}
}
\makeatother

\begin{document}

\begin{align}
    \eqmathbox[LHS][r]{ 123\frac{\partial A}{\partial x}} &= \eqmathbox[RHS][l]{abcd}           & \eqmakebox[cond][r]{,} \label{e:first} \\
    \eqmathbox[LHS][r]{  45\frac{\partial A}{\partial y}} &= \eqmathbox[RHS][l]{e}              & \eqmakebox[cond][r]{,} \label{e:second}
\end{align}
and:
\begin{subequations}
  \label{e:last}
  \begin{align}
    \eqmathbox[LHS][r]{   6\frac{\partial A}{\partial z}} &= \eqmathbox[RHS][l]{fghijkl}        & \eqmakebox[cond][r]{if type $Z$,} \\
    \eqmathbox[LHS][r]{7890\frac{\partial A}{\partial w}} &= \eqmathbox[RHS][l]{mnopqrstuvwxyz} & \eqmakebox[cond][r]{if type $W\!$,}
  \end{align} 
\end{subequations}

Eqs.~\eqref{e:first}~and~\eqref{e:second} are fine, but I'd like Eq.~\eqref{e:last} to show as Eq.~(3).
It's easy to do with a regular \verb|subequations| environment:
\begin{subequations}
  \label{e:goodref}
  \begin{align}
    a &= b \\
    b &= c
  \end{align}
\end{subequations}

See? Eq.~\eqref{e:goodref}.

\end{document}

In the above code, \eqmakebox[<tag>][<align>]{<stuff>} will ensure that all <stuff> under the same <tag> will have the same, largest width. Individual boxes can be <align>ed as needed (default is centred). However, <stuff> is always set in text mode. We use \mathpalette to capture the current math style (one of \displaystyle, \textstyle, \scriptstyle or \scriptscriptstyle) and use that to set <stuff> in the appropriate style, all wrapped inside \eqmathbox.

For the sake of demonstration, I've added some content to the left-hand side, right-hand side and conditions, just to show you that the use-case is flexible. Since eqparbox uses a \label-\ref-like evaluation process for establishing the widest box width for each <tag>, at least two compilations are required whenever <stuff> changes within a specific <tag> to let references (calculations) settle.

| improve this answer | |
  • This mostly works -- I have a slightly misaligned equals sign between the two align environments, but I'm going to call it good enough (using \eqmakebox on the LHS causes errors even with a preceeding \displaystyle) – arghdos Dec 22 '17 at 18:41
  • @arghdos: See the updated post where I provide \eqmathbox that captures and sets the appropriate math style content. – Werner Dec 22 '17 at 20:27
  • @Werner -- "at least compilations are required" ... how many? – barbara beeton Dec 25 '17 at 1:37
  • @barbarabeeton: Fixed, thanks. Two, as usual. – Werner Dec 25 '17 at 3:08
  • @Werner thank you very much for all your help, – arghdos Jan 2 '18 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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