5

I recently asked a question regarding the numbering of a block of equations within an align environment of which only some should be numbered as subequations; in my case I had three equations that I wanted to be numbered (1a), (1b), (2).

I was pointed to an already proposed solution here, which I tested and it is indeed working, but I found out that this solution fails when including the cleveref package. Here you can see the MWE:

\documentclass{article}
\usepackage{amsmath}

\usepackage{cleveref} % Comment/uncomment to see the difference

\makeatletter
\newcounter{manualsubequation}
\renewcommand{\themanualsubequation}{\alph{manualsubequation}}
\newcommand{\startsubequation}{%
  \setcounter{manualsubequation}{0}%
  \refstepcounter{equation}\ltx@label{manualsubeq\theequation}%
  \xdef\labelfor@subeq{manualsubeq\theequation}%
}
\newcommand{\tagsubequation}{%
  \stepcounter{manualsubequation}%
  \tag{\ref{\labelfor@subeq}\themanualsubequation}%
}
\let\subequationlabel\ltx@label
\makeatother

\begin{document}

\begin{align}
    % With 2 elements
    \startsubequation\subequationlabel{eq:prefyk}
    a &= \begin{bmatrix}
                                a_1 \\
                                \vdots \\
                                a_2
                            \end{bmatrix} 
    \tagsubequation\label{eq:prefyka}                            \\
    &= \left[ a_1 \quad\!\! \ldots \quad\!\! a_2 \right]^\mathrm{T}     \tagsubequation\label{eq:prefykb}\\
    b &= \left[ b_1 \quad\!\! \ldots \quad\!\! b_2 \right]^\mathrm{T}, \label{eq:prefe}
\end{align}

\end{document}

If the line \usepackage{cleveref} is included, the result is the following.

Wrong numbering

Is there any way to get around this problem?

1
  • Looking at the construction of the references I am not sure this is the best way, actually
    – user31729
    Jul 5 '16 at 16:35
3

Some changes need to be done with cleveref, because it modifies several things under the hood.

\documentclass{article}

\usepackage{amsmath}
\usepackage{cleveref}

\makeatletter
\newcounter{manualsubequation}
\renewcommand{\themanualsubequation}{\alph{manualsubequation}}
\newcommand{\startsubequation}{%
  \setcounter{manualsubequation}{0}%
  \refstepcounter{equation}\ltxcref@label{manualsubeq\theequation}%
  \xdef\labelfor@subeq{manualsubeq\theequation}%
}
\newcommand{\tagsubequation}{%
  \stepcounter{manualsubequation}%
  \tag{\ref{\labelfor@subeq}\themanualsubequation}%
}
\newcommand\ltxcref@label[1]{%
  \protected@write\@auxout{}
    {\string\newlabel{#1}{{\@currentlabel}{\thepage}}}%
  % also do the cleveref label
  \protected@write\@auxout{}
    {\string\newlabel{#1@cref}{{\cref@currentlabel}{\thepage}}}%
}
\let\subequationlabel\ltxcref@label
\makeatother

\begin{document}

\begin{align}
% With 2 elements
\startsubequation\subequationlabel{eq:prefyk}
  a &= \begin{bmatrix}
       a_1 \\
       \vdots \\
       a_2
       \end{bmatrix}
  \tagsubequation\label{eq:prefyka}
\\
    &= \left[ a_1 \quad\!\! \ldots \quad\!\! a_2 \right]^\mathrm{T}
       \tagsubequation\label{eq:prefykb}
\\
  b &= \left[ b_1 \quad\!\! \ldots \quad\!\! b_2 \right]^\mathrm{T},
\label{eq:prefe}
\end{align}

Let's see the next number
\begin{equation}
a=b
\end{equation}

Also references: \eqref{eq:prefe}, \eqref{eq:prefyka} and \eqref{eq:prefykb}

\cref{eq:prefykb}

\end{document}

enter image description here

1
  • Great! This works fine. As a related note, I tried to call the references with cleveref using \cref{eq:prefyka,eq:prefykb,eq:prefe}, which should produce the string eq. (1a), (1b) and (2), but for some reason it produced the string eq (2), (1a) and (1b). Anyway, that is not a problem for me, at the moment ;)
    – gbernardi
    Jul 6 '16 at 7:40
2

The problem is, that cleveref redefines \label to have an optional argument which may be given to override the type of the label. In this case the \ltx@label must fail unless the type is given, i.e. subequation.

In order to use the full cleveref support, provide the \crefname names:

\documentclass{article}

\usepackage{amsmath}

\makeatletter
\newcounter{manualsubequation}
\renewcommand{\themanualsubequation}{\alph{manualsubequation}}
\newcommand{\startsubequation}{%
  \setcounter{manualsubequation}{0}%
  \refstepcounter{equation}\ltx@label[subequation]{manualsubeq\theequation}%
  \xdef\labelfor@subeq{manualsubeq\theequation}%
}
\newcommand{\tagsubequation}{%
  \refstepcounter{manualsubequation}%
  \tag{\ref{\labelfor@subeq}\themanualsubequation}%
}
\let\subequationlabel\ltx@label
\makeatother


\usepackage{cleveref} % Comment/uncomment to see the difference



\crefname{subequation}{subequation}{subequations}
\Crefname{subequation}{Subequation}{Subequations}


\begin{document}

\begin{align}
    % With 2 elements
    \startsubequation\subequationlabel{eq:prefyk}
    a &= \begin{bmatrix}
                                a_1 \\
                                \vdots \\
                                a_2
                            \end{bmatrix} 
    \tagsubequation\label{eq:prefyka}                            \\
    &= \left[ a_1 \quad\!\! \ldots \quad\!\! a_2 \right]^\mathrm{T}     \tagsubequation\label{eq:prefykb}\\
    b &= \left[ b_1 \quad\!\! \ldots \quad\!\! b_2 \right]^\mathrm{T}, \label{eq:prefe}
\end{align}

In \cref{manualsubeq1} we saw that


\end{document}

enter image description here

1
  • This works fine, too. Thanks @Christian Hupfer. As for the other solution, I tried to call the references with cleveref using \cref{eq:prefyka,eq:prefykb,eq:prefe}, which should produce the string eq. (1a), (1b) and (2), but for some reason it produced the string eq (2), (1a) and (1b). Anyway, that is not a problem for me, at the moment ;)
    – gbernardi
    Jul 6 '16 at 7:41

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.