# Help with a \newcommand not working with autonum

I am writing my thesis. To write math formulas I created the command \eq as follows.

\newcommand{\eq}[1]{
$$\begin{split} #1 \end{split}$$}


However, now I want to use cleveref together with autonum.

I think, however, that this is not possible due to the way I defined the command \eq. Here goes a minimal working example.

\documentclass{minimal}
\usepackage{amsmath}
\usepackage{cleveref}
\usepackage{autonum}

\newcommand{\eq}[1]{
$$\begin{split} #1 \end{split}$$}

\begin{document}

Without my command:\\

This is an equation
$$\label{eq_1} \begin{split} 1+1 &=2,\\ 2+2 &=4. \end{split}$$

This is another
$$\label{eq_2} 1+1=3.$$

Only \cref{eq_1} is correct.\\

\noindent\rule{8cm}{0.4pt}

But with my command:\\

This is an equation
\eq{\label{Eq_1}
1+1 &=2,\\
2+2 &=4.
}

This is another
\eq{\label{Eq_2}
1+1=3.
}

Only \cref{Eq_1} is correct.

\end{document}


The output does not show the number of the equation Eq_1.

So, I think the mistake is that the label of \eq is within \split, and autonum has problems understanding that. If I comment the package autonum everything goes fine but of course all eq's get numbered.

I have a lot of equations, so I do not want to change each of them.

Can any body suggest a way to just change the definition of the command \eq so that autonum works?

• Welcome to TeX.SX! cleveref is a package that should come most times as the last one in a preamble. In addition, I believe you fell in the split trap ;-) – user31729 Jan 8 '15 at 15:24
• Hi!, thank you! I think autonumwould be an exception. In this case if autonumgoes before clevrefI get a bunch of errors. So, do you think this trap is "escapable"?? – PepeToro Jan 8 '15 at 15:34
• I'm thinking why you'd want to always open a split environment in every equation. Shorthands may appear easier to manage, but you're obfuscating your typescript. – egreg Jan 8 '15 at 17:45
• @egreg thanks for your comment. This is only an example. In my particular case, 99.9% of the equations I use are systems of ordinary differential equations. So I do heavily use the mentioned \eq. Of course for the 1 line eq's I do not complicate my life. – PepeToro Jan 8 '15 at 18:20

This is so wrong; you're obfuscating your code for minimal gain.

Anyway, here's the way without changing your input.

\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}
\usepackage{autonum}

\makeatletter
\newcommand{\eq}[1]{%
\begingroup
\findlabel@{#1}%
$$\put@label \begin{split} \pre@label\post@label \end{split}$$%
\endgroup
}
\def\put@label{}
\def\findlabel@#1{\findlabel@i#1\label\@nil}
\def\findlabel@i#1\label#2\@nil{%
\def\pre@label{#1}
\if\relax\detokenize{#2}\relax
\def\post@label{}%
\else
\findlabel@ii#2\@nil
\fi
}
\def\findlabel@ii#1#2\@nil{%
\def\put@label{\label{#1}}\findlabel@iii#2%
}
\def\findlabel@iii#1\label{%
\def\post@label{#1}%
}
\makeatother

\begin{document}

Without my command:

This is an equation
$$\label{eq_1} \begin{split} 1+1 &=2,\\ 2+2 &=4. \end{split}$$

This is another
$$\label{eq_2} 1+1=3.$$

Only \cref{eq_1} is correct.

\noindent\rule{8cm}{0.4pt}

But with my command:

This is an equation
\eq{\label{Eq_1}
1+1 &=2,\\
2+2 &=4.
}

This is another
\eq{\label{Eq_2}
1+1=3.
}

Now \cref{Eq_1} is correct.

\end{document}


The code looks for a \label command, isolating what comes before it and after it, then building the environment and supplying the various bits in the correct position (\label outside split).

• Thanks for this answer, it does solve my original problem. An answer proper of an expert. I'd like to understand what is so wrong. After all, we are all here to learn, and even more from our mistakes. I'll accept this answer is it indeed solves the problem asked. – PepeToro Jan 9 '15 at 12:22

If you define \eq as you did, then the label goes inside the split environment, so your labels are not recognized.

A correct definition could be

\newcommand{\eq}[2]{%
$$\label{#1}% \begin{split}% #2% \end{split}%$$}


to be used in this way (the first argument is the label, the second is the body):

\eq{Eq_1}{
1+1 &=2,\\
2+2 &=4.
}


MWE:

\documentclass{minimal}
\usepackage{amsmath}
\usepackage{cleveref}
\usepackage{autonum}

\newcommand{\eq}[2]{%
$$\label{#1}% \begin{split}% #2% \end{split}%$$}

\begin{document}

Without my command:\bigskip

This is an equation
$$\label{eq_1} \begin{split} 1+1 &=2,\\ 2+2 &=4. \end{split}$$

This is another
$$\label{eq_2} 1+1=3.$$

Only \cref{eq_1} is correct.\bigskip

\noindent\rule{8cm}{0.4pt}\bigskip

But with my command:\bigskip

This is an equation
\eq{Eq_1}{
1+1 &=2,\\
2+2 &=4.
}

This is another
\eq{Eq_2}{
1+1=3.
}

Only \cref{Eq_1} is correct.

\end{document}


Output:

A remark: never use \\ to terminate a line... See When to use \par and when \\, or blank lines for reference.

• thank you. I did think on something like what you write. I understand the problem with the label within split. Naturally, as you show, it works. However, this solution implies that I go to every labeled equation and change it. I would like to avoid that... well, if there is no other way, I guess I'll have a tedious weekend! Let me wait a bit to see if there are other proposals. – PepeToro Jan 8 '15 at 18:24
• @PepeToro Your definition of \eq is wrong anyway. About spending your time changing your code, every good editor with RegEx capabilities is able to do those substitutions in less than a minute. – karlkoeller Jan 8 '15 at 19:44
• Thank you for letting me know of the existence of the RegEx concept. I've learned more. I do understand now that the definition I made is not as it should be. I'll accept egreg's answer as it shows a code that solves my problem (hope you don't mind) but I'll definitely try your recommendation. – PepeToro Jan 9 '15 at 12:25