Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to include a Mathematica code in LaTeX. To obtain the Mathematica code I just exported notebook as PDF. I didn't like the way it was included in my TEX code so I just thought of adding a box around picture so as to make it a bit more fancy. It's not yet there but it's better than nothing. The code used is


\nonumber W_{r\rightarrow\infty}=&-\int_{r}^{\infty}\!F\,\mathrm{d}y=-    \int_r^\infty \!     \dfrac{1}{4\pi \epsilon_0} \dfrac{q^2}{\alpha^2}     \dfrac{\alpha^3}{y^3}\left(1-    \dfrac{\alpha^2}    {y^2}\right)^{-2}\,\mathrm{d}y\\
=&-\dfrac{1}{4\pi \epsilon_0} \dfrac{q^2}{\alpha^2}\alpha^3     \underbrace{\int_r^\infty     \! y^{-3} \left(1-\dfrac{\alpha^2}    {y^2}\right)^{-2} \,\mathrm{d}y}_{I} \label{eq:WcondI}



My output is

Any ideas on how to include Mathematica code in a more aesthetically way?

Edit:At first I used package listing but the problem was the fraction and the fact that I don't know how to include in a convenient way In[1] and Out[1]

share|improve this question
Did you give a try with ctan.org/pkg/listings –  texenthusiast Nov 29 '12 at 7:24
@texlearner: Thank you very much for your comment. I did actually but the problem is that the fraction in the answer is exported as a/b which is something I would like to avoid! –  Thanos Nov 29 '12 at 7:46
You could use mathescape - i'll extend my answer to that. –  Ronny Nov 29 '12 at 7:47

2 Answers 2

You could use listings together with xcolor to include the code, for example with this MWE



  \begin{lstlisting}[language=Mathematica,caption={Example code}]

You would obtain something like Code example image

and by changing the \lstset even adapt the colors to look more Mathematicaish. You could then also use external code files and something like \lstinputlisting{yourfile.m} to include them. This is what i prefer, because then you could just (ok in Mathematica working on one cell) code the stuff you want and change the code. Due to the input it is then automatically the most recent version of your code example.

Edit 1 The OP requested to use Math symbols and fractions in his code:

One way to do that is, to add mathescape to the lstset as a further key. Then at any $ in the code the mode is switched to mathmode and one can type simply math. Then one could change the code line to


to obtain Second Code with math though i think it is not that nice to read (because there's still ^2 in the code and other non-LaTeX-set formulae. Finally my remark above won't work anymore, this code would - of course - not be able to run in Mathematica anymore.

share|improve this answer
Thank you very much for your answer! The first thing I did was to use listings but if you see in my figure the answer contains a fraction which uses a horizontal line to separate numerator from denominator. If I use listings the result is something like a/b, with a slash instead of a horizontal line, that is... Another issue I am facing is the fact that I want to include In[1] and Out[1]. If I use listings those "keywords",which are different from actual keywords, are printed as part of the code... –  Thanos Nov 29 '12 at 7:50
Another issue I am facing is the fact that I want to include In[1] and Out[1]. If I use listings those "keywords",which are different from actual keywords, are printed as part of the code... –  Thanos Nov 29 '12 at 7:55
I'll think about that, but that's a tricky one. To Obtain In[1] instead of 1 (the number now printed as line number) would be possible i think, but alternating them (with Out[...]) is challenging. –  Ronny Nov 29 '12 at 7:56
@You are right about that, so I will edit my question in order to provide the information I forgot to include. –  Thanos Nov 29 '12 at 8:02
@Ronny, is there a way to make the comments in my code look grey in the latex file, instead of black? I want to make them easy to be distinguished from the rest of the code... –  An old man in the sea. yesterday

Using the listings package, it is possible to get math mode in the mathematica code. You need to add the mathescape option on the listings environment definition, and manually place the math delimiters in the listing. I realise that this may be impractical if you want to insert a lot of code.

I hope the following code makes my meaning clear.





\nonumber W_{r\rightarrow\infty}=&-\int_{r}^{\infty}\!F\,\mathrm{d}y=-    \int_r^\infty \!     \dfrac{1}{4\pi \epsilon_0} \dfrac{q^2}{\alpha^2}     \dfrac{\alpha^3}{y^3}\left(1-    \dfrac{\alpha^2}    {y^2}\right)^{-2}\,\mathrm{d}y\\
=&-\dfrac{1}{4\pi \epsilon_0} \dfrac{q^2}{\alpha^2}\alpha^3     \underbrace{\int_r^\infty     \! y^{-3} \left(1-\dfrac{\alpha^2}    {y^2}\right)^{-2} \,\mathrm{d}y}_{I} \label{eq:WcondI}


In[1]:= Integrate[{y^(-3)*(1-(a/y)^2)^(-2)},{y,r,Infinity}]
Out[1]= {ConditionalExpression[$\displaystyle-\frac{1}{2(a^2-r^2)}$, Im[r] 
Re[a] $\neq$ Im[a] Re[r] || (( $a+r>0$ || $a+r \notin $ Reals) && ($a<r$ || 
$a-r \notin$ Reals) || $r\notin$ Reals )]}




Note that I formatted the output manually. Here is the result: result

share|improve this answer
Adding the ` Out[]` and In[] is of course easier, than the idea i just thought about, namely tweaking the numbering of the listing. –  Ronny Nov 29 '12 at 8:04
@Ronny Oops! I didn't see that you already mentioned the use of mathescape, sorry about that. –  guillem Nov 29 '12 at 8:17
Actually i added that, when the OP mentioned that in his first comment below the Introduction, though i was too lazy to type the Output myself in math, i was a little faster with that additional edit than your post ;) –  Ronny Nov 29 '12 at 10:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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