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.
Open a new notebook and create a new cell with an equation.
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 enterDisplayFormulaNumbered
. At this step you should have something like this:Output 2 http://reference.wolfram.com/mathematica/ref/format/TeX.html#1511511812
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 enteredsimpleParabola
.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}
\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}[1]{{}}
\newcommand{\unicode}[1]{{}}
\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 theDisplayFormulaNumbered
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 ofDisplayFormulaNumbered
. - 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"}],
TraditionalForm]
],
"NumberedEquation",
CellTags -> "eq:myEq"
]
, "TeX",
"ConversionRules" -> {
"NumberedEquation" -> {
"\\begin{equation}\n", Convert`TeX`BoxesToTeX[#] &,
"\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}[1]{{}}
\newcommand{\unicode}[1]{{}}
\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
?
\label{}
after whatever it is that is being labeled (to be safe, before theend
). This might not matter here, but you'll see the difference in afigure
environment. $\endgroup$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. $\endgroup$