Numbered equations/formulas

How do you number equations in Mathematica notebooks so that they are exported correctly into latex? In case you don't know how to put numbered equations here is quick review.

1. Open a new notebook and create a new cell with an equation.

2. Select the cell, on the menu go to Format -> Style -> DisplayFormulaNumbered or change this by pressing the shorcut command+0 on mac or alt+0 on windows and enter DisplayFormulaNumbered. At this step you should have something like this:

3. Select the cell again in case you deselected like I did. Go to Cell -> Cell Tags -> Add/Remove Cell Tags or use the shorcut command+J on the mac (don't know about windows). A panel should appear, enter the name of how you would like to refer to the equation and click Add. I entered simpleParabola.

4. To see that you successfully added the tag go to Cell -> Cell Tags -> Show Cell Tags. You can use this options to show and hide the cell tags. I suggest to keep them on while you are editing and hiding them when you are done. This is what I see so far.

You can repeat these steps to enter your equations and labeling them. Now, to refer to them by the tag you have given them you have to write some text. Say you write something like:

This is my text and now I refer to XXX because it is important to refer to it.


What we want is to let XXX to refer to the label simpleParabola. What you do is select that part of the text. Then go to Insert -> Automatic Numbering .... On Counter you need to select DisplayFormulaNumbered because that's the type of cell you want to refer to. Then select simpleParabola, click ok and you are done. The XXX should change to 1. The good thing about this is that if in the future your numbers change then your number will automatically change. To test this, just go above the cell where you have numbered formula and create a new DisplayFormulaNumbered cell, Now this new one should have the label 1 and the cell with the tag simpleParabola should have label number 2. But guess what? You don't have to worry about changing the numbers in your text because you already linked it to the label. Here is a screenshot:

Ok, so we can create documents with numbered equations. Now lets assume that you are collaborating in some document and this document is required to be typed in latex. As the awesome Mathematica user that you are you type the document faster in Mathematica thanks to all the shortcuts that it offers. Finally, after you are done you save it in Latex format. Here is a what we obtain:

%% AMS-LaTeX Created by Wolfram Mathematica 8.0 : www.wolfram.com

\documentclass{article}
\usepackage{amsmath, amssymb, graphics, setspace}

\newcommand{\mathsym}[1]{{}}
\newcommand{\unicode}[1]{{}}

\newcounter{mathematicapage}
\begin{document}

$$g(x)=x^3$$

$$f(x)=x^2$$

This is my text and now I refer to 2 because it is important to refer to it.

\end{document}


Not bad. Unfortunately, our cell tags are gone. Now, laTeX users, please correct me if I'm wrong. Will the next modification work?

  %% AMS-LaTeX Created by Wolfram Mathematica 8.0 : www.wolfram.com

\documentclass{article}
\usepackage{amsmath, amssymb, graphics, setspace}

\newcommand{\mathsym}[1]{{}}
\newcommand{\unicode}[1]{{}}

\newcounter{mathematicapage}
\begin{document}

$$g(x)=x^3$$

$$\label{simpleParabola} f(x)=x^2$$

This is my text and now I refer to ~\ref{simpleParabola} because it is important to refer to it.

\end{document}


All I've done is add \label{simpleParabola} and ~\ref{simpleParabola}. After running LaTeX on it twice I obtain this pdf

Mathematica did a pretty good job at giving the latex version but it gave me the job to add the labels and references.

Now here is something that is very interesting. If you have the modifed version of the latex document that I showed I want you to open a new notebook and enter:

NotebookPut[Import["Path_To_Latex_Document"]]


You will end up with a new document with the latex content in there. But what happened to the numbers? and why is the reference messed up? If you show the cell tags again it will reveal that the label that you put in latex was converted to a cell tag. And if you see the expression on the text you will see that it added the automatic numbering for you. The reason it isn't working is because it placed the wrong counter.

So now that you have an idea of what I am doing here are the questions:

• Is there any way to write in latex the style that the Mathematica cells should use? Notice how Mathematica uses NumberedEquation instead of the DisplayFormulaNumbered style.
• How do you specify the counter so that Mathematica knows what counter to use. Notice that in this case it is using Section instead of DisplayFormulaNumbered.
• How do you make it so that when you export the notebook file to a latex file it places the \labels and \~ref in the latex file?

Any ideas?

I will post some new stuff I've found here. I still haven't found an answer. If you go to this link you will find an example. This is what I've been working with

ExportString[
Cell[
BoxData[{
"This is a reference to an equation: ",
CounterBox["NumberedEquation", "eq:myEq1"],
". This is another one: ",
CounterBox["NumberedEquation", "eq:myEq2"],
". And this is yet another one: (",
CounterBox["NumberedEquation", "eq:myEq3"],
")."
}
], "Text"
]
, "TeX",
"ConversionRules" -> {
"Text" -> {
# &
}
} ]


The output I obtain is:

%% AMS-LaTeX Created by Wolfram Mathematica 8.0 : www.wolfram.com

\documentclass{article}
\usepackage{amsmath, amssymb, graphics, setspace}

\newcommand{\mathsym}[1]{{}}
\newcommand{\unicode}[1]{{}}

\newcounter{mathematicapage}
\begin{document}

This is a reference to an equation: CounterBox[NumberedEquation, eq:myEq1]. This is another one: CounterBox[NumberedEquation, eq:myEq2]. And this
is yet another one: (CounterBox[NumberedEquation, eq:myEq3]).

\end{document}


I can tell Mathematica to let the the contents go as they are. One problem though, I want to replace say CounterBox[NumberedEquation, eq:myEq1] with ~\ref{eq:myEq1}.

How do you use the replacement rules here?

EDIT:

The ConversionRule for text should be

"Text" -> {
If[MatchQ[#, _CounterBox], "~\\ref{" <> #[[2]] <> "}", #] &
}


Here is the remaining problem I can't figure out:

ExportString[
Cell[
BoxData[
FormBox[RowBox[{RowBox[{"f", "(", "x", ")"}], " ", "=", " ", "x"}],
],
"NumberedEquation",
CellTags -> "eq:myEq"
]
, "TeX",
"ConversionRules" -> {
"NumberedEquation" -> {
"\$$\n", ConvertTeXBoxesToTeX[#] &, "\n\$$"
}
} ]


We get:

%% AMS-LaTeX Created by Wolfram Mathematica 8.0 : www.wolfram.com

\documentclass{article}
\usepackage{amsmath, amssymb, graphics, setspace}

\newcommand{\mathsym}[1]{{}}
\newcommand{\unicode}[1]{{}}

\newcounter{mathematicapage}
\begin{document}

$$f(x) = x$$

\end{document}


We need to get

$$\label{eq:myEq} f(x) = x$$


How do we obtain the CellTag?

-
I don't know why I didn't stumble on TeX. Even so, I still have no idea how to add the label and references to the latex file. –  jmlopez Jul 29 '11 at 9:06
This is not an answer, but just a latex comment: In general, it is advisable to write \label{} after whatever it is that is being labeled (to be safe, before the end). This might not matter here, but you'll see the difference in a figure environment. –  Lorem Ipsum Jul 29 '11 at 14:21
Here's how I reference eqns in (la)tex: Some text that needs to refer to Equation ~\ref{eqn:kh}  <some equation>  \label{eqn:kh} This board doesn't seem to map the reference. But it finds it just find on MacTex. –  earnric Aug 19 '13 at 21:45
hi @earnric, welcome to Mathematica.SE! I have converted your "answer" to a comment because it didn't really address the question. –  Verbeia Aug 19 '13 at 23:24
@jmlopez, it seems that inside "ConversionRules", the expression handed over to the conversion rule is a subexpression from within the given Cell; there is no access to the whole Cell object. So, it seems the only way forward is to craft a custom conversion rule for each Cell based on the tags it contains. This can be done with Notebook manipulation, I guess. Sounds reasonable to you? –  caya Dec 30 '13 at 7:17
show 1 more comment

migrated from stackoverflow.comJul 28 '13 at 6:31

This question came from our site for professional and enthusiast programmers.