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

\documentclass[a4paper,11pt]{article}
\usepackage{kerkis}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage[pdftex]{graphicx}
\usepackage{xcolor}

\begin{document}
\begin{align}
\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}
\end{align}

\setlength{\unitlength}{1cm}
\begin{picture}(15,5)
\color{blue}
\put(-1,0){\line(0,1){5}}
\put(0,1.5){\includegraphics[scale=0.8]{math}}
\put(-1,0){\line(1,0){15}}
\put(14,0){\line(0,1){5}}
\put(-1,5){\line(1,0){15}}
\end{picture}

\end{document}

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
    
@Jubobs Where are thou? –  percusse Feb 5 at 0:11

3 Answers 3

I know this question is old and surely OP doesn't need it anymore, but recently I had similar problem and I think my solution answers the question.

First thing to note is that in Mathematica FrontEnd cells can have arbitrary styles. Each styles appearance is customizable by a stylesheet. With default stylesheet even most basic cell styles i.e. Input and Output look different.

In cells with some styles (e.g Input or Code) code syntax is colored, this can be achieved using already mentioned in other answers listings package.

Cells with some styles (e.g. Input, Output or Print) by default use, so called StandardForm, which allows embedding of complicated formatting (fractions, superscripts etc.) inside code. This was partially solved in other answers by using mathescape functionality of listings package. Problem with this solution is that mathescape "completely escapes" to TeX. Since one can't nest listings environments/commands, I don't see a way to treat parts of escaped content again as code (e.g. typeset it verbatim) using listings only.

To achieve such functionality one can use Verbatim environment from fancyvrb package. By setting proper commandchars we can embed for example \frac command inside code in such way that frac's arguments are typeset verbatim. Downside of fancyvrb is that it doesn't offer automatic syntax coloring. Fortunately listings has special interface to fancyvrb that allows combining of reading code by fancyvrb and typesetting it by listings.

Below I present usage of my 'mmacells' package which implements solution based on fancyvrb + listings approach, with some additional features like customizable cell styles, automatic cell labels etc.

Print screen of result: Mathematica cells in pdf

TeX code:

\documentclass{article}

