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]
Best Answer
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
andOutput
look different.In cells with some styles (e.g
Input
orCode
) code syntax is colored, this can be achieved using already mentioned in other answerslistings
package.Cells with some styles (e.g.
Input
,Output
orPrint
) by default use, so calledStandardForm
, which allows embedding of complicated formatting (fractions, superscripts etc.) inside code. This was partially solved in other answers by usingmathescape
functionality oflistings
package. Problem with this solution is thatmathescape
"completely escapes" to TeX. Since one can't nestlistings
environments/commands, I don't see a way to treat parts of escaped content again as code (e.g. typeset it verbatim) usinglistings
only.To achieve such functionality one can use
Verbatim
environment fromfancyvrb
package. By setting propercommandchars
we can embed, for example,\frac
command inside code, in such way that frac's arguments are typeset verbatim. Downside offancyvrb
is that it doesn't offer automatic syntax coloring. Fortunatelylistings
has special interface tofancyvrb
that allows combining of reading code byfancyvrb
and typesetting it bylistings
.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.There's also corresponding Mathematica package: CellsToTeX, that automatically exports Mathematica code to TeX code compatible with
mmacells
. Mathematica package is described in detail in answer to "How best to embed various cell groups into a latex project?" question on Mathematica Stack Exchange.Usage example
Print screen of result:
TeX code:
Unicode
There are three strategies of handling Unicode supported by
mmacells
. Code for all of them can be automatically generated byCellsToTeX
Mathematica package.1. No Unicode
Don't use Unicode at all, use appropriate TeX commands instead. This approach works in all engines.
listings
package doesn't color elements provided by escaped commands, so they need to be wrapped with appropriate annotations.2. Unicode input
Use Unicode characters in input and automatically convert them to appropriate TeX commands by using
\mmaDefineMathReplacement
. This approach works in pdfTeX engine.Replacements are implemented using
listings
literate
option, so are "excluded" from automatic coloring, and identifiers containing Unicode characters need to be wrapped with appropriate annotations. Whether replacements will be used is controlled bymathreplacements
option. By defaultCode
cells don't use replacements (mathreplacements=none
),Input
cells use bold replacements (mathreplacements=bols
) i.e. will use given command wrapped with math delimiters and\pmb
,Output
,Print
andMessage
cells use "light" replacements (mathreplacements=light
) i.e. will will use given command wrapped with math delimiters.3. Unicode input and output
Use Unicode characters in input and rely on appropriate glyphs of used fonts. This approach works in Unicode-aware engines.
Since
listings
does not support Unicode, it needs to be switched off usinguselistings=false
option. Withlistings
switched off no automatic coloring occurs, so all identifiers, also those not containing Unicode characters, need to be wrapped with appropriate annotations.