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?

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}  
  • 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? – Nikki Bisschop Jan 20 '15 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 Jan 20 '15 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.