10
$\begingroup$

When I save the following notebook code as $\LaTeX$,

In[1]:= DSolve[y'[x] + x y'[x]^2 == 1, y, x]

Out[1]= {{y -> 
   Function[{x}, 
    C[1] + 1/2 (-2 Sqrt[1 + 4 x] - 2 Log[1 - Sqrt[1 + 4 x]])]}, {y -> 
   Function[{x}, 
    C[1] + 1/2 (2 Sqrt[1 + 4 x] - 2 Log[1 + Sqrt[1 + 4 x]])]}}

I get this $\LaTeX$ code:

%% 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{doublespace}
    \noindent\(\pmb{\text{DSolve}[y'[x]+x y'[x]{}^{\wedge}2==1,y,x]}\)
    \end{doublespace}
    \begin{doublespace}
    \noindent\(\left\{\left\{y\to \text{Function}\left[\{x\},C[1]+\frac{1}{2} \left(-2              
    \sqrt{1+4 x}-2 \text{Log}\left[1-\sqrt{1+4 x}\right]\right)\right]\right\},\left\{y\to
    \text{Function}\left[\{x\},C[1]+\frac{1}{2} \left(2 \sqrt{1+4 x}-2
    \text{Log}\left[1+\sqrt{1+4 x}\right]\right)\right]\right\}\right\}\)
    \end{doublespace}
    \end{document}

I would like to have $$ instead of

\begin{doublespace}
\noindent

i.e., as an ordinary $\TeX$ file.

$\endgroup$
3
  • $\begingroup$ Cross posted here and here $\endgroup$
    – Szabolcs
    Mar 20, 2012 at 6:30
  • 1
    $\begingroup$ Is it such a big deal to take the LaTeX code saved as shown and then just do a global search-and-replace in a text editor (or, if you prefer, define a Mathematica function to do the same thing upon the saved file? $\endgroup$
    – murray
    Mar 20, 2012 at 18:49
  • $\begingroup$ Your link, unfortunately, appears to have died :-( $\endgroup$ Feb 18, 2015 at 11:40

2 Answers 2

8
$\begingroup$

The way you do this - by saving the whole notebook with un-formatted In/Out cells - will get you a file with non-traditional working Mathematica notation. For example you will get square brackets for functions instead of round ones and capitalized functions names. If your goal is just to get a nicely formatted formula in $\TeX$ form, you could use this (for your 1st equation):

In[1]:= f = First[y /. DSolve[y'[x] + x y'[x]^2 == 1, y, x]];
        TeXForm[f[x]]

Out[1]//TeXForm=
c_1+\frac{1}{2} \left(-2 \sqrt{4 x+1}-2 \log \left(1-\sqrt{4 x+1}\right)\right)

If you paste the output in the body of your $\TeX$ file between $$ you'll get a nice formula:

enter image description here

If you would like to save the whole document you need to apply some formatting to it, depending on what you exactly need. For example turning an output cell into text cell will result in wrapping the formulas in \( ... \) , which is an equivalent of $...$ in $\LaTeX$.

$\endgroup$
4
$\begingroup$

What I find convenient: in Mathematica

probi = Exp[Sum[-τ((Subscript[x,i,k] - Subscript[x,j,k])^2)/2,
        {i,j}∈Subscript[Edge,k]]] HoldForm[Sqrt[τ/(2Pi)]]^Subscript[N,i]

Export["formula1.tex", (HoldForm[logP[X,τ]] == Log[probi]//PowerExpand)//TeXForm, "text"]

and in $\LaTeX$:

$$ \input{formula1.tex} $$

This results with the formula: $$\text{logP}(X,\tau )=\sum _{\{i,j\}\in \text{Edge}_k} \frac{1}{2} (-\tau ) \left(x_{i,k}-x_{j,k}\right){}^2+N_i \log \left(\sqrt{\frac{\tau }{2 \pi }}\right)$$

If the formula is large use breqn.

\begin{dmath}
\scriptstyle
\input{formula1.tex}
\end{dmath}`
$\endgroup$
1
  • 1
    $\begingroup$ Probably best to include in LaTeX preamble \DeclareMathOperator{\logP}{logP} and \DeclareMathOperator{\Edge}{Edge}, then change \text{logP} to \logP and change \text{Edge} to `\Edge'. $\endgroup$
    – murray
    May 26, 2015 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.