3

I have some big equations that I wanted to separate out of the text of my document to make the source code easier to read. The idea was to declare a command for each equation in a separate file, include the file in the preamble of my document, and then just call the command for that equation in the appropriate place in the text. Here's a single-file example:

\documentclass{article}

\newcommand{\testCommand}{
    \begin{equation} \label{eq1}
        a = b + c
    \end{equation}
}

\begin{document}
    \testCommand
    \cite{eq1}
\end{document}

The problem with this is that I get undefined reference errors when I try to cite the equation. I'm assuming that the compiler must go through and find labels before it expands commands or something. Anyway, I was just wondering if there is a way to make this work. I would also appreciate some insight on what's going on under the hood that causes this to happen.

  • Don't do this; say \newcommand{\myweirdeq}{a=b+c} and use \begin{equation}\label{eq1}\myweirdeq\end{equation}, so your document will be correctly marked up. – egreg Jul 6 '12 at 7:56
3

The way to cite a \label is with \ref; \cite is for bibliographies.

  • Wow, duh. Don't know why my mind randomly got set on \cite. I guess that's what happens when you haven't used LaTeX much for a while. Now I feel sheepish. =) Also, what's the appropriate thing to do with the question in this case? Should it be closed, deleted, or just left as it is? – Brandon Jul 6 '12 at 3:19
  • @Brandon: No upside in deleting it. I am sure you are not the only one who will make this mistake. I have had a few embarrassing questions myself, so don't worry about it. – Peter Grill Jul 6 '12 at 4:33
  • @Brandon: As further evidence that this question should stay: I have done this myself. A lot. – Ryan Reich Jul 6 '12 at 5:54

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.