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.

5
  • 5
    Have a look at the cleveref package
    – cmhughes
    Commented Nov 23, 2011 at 16:56
  • I did look at the cleveref package, but I don't think it can do what I'm asking.
    – AndyS
    Commented Nov 23, 2011 at 17:10
  • @AndyS: Do you want to use hyperref? Commented Nov 27, 2011 at 19:03
  • @Marco: Yes, I would like to use hyperref.
    – AndyS
    Commented Nov 27, 2011 at 19:16
  • 2
    Adding a comment several years later as I think there was a flexible and powerful solution not using the cref package posted by @Werner here: tex.stackexchange.com/questions/35921/…
    – TSGM
    Commented Jul 8, 2015 at 10:08

2 Answers 2

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).

1
  • Welcome to TeX.SX. You can use backticks (`) to highlight inline code. Commented May 9, 2013 at 18:47
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}
2
  • Thanks Peter, I did look into cleveref, but I wasn't able to modify its behavior to fit my needs. Any other ideas?
    – AndyS
    Commented Nov 24, 2011 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. Commented Nov 24, 2011 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .