4

Suppose I am writing a LaTeX document describing some code and in the code I would like to refer to specific equation numbers in the document. Is there an automatic way to determine what equation number will be assigned to \label{eqn:myequation}

It would be nice to write a program which parses the source code and replaces an equation label in the comments with the current number assigned to that label by pdflatex.

6
  • Welcome to TeX.SX! If you give \label{foo} to an equation, then \ref{foo} will print the number assigned to it. – egreg Jul 18 '14 at 22:08
  • 1
    Welcome to TeX.SX! Why can't you just use \ref{eqn:myequation}? Sorry but I'm not following this. Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – cfr Jul 18 '14 at 22:08
  • 1
    The key point in the \label-\ref mechanism is that you never need to know the actual number, but just the label. The actual number may change with the text, the labels won't. – egreg Jul 18 '14 at 22:11
  • 1
    @egreg I think the OP wants to generate a comment in (say) a C file of the form /* this implements equation 5 */ so basically either get TeX to write the source file or perl etc to insert the number from the aux file. – David Carlisle Jul 18 '14 at 22:27
  • 2
    An idea: the .aux file knows the numbers attached to labels. If you use the labels in your code you can write a perl (or sed or awk) script to replace the label in the code. Better put that in a makefile and recompile the source to make sure nothing's broken. – Ethan Bolker Jul 19 '14 at 0:32
3

This may or may not be desirable for your uses, because it doesn't physically alter your code files. (It leaves the \labels in the code rather than replacing them with the numbers.) But several ways to do this exist using the listings package's escape to LaTeX features. Other code pretty-printing packages have similar features, but I show an example with listings because it is the most ubiquitous.

You can use Eq.~\ref{eqn:foo} directly in the comments, and tell the listings package to escape to LaTeX code between a set of arbitrary delimiters (as shown in the first example, with |), or to treat all comment lines as escaped LaTeX code, as in the second example.

Here is the general idea:

\documentclass{article}
\usepackage{listings}
\lstset{language=c}

\begin{document}
My code implements
\begin{equation}\label{eq:energy}
e = mc^2,
\end{equation}
which describes the relationship between mass and energy.
The code is displayed in Listing~\ref{lst:func}.
\begin{lstlisting}[
  caption={Test function.},
  label={lst:func},
  escapechar=|,% <-- for escaping to LaTeX inside comments
]
double NucEnergy(double mass){
    /* here I have |\itshape Eq.~\ref{eq:energy}| of the text */
    < ... code omitted ... >
}
\end{lstlisting}

Another option, where all comment lines are parsed as \LaTeX\ input:
\begin{lstlisting}[
  caption={Another test function.},
  label={lst:func2},
  texcl=true,%<-- to treat all comment lines as escaped LaTeX
]
double NucEnergy(double mass){
    // here I have Eq.~\ref{eq:energy} of the text
    < ... code omitted ... >
}
\end{lstlisting}
\end{document}

enter image description here

Notice how, in the first example, we are completely in charge of the text style (note \itshape), while in the second example, the default comment style is automatically used.

0

Thanks for the interesting example, Paul.

It looks like the suggestion by Ethan regarding the .aux file is precisely what I wanted. I admit that I have been using LaTeX for about 13 years, but have never actually looked at the .aux file before.

In my current document, I have an equation

\begin{equation}
\min_{\psi,\mathcal{E}} -|\langle \psi(T)|f\rangle|^2 +\frac{\gamma}
{2}\|\mathcal{E}\|^2
\label{eqn:state_equation}
\end{equation}

and the .aux file contains a line

\newlabel{eqn:state_equation}{{1}{1}{}{equation.0.1}{}}

I can see that in the .pdf this is indeed equation 1, so it looks like the contents of the first set of curly braces is the assigned number and from another example, it appears that the page number is in the second {} and the name of the containing subsection appears in the third {}.

Now I can write a text parser that will read my source code and create a distribution version where eqn:state_equation is replaced with (1).

Many thanks for the suggestions!

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.