[Tex/LaTex] Equation reference undefined when using \cref and package{amsmath}

amsmathcleverefcross-referencingequations

When I attempt to reference an equation using \cref{..} while also using the package amsmath the references appear as ?? in the outputted pdf. For example

\documentclass[]{article}
\usepackage{graphicx}
\usepackage{cleveref} %for cref
\usepackage{amsmath} %for sub equation numbers and words within equations (\text)

\begin{document}


\section{Introduction}

    \begin{equation}\label{eq:solve}
    x^2 - 5 x + 6 = 0 
    \end{equation}


    Ref: \ref{eq:solve}

    CRef: \cref{eq:solve}

\end{document}

I get the output:

enter image description here

However if the \usepackage{amsmath} is commented out the \cref works correctly.

enter image description here

Is it possible to use both together successfully?

Best Answer

I tried to look for other questions with the same problem and found none. So here's an answer: load cleveref last.

From the documentation:

The cleveref package is loaded in the usual way, by putting the line

\usepackage{cleveref}

in your document’s preamble. However, care must be taken when using cleveref in conjunction with other packages that modify LaTeX’s referencing system (see Section 11). Basically, cleveref must be loaded last.

The problem is that amsmath modifies the referencing system, in order to cope with \tag, for instance. So cleveref has to hook into the redefined macros, rather than the original ones. By loading amsmath after cleveref you don't give the package the chance to know amsmath is also requested.

Related Question