Q:

breqn and hyperref breaking subnumcases environment when used together

I have an issue when using cases, breqn and hyperref together. This is my MWE
\documentclass[preview]{standalone}
\usepackage{cases}
\usepackage{breqn} % For line breaks in some really long equations in another part of the document
\usepackage{hyperref} 

\begin{document}
\begin{subnumcases}{\label{eq:case} x =}
a+b, & example 1\\
c+d, & example 2.
\end{subnumcases}
This text refers to \ref{eq:case}.
\end{document}

When compiling my thesis with breqn loaded I get the image below, where apparently the label isn't set and x has disappeared.

When I don't load breqn I get the behaviour I want but at the expense of the equations that do require breqn later on in my thesis:

And when I don't load hyperref I also get useable output but with the links in the final PDF. Is there a way to keep breqn and hyperref without breaking my subnumcases environments?

A:

Don't ask me how I found this out, I just drink too much coffee these days.
\documentclass{article}
\usepackage{cases}
\usepackage{breqn} % For line breaks in some really long equations in another part of the document
\usepackage{hyperref} 

% Patching the numcases environment from 
% cases.sty for compatibility with breqn+hyperref
% Source of problem: use of \box\z@, but \box\z@ somehow
% is used by some other macro at some point when breqn+hyperef
% are used.
\usepackage{etoolbox}
\makeatletter
\newbox\boxzero
\patchcmd{\numcases}{\setbox\z@}{\setbox\boxzero}{}{}
\patchcmd{\numcases}{\kern\wd\z@}{\kern\wd\boxzero}{}{}
\patchcmd{\endnumcases}{\displaystyle \box\z@}{\displaystyle\box\boxzero}{}{}
\let\endsubnumcases\endnumcases
\makeatother

\begin{document}
\begin{subnumcases}{\label{eq:case}x =}
a+b, & example 1\\
c+d, & example 2.
\end{subnumcases}

This text refers to \ref{eq:case}.
\end{document}

Output:

