Here is some information on how to convert formulas from Mathematica to LaTeX and vice versa (see also this post).
For my writing I always use LyX
, a LaTeX editor and front end that can format equations while you type them.
Some people prefer to do all their writing in Mathematica
. However, for larger documents, I find that impractical. because important editor functions (such citation management, and publication-ready PDF output) are missing. This is the reason why I do the bulk of my writing in LyX
– it is not only a LaTeX
frontend, but it moreover leaves you immense freedom to customize and export the document.
In going back and forth between Mathematica and LyX, it's convenient that both of them understand LaTeX
. Although LaTeX
code is semantically ambiguous, it can serve as a bridge connecting these applications.
If you're not interested in using LaTeX
, look instead at converting Mathematica formulas to PDF.
This is a selective approach that works well when you aren't interested in moving text between Mathematica and LaTeX, only equations:
The Mathematica function TeXForm
produces a LaTeX representation of a syntactically correct
expression, often trying to apply conventional typesetting rules. The result looks similar but not
identical to TraditionalForm
output.
TeXForm[HoldForm[
Integrate[f[\[Theta]], {\[Theta], -\[Pi], \[Pi]}]]]
Here, I use HoldForm
(alternatively: Unevaluated
) to get the
LaTeX
code for the input expression, \int_{-\pi }^{\pi } f(\theta ) \,
d\theta
, not the evaluated result of the integral.
Note that this is different from what you get by clicking the cell and selecting
Copy as ... LaTeX
: that will give you the literal cell text in the
form \text{Integrate}[f[\theta ],\{\theta ,-\pi ,\pi \}]
, which is
a much less useful LaTeX
expression for presentation purposes.
The "less useful" copy-as-LaTeX method is actually quite useful when you're
not dealing with valid code expressions, such as this text cell containing
a typical math formula in box notation:
As input to TexForm, this would be nonsense because there's no intepretation for the ∇2
symbol, and the equal sign acts as an assigmnent. But
Copy as ... LaTeX
works perfectly in this situation.
The TeXForm
approach is the one you would usually choose in order to transfer the output of a
Mathematica evaluation to LaTeX. On the other hand, the menu approach allows you to get the LaTeX form of
inline or displayed equations in a Mathematica Notebook.
Because notational tastes vary, it is likely that not every valid expression will be translated into the TeX form you expect.
For derivatives, there are several different display formats, and you can
influence which one Mathematica chooses by specifying the format of the output.
The situation is discussed in this post on the Wolfram Blog. On that page, I am
also giving an example expression that contains several different types of derivatives:
d = D[f[g[x]] + h[x, y], {x, 2}]
The derivative notation that you get from both TeXForm[d]
and
TraditionalForm[d]
doesn't make use of the standard (fraction-like) notation for
derivatives. Motivated by the comments on the blog post linked above, my suggestion is to modify
the TraditionalForm
handling of derivatives as follows:
Derivative /: MakeBoxes[Derivative[\[Alpha]__][f1_][vars__Symbol], TraditionalForm] := Module[{bb, dd, sp}, MakeBoxes[dd, _] ^= If[Length[{\[Alpha]}] == 1, "\[DifferentialD]", "\[PartialD]"]; MakeBoxes[sp, _] ^= "\[ThinSpace]"; bb /: MakeBoxes[bb[x__], _] := RowBox[Map[ToBoxes[#] &, {x}]]; FractionBox[ToBoxes[bb[dd^Plus[\[Alpha]], f1]], ToBoxes[Apply[bb, Riffle[Map[bb[dd, #] &, Select[({vars}^{\[Alpha]}), (# =!= 1 &)]], sp]]]] ]
I copied the code from my post so that you can copy it into a Notebook directly from here
without having to correct the quotation marks that came out wrong on the blog
(Wolfram should really get to work on a proper HTML
-ization utility for their
blog posts and online help — it doesn't reflect well on MMA if their
web pages rely on bitmap images to display notebook content).
With this, the output of TeXForm[d]
and
TraditionalForm[d]
looks much nicer (I'm only showing the TraditionalForm
output – the LaTeX
output is very similar):
Now you have partial derivative symbols where two or more variables are involved, total derivative symbols where only one variable appears, and primes where the differentiation is with respect to a function slot that isn't one of the differentiation variables.
In the above definition, the second argument f1
to the Derivative
function is always the name of a function. If you expect to
encounter the non-standard case where f1
is something more complex (like a sum
of symbols), it would be better to not use my abbreviated notation. You can rule such cases out
by replacing [f1_]
with [f1_AtomQ]
in the Derivative
pattern.
The first thing to realize is that LaTeX is great for presenting conventional math notation, but it often leaves room for interpretation, so that the structure of an expression cannot always be guessed correctly by Mathematica. For example, a derivative is often written in a way that looks like a fraction, and one needs some comventions to decide which is which.
ToExpression["\\int_{-\\pi }^{\\pi } f(\\theta) \\, d\\theta", TeXForm, Hold]
Note that all backslashes are doubled because a single \
counts as an escape character. In the above code, the argument Hold
prevents evaluation so you can copy the expression from the output
cell if desired (you can later apply ReleaseHold
to evaluate the expression). This conversion will throw an error if Mathematica thinks the expression has incorrect syntax. So you won't always be able to get literal translations of arbitrary LaTeX
fragments.
z = x y
. The Mathematica expression will become z = xy which incorrectly references a single variable with a two-letter name xy
. You have to explicitly insert thin spaces ("\\,"
) in the string argument of ToExpression
wherever you want an implicit multiplication to appear. Note that LaTeX
ignores spaces too, but it defaults to defining each letter as a separate variable in the above expression. Mathematica simply uses the wrong default assumption here.
3.1 103
are also ignored, so this example would be translated as (3.11)3
. As above, you have to force Mathematica to recognize spaces, or use a multiplication operator such as \times
in your LaTeX
code.
LaTeX
code to be preceded by \\, d
. Only with the additional space will it
recognize the d
as the differential part of the integral. \partial
symbol:ToExpression["\\frac{\\partial^2 f(x)}{\\partial x^2}", TeXForm, Hold]
\{x,y,f (z)\}
array
environments or commands such as \pmatrix
to Mathematica produces wrong formatting.
\left(
and
\right)
, not square [...]
or curly {...}
brackets.
The latter are interpreted differently by Mathematica.
LaTeX
, create a snippet of the MMA code that you think should be generated. Put this into TeXForm
. From the result, you can often guess the required LaTeX
form that will work in reverse as an input to ToExpression
.
This approach is sometimes necessary, but I would suggest trying the previous approach first, because the exported equations are almost certainly not going to be coded as real LaTeX
equation environments.
To bring a whole Mathematica Notebook into LaTeX
format, you can do this via the File > Export
menu. However, there are some restrictions that one should keep in mind to avoid surprises:
\pmb{ }
(that's poor man's bold, and it deserves the name)
(* ... *)
because inter-word spaces are omitted upon export.
LaTeX
output.
Finally, even if you decide to export the whole document to LaTeX
, it will generally be necessary to go through the source with a text editor to clean up formatting problems.