# 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:

Output 2 http://reference.wolfram.com/mathematica/ref/format/TeX.html#1511511812

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}{{}}
\newcommand{\unicode}{{}}

\newcounter{mathematicapage}
\begin{document}

\begin{equation}
g(x)=x^3
\end{equation}

\begin{equation}
f(x)=x^2
\end{equation}

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}{{}}
\newcommand{\unicode}{{}}

\newcounter{mathematicapage}
\begin{document}

\begin{equation}
g(x)=x^3
\end{equation}

\begin{equation} \label{simpleParabola}
f(x)=x^2
\end{equation}

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}{{}}
\newcommand{\unicode}{{}}

\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{" <> #[] <> "}", #] &
}


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" -> {
"\\begin{equation}\n", ConvertTeXBoxesToTeX[#] &,
"\n\\end{equation}"
}
} ]


We get:

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

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

\newcommand{\mathsym}{{}}
\newcommand{\unicode}{{}}

\newcounter{mathematicapage}
\begin{document}

\begin{equation}
f(x) = x
\end{equation}

\end{document}


We need to get

\begin{equation}\label{eq:myEq}
f(x) = x
\end{equation}


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. Jul 29, 2011 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, 2011 at 14:21
• Here's how I reference eqns in (la)tex: Some text that needs to refer to Equation ~\ref{eqn:kh} \begin{equation} <some equation>  \label{eqn:kh} \end{equation} This board doesn't seem to map the reference. But it finds it just find on MacTex. Aug 19, 2013 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. Aug 19, 2013 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? Dec 30, 2013 at 7:17

Here is a way that I have found to insert the value(s) of CellTag into the TeX output. It assumes a singular value for CellTag, but I'm sure the approach can be generalized to a case where a cell has more than one. (Although, if the CellTag is specifically being used to label references, I'm not sure why there would be more than one value!)

With[{cells = {
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"],
Cell[BoxData[FormBox[RowBox[{RowBox[
{"f", "(", "x", ")"}], " ", "=", " ", "x"}], TraditionalForm]],
"NumberedEquation", CellTags -> "eq:myEq1"]
}},
ExportString[cells, "TeX",
"ConversionRules" -> {
"Text" -> {If[MatchQ[#, _CounterBox], "~\\ref{" <> #[] <> "}", #] &},
"NumberedEquation" -> {Evaluate["\\begin{equation}\\label{" <>
ToString[CellTags /. Options[cells[]]] <> "}\n"],
ConvertTeXBoxesToTeX[#] &, "\n\\end{equation}"}
}]
]


The TeX output I got with this code, which hopefully puts everything together for the OP, shows both \ref{} and \label{} commands.

%% AMS-LaTeX Created with the Wolfram Language : www.wolfram.com

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

\newcommand{\mathsym}{{}}
\newcommand{\unicode}{{}}

\newcounter{mathematicapage}
\begin{document}

This is a reference to an equation: ~\ref{eq:myEq1}. This is another \
one: ~\ref{eq:myEq2}. And this is yet another one: (~\ref{eq:myEq3}).

\begin{equation}\label{eq:myEq1}
f(x) = x
\end{equation}

\end{document}