# Block of equations with only some being subequations and cleveref

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}                            \\
\end{align}

\end{document}


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

Is there any way to get around this problem?

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

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}
\\
\tagsubequation\label{eq:prefykb}
\\
\label{eq:prefe}
\end{align}

Let's see the next number
$$a=b$$

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

\cref{eq:prefykb}

\end{document}


• 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 ;) Jul 6 '16 at 7:40

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}                            \\

• 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 ;) Jul 6 '16 at 7:41