How do I evaluate Mathematica expressions and insert them in a LaTeX file?

15

5

I want to make use of Mathematica as the backend to solve some computations used in my LaTeX input file. As I have no knowledge to make it in Mathematica, I used Maxima as explained in the following steps that I got somewhere on the internet.

Step 1

Download and install Maxima.

Step 2

Create a batch file named cas.bat (for example) as follows.

rem cas.bat
echo off
set PATH=%PATH%;"C:\Program Files (x86)\Maxima-5.31.2\bin"
maxima --very-quiet -r %1 > solution.tex

Save the batch in the same directory in which your input file below exists. It is just for the sake of simplicity.

Step 3

Create the input file named main.tex (for example) as follows.

% main.tex
\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{amsmath}

\def\f(#1){(#1)^2-5*(#1)+6}


\begin{document} 

\section{Problem}
Evaluate $\f(x)$ for $x=\frac 1 2$.

\section{Solution}
\immediate\write18{cas "x: 1/2;tex(\f(x));"}

\input{solution}

\end{document}

Step 4

Compile the input file with pdflatex -shell-escape main and you will get a nice output as follows.

enter image description here

Questions

Apparently the output of Maxima is as follows. I don't know how to make it cleaner.

solution.tex

                                       1
                                       -
                                       2
$${{15}\over{4}}$$
                                     false

Now, my questions are

  • how to implement the scenario above in Mathematica?
  • if Mathematica also produces the unnecessary texts, how to remove such texts?
  • if Mathematica also produces the same output format, how to obtain just \frac{15}{4} without $$...$$?

kiss my armpit

Posted 2013-12-02T01:15:07.773

Reputation: 747

Answers

32

This is a good application for the highly underused Splice function which has been in Mathematica since version 1 (I don't recall it ever being used on this site).

  1. Create a file called main.mtex with the following text:

    \documentclass[preview,border=12pt,12pt]{standalone}
    \usepackage{amsmath}
    \def\f(#1){(#1)^2-5*(#1)+6}
    \begin{document}
    
    \section{Problem}
    Evaluate $\f(x)$ for $x=\frac 1 2$.
    
    \section{Solution}
    $ <* f[x_] := x^2 - 5 x + 6; f[1/2] *> $
    \end{document}
    

    Everything you want Mathematica to evaluate should be written between <* *>.

  2. In Mathematica, run Splice["main.mtex"] (replace with the correct path to your file). The Splice function reads the file and replaces the text between <* *> with its Mathematica output. The default output for input files with the extension .mtex is TeXForm, which mostly does a good job of translating to LaTeX for non-complicated expressions.

  3. Compile the main.tex file that is created as an output of Splice.

    enter image description here


Rather than using a TeX macro to expand the equation, I would suggest using Mathematica here as well. Here's a modified example:

\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{amsmath}
% <* f[x_] := x^2 - 5 x +6 *>
\begin{document}
\section{Problem}
Evaluate $ <* f[x] *> $ for $x=\frac 1 2$.

\section{Solution}
$$ <* f[1/2] *> $$

\end{document}

The definition f[x_] will be available to Mathematica throughout the splice operation and we can use this to make the text replacement as well. I think it would be a much simpler solution than hacking something together with TeX/interpreting the macro in Mathematica. Note that I commented out the f[x_] definition in TeX, because Mathematica outputs Null for that operation, which gets inserted as \text{Null} and we don't want that. The result is the same (perhaps better than using the TeX macro):

enter image description here

rm -rf

Posted 2013-12-02T01:15:07.773

Reputation: 85 395

Perhaps not here, but http://stackoverflow.com/a/1688457/353410

– Dr. belisarius – 2013-12-02T02:06:07.970

@DonutE.Knot You can create a MathematicaScript. I don't have the time to do this now, but you can look at questions and answers here using this. How to use Mathematica functions in Python programs? and Reading from STDIN, or: how to pipe data into Mathematica might be of use.

– rm -rf – 2013-12-02T02:45:58.110

How to show * for multiplication in x^2 -5 x+6 produced by Splice? Some plotting macros (PSTricks) need the *. – kiss my armpit – 2013-12-02T03:20:45.430

1Underused indeed. I do not recall ever having used it, but it is a very good match for this question. +1! – Sjoerd C. de Vries – 2013-12-02T10:34:51.753

+1 for a very useful post. As of Version 10 Splice has been superseded by FileTemplate as the documentation tells me.

– gwr – 2015-09-25T15:16:05.570

Could you explain how to use FileTemplate in spite of Splice that has been superseded? – Money Oriented Programmer – 2016-08-06T16:23:36.393