5

Because I have many equations in my document and most of them do not need to be referred to, I always use \begin{align*}...\end{align*}. For the times I do need an equation number I have written a command

\newcommand{\nr}{\addtocounter{equation}{1}\tag{\theequation}}

This works great, only cleveref does not sort it. An example

\documentclass{article}
\usepackage{amsmath,cleveref}
\newcommand{\nr}{\addtocounter{equation}{1}\tag{\theequation}}

\begin{document}
\begin{align*}
  foo \nr\label{eq1}\\
  bar \nr\label{eq2}
\end{align*}

\cref{eq2,eq1}

\end{document}  

This outputs "eqs. (2) and (1)" instead of "eqs. (1) and (2)". Is there a way to fix this?

0

2 Answers 2

3

(Not a solution, just a deeper analysis of the problem.)

It appears that the problem is related to the way the \tag macro is defined by the amsmath package. In the code given below, the equation numbers are sorted correctly if one (i) defines two unnumbered displayed equations via \[...\] and (ii) uses \eqno (a "primitive" TeX macro) instead of \tag. Unfortunately, the amsmath package does not permit using \eqno in its math environments.

enter image description here

\documentclass{article}
\usepackage{amsmath,cleveref}
\setlength\textwidth{2in}  % just for this example

\newcommand{\nr}[1]{%  first method uses \tag
     \refstepcounter{equation}\label{#1}\tag{\theequation}}
\newcommand{\nrx}[1]{% second method uses \eqno
     \refstepcounter{equation}\label{#1}\eqno(\theequation)}

\begin{document}

\begin{align*}
  foo \nr{eq1}\\
  bar \nr{eq2}
\end{align*}
\cref{eq2,eq1}

\[ newfoo \nrx{eq3} \]
\[ newbar \nrx{eq4} \]
\cref{eq4,eq3}

\end{document}  
2
  • thanks for your research. Now I have another question: why does \addtocounter{equation}{1} instead of \refstepcounter{equation} in the second command give me errors? Commented Jan 20, 2015 at 18:57
  • 1
    @NikkiBisschop - I assume you're getting the error message "Missing number, treated as zero." As a rule, use only \refstepcounter to increment a counter if cross-referencing to the numbered item is an objective.
    – Mico
    Commented Jan 20, 2015 at 18:57
1

Super late answer, but his seems to work well.

\documentclass{article}
\usepackage{amsmath}

% just make sure that cleveref is included last (even after hyperref if used)
\usepackage[capitalize]{cleveref}
\newcommand{\creflastconjunction}{,~and~}

\begin{document}
\begin{align}
  x &= 1 \nonumber \\
  x &= 2 \nonumber \\
  x &= 3.14159 \label{eq:1}\\
  y &= 4 \label{eq:2}
\end{align}

The first two equations are unnumbered (e.g., working out details) and \cref{eq:2,eq:1} are referenced correctly by cleveref.
Note that by default cleveref will sort the list.
Now, if you want to refer to equation 2 before equation 1 you must be explicit with cleveref, like \cref{eq:2} and \labelcref{eq:1}.
This sorting behavior can be changed globally with the option nosort.

\end{document}

Solution_pdf

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .