25

I'm trying to write a macro that puts subequations' labels in parentheses in a compact form. For example, if I have

\begin{subequations}
\begin{align}
    y - ax &= b \label{subeq1}\\
    x^2 + y^2 &= r^2 \label{subeq2}
\end{align}
\end{subequations}

with the two equations being tagged as (1a) and (1b), then I want to be able to type \ref{subeq1,subeq2} and get (1a,b). Similarly, if I have three subequations, say (2a), (2b) and (2c) with labels subeq3, subeq4 and subeq5 respectively, then I want to be able to type \ref{subeq3,subeq4,subeq5} and get (2a--c). I would like this to work for any number of subequations.

Moriambar
  • 11,466
AndyS
  • 702

2 Answers2

17

I have a solution that isn't quite what you wanted, but it gives more compact label. The code I would use is:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
    \begin{subequations}\label{eq:subeqns}
     \begin{align}
      y - ax &= b \label{eq:subeq1}\\
      x^2 + y^2 &= r^2 \label{eq:subeq2}
     \end{align}
    \end{subequations}
\end{document}

I reference the entire set as \eqref{eq:subeqns} to get (1), and individuals as \eqref{eq:subeq1} or \eqref{eq:subeq2} to get (1a) or (1b).

Troy
  • 13,741
user30453
  • 171
15

This isn't exactly what you wanted but is close.

enter image description here

Note:

  • The latest version (V0.18) is not yet on CTAN, but can be obtained from Toby Cubitt's site. Though it does no appear to make a difference in this particular example.

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}% load last

\crefname{equation}{equation}{equations} \Crefname{equation}{Equation}{Equations}% For beginning \Cref \crefrangelabelformat{equation}{(#3#1#4--#5#2#6)}

\crefmultiformat{equation}{equations (#2#1#3}{, #2#1#3)}{#2#1#3}{#2#1#3} \Crefmultiformat{equation}{Equations (#2#1#3}{, #2#1#3)}{#2#1#3}{#2#1#3}

\begin{document}

\begin{subequations} \begin{align} y - ax &= b \label{eq:subeq1}\ x^2 + y^2 &= r^2 \label{eq:subeq2}\ x^2 + y^2 &= r^2 \label{eq:subeq3}\ x^2 + y^2 &= r^2 \label{eq:subeq5} \end{align} \end{subequations}

\noindent For the case where the label is part of a sentence:\par Once referenced: \cref{eq:subeq1}\par Two referenced: \cref{eq:subeq1,eq:subeq2}\par Three referenced: \cref{eq:subeq1,eq:subeq2,eq:subeq3}\par

\bigskip \noindent For the case where the labels beign a sentence:\par \Cref{eq:subeq1} is one equations.\par \Cref{eq:subeq1,eq:subeq2} are two equations.\par \Cref{eq:subeq1,eq:subeq2,eq:subeq3} are three equations. \end{document}

Peter Grill
  • 223,288
  • Thanks Peter, I did look into cleveref, but I wasn't able to modify its behavior to fit my needs. Any other ideas? – AndyS Nov 24 '11 at 17:10
  • I'd wait to see if someone else comes up with a better solution. Otherwise, it should be possible to post process the output of the above cref to remove duplicated numbers to get exactly what you want, but I think that should be a separate question as I am not able to figure out how to exactly do that. – Peter Grill Nov 24 '11 at 17:29