\usepackage[margin=2cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{mmacells}

\usepackage[export]{adjustbox}


\mmaDefineMathReplacement[≤]{<=}{\leq}
\mmaDefineMathReplacement[≥]{>=}{\geq}
\mmaDefineMathReplacement[≠]{!=}{\neq}
\mmaDefineMathReplacement[→]{->}{\to}[2]
\mmaDefineMathReplacement[⧴]{:>}{:\hspace{-.2em}\to}[2]
\mmaDefineMathReplacement{∉}{\notin}
\mmaDefineMathReplacement{∞}{\infty}
\mmaDefineMathReplacement{𝕕}{\mathbbm{d}}


\mmaSet{
  fv*={gobble=2},
  linklocaluri=mma/symbol/definition:#1,
  graphics={valign=t}
}


\begin{document}

Input from question with output from my Mathematica version.
Input is in input form, it can be copied and pasted to Mathematica.
\begin{mmaCell}[dynamic=y]{Code}
  Integrate[{y^(-3)*(1-(a/y)^2)^(-2)},{y,r,Infinity}]
\end{mmaCell}
\begin{mmaCell}{Output}
  \{ConditionalExpression[-\mmaFrac{1}{2 (\mmaSup{a}{2} - \mmaSup{r}{2})},
     Im[r] Re[a] ≠ Im[a] Re[r] || ((a + r > 0 || a + r ∉ Reals) &&
       ((Re[a] < r && Im[a] == 0) || a - r ∉ Reals)) || r ∉ Reals]\}
\end{mmaCell}

For comparison, same cells obtained by including PDFs exported from
Mathematica:
% You need to export mathematica cells to inCell.pdf and outCell.pdf files for this to work.
% \mmaCellGraphics{Input}{inCell.pdf}
% \mmaCellGraphics[graphics*={raise=.7em}]{Output}{outCell.pdf}

Same input expression, but in standard form (as if it was inputted using math
assistant). Note that syntax coloring still works.
\begin{mmaCell}[index=3,dynamic=y]{Input}
  \mmaSubSup{\int}{r}{∞}\{\mmaFrac{1}{\mmaSup{y}{3}\mmaSup{\big(1-\mmaSup{\big(\mmaFrac{a}{y}\big)}{2}\big)}{2}}\}𝕕y
\end{mmaCell}


\newpage
More features:
\begin{mmaCell}[
  defined*=f,dynamic=a,lexical=b,argument={x_,x},error=n,linkbuiltin=List
]{Code}
  (* A (* nested *) comment. *)
  Block[{a=3},a+2]
  f[x_]:=2x+1 
  Module[{b=c},
      Print["a string \" with double quotes inside ", b/d];
      b+1
  ]
  f[z]//FullForm
  Sin[m,n]
  List[1,List[2,3]]; (* Links to documentation. *)
\end{mmaCell}
\begin{mmaCell}{Output}
  5
\end{mmaCell}
\begin{mmaCell}{Print}
  a string " with double quotes inside \mmaFrac{c}{d}
\end{mmaCell}
\begin{mmaCell}[index=6]{Output}
  1+c
\end{mmaCell}
\begin{mmaCell}[form=FullForm]{Output}
  Plus[1,Times[2,z]]
\end{mmaCell}
\begin{mmaCell}[messagelink={message/General/argx}]{Message}
  Sin::argx: Sin called with 2 arguments; 1 argument is expected. >>
\end{mmaCell}
\begin{mmaCell}{Output}
  Sin[m,n]
\end{mmaCell}
\begin{mmaCell}{Output}
  \{1,\{2,3\}\}
\end{mmaCell}
\begin{mmaCell}{Code}
  (* Different roles of one symbol. *)
  x;
  _x;
  \mmaDef{x_};
  \mmaArg{x_}->2x;
  \mmaArg{x_}:>2\mmaArg{x};
  f[\mmaArg{x_}]=2x;
  f[\mmaArg{x_}]:=2\mmaArg{x};
  Block[{\mmaDyn{x}},2\mmaDyn{x}];
  Module[{\mmaLex{x}},2\mmaLex{x}];
  \mmaUnd{Module}; (* undefined Module symbol *)
  \mmaLinkBuiltin{Module}; (* Module symbol with link to documentation *)
  Sin[x\mmaErr{,x}];
\end{mmaCell}
\begin{mmaCell}[index=22]{Input}
  (* Nesting of "formatting boxes" *)
  \mmaSup{a}{\mmaSup{a}{a}} \mmaSub{a}{\mmaSub{a}{a}} \mmaSubSup{a}{\mmaSubSup{a}{a}{a}}{\mmaSubSup{a}{a}{a}} \mmaUnder{a}{\mmaUnder{a}{a}} \mmaOver{a}{\mmaOver{a}{a}} \mmaUnderOver{a}{\mmaUnderOver{a}{a}{a}}{\mmaUnderOver{a}{a}{a}} \mmaFrac{a}{\mmaFrac{a}{\mmaFrac{a}{a}}} \mmaSqrt{\mmaSqrt{a}} \mmaRadical{a}{\mmaRadical{a}{a}}
\end{mmaCell}
\begin{mmaCell}{Input}
  (* Replacements for infix operators in "Input" cells. *)
  x>=y; x<=y; x!=y; x->y; x:>y;
\end{mmaCell}
\begin{mmaCell}{Code}
  (* No replacement in "Code" cells. *)
  x>=y; x<=y; x!=y; x->y; x:>y;
\end{mmaCell}
\begin{mmaCell}[label={(\mmaCellIndex)custom}]{Code}
  (* Cell with custom label. *)
\end{mmaCell}
\begin{mmaCell}{Input}
  \mmaLinkTarget{x}=2;(* labeled definition of x *)
\end{mmaCell}
\begin{mmaCell}{Input}
  \mmaLinkLocal{x}(* usage of x with link to its definition *)
\end{mmaCell}

\end{document}
share|improve this answer
    
Welcome to TeX.SX! –  darthbith Jan 19 at 21:50

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

\documentclass[a4paper,12pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{listings,xcolor}

\lstset{language=Mathematica}
\lstset{basicstyle={\sffamily\footnotesize},
  numbers=left,
  numberstyle=\tiny\color{gray},
  numbersep=5pt,
  breaklines=true,
  captionpos={t},
  frame={lines},
  rulecolor=\color{black},
  framerule=0.5pt,
  columns=flexible,
  tabsize=2
}

\begin{document}
  \begin{lstlisting}[language=Mathematica,caption={Example code}]
    Integrate[{y^(-3)}*(1-(a/y)^2)^(-2),{y,r,Infinity}]
  \end{lstlisting}
\end{document}

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

Integrate[{y^(-3)}*(1-$\bigl(\frac{a}{y}\bigr)$^2)^(-2),{y,r,Infinity}]

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. Nov 27 '14 at 11:05

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.

\documentclass{article}

\usepackage{listings}
\usepackage{framed}
\usepackage{xcolor}
\usepackage{amsmath}
\colorlet{shadecolor}{gray!20}

\lstnewenvironment{mat}
{\lstset{language=mathematica,mathescape,columns=flexible}}
{}

\begin{document}

\begin{align}
\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}
\end{align}

\begin{shaded}
\begin{mat}

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 )]}

\end{mat}

\end{shaded}

\end{document}

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

 
discard

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.