# Cleveref and breqn packages don't play nice together

I have been using the breqn and cleveref packages independently, and I think they are both excellent packages; but recently I have tried using them together, but it appears that cleveref cannot identify the dmath environment so that all my references to equations reference the containing section. Is there a way to make these package work together?

Here's a MWE illustrating the problem:

\documentclass{article}
\usepackage{breqn}
\usepackage{cleveref}

\begin{document}

\section{Content}

\begin{dmath}\label{equ:test}
a=d+c
\end{dmath}
\cref{equ:test}

\end{document}


-
Please provide a "Minimal Working Example" (MWE) of your code. –  Mico Sep 15 '11 at 18:16
Please, as Mico suggested, post a MWE of your code. I did it for you in my edit (for the sake of the quality of the question) but you are free to roll back or add an MWE of your own if you want to. –  Gonzalo Medina Sep 15 '11 at 18:28

An alternative answer, which allows you to use \label as normal:

The problem is caused by the fact that cleveref relies on redefining \refstepcounter, while breqn doesn't actually use this to advance the equation counter. Fortunately the breqn code for dealing with footnotes, which don't use \refstepcounter either, provides a basis for dealing with dmaths as another special case by adding some code to \eq@setnumber:

\makeatletter
\let\cref@old@eq@setnumber\eq@setnumber
\def\eq@setnumber{%
\cref@old@eq@setnumber%
\cref@constructprefix{equation}{\cref@result}%
\protected@xdef\cref@currentlabel{%
[equation][\arabic{equation}][\cref@result]\p@equation\theequation}}
\makeatother


Including this code in the preamble yields the correct result for the MWE.

-
This works perfectly! Thanks for the quick response Ant! –  Craig Sep 15 '11 at 22:18

Not ideal, but a workaround is to add an optional argument equation to each label:

\label[equation]{equ:test}

-
it may not be ideal, but it's probably the best you are going to achieve. (the author of cleveref could in principle read all packages and identify labellable envs that they introduce so as to program round them, but it's not likely to happen, is it?) –  wasteofspace Sep 15 '11 at 21:52
Fortunately not every environment needs to be dealt with individually. If the environment number is incremented with \refstepcounter then cleveref should work automagically. If not, you can insert the patch of \refstepcounter into whatever command is used instead by hand - see my other answer. –  Ant Sep 15 '11 at 22:02
This doesn't work for me. The dmath displays as equation, but the counters aren't right. Your other answer appears to be required. –  Peeter Joot Feb 11 '13 at 15:05
This doesn't work, the argument to \label becomes just the opening bracket [. I guess options to \label require a special package? –  Ben Voigt Apr 28 at 22:51

I used to use Ant's makeatletter method, but with the packages contained in the Feb 2014 MikTex 2.9 installation:

Package: cleveref 2013/12/28 v0.19 Intelligent cross-referencing
Package: breqn 2012/05/10 v0.98b Breaking equations


this starts causing:

TeX capacity exceeded, sorry [input stack size=5000]


errors.

It also seems like dmath and cleverref now play together well by default, so upgrading the latex packages now appears to be a viable solution.

-