# Highlighting Mathematica code in $\LaTeX$ document

I was thinking about the best way to include Mathematica code in a $\LaTeX$ document with a nice syntax highlighting.

I have tried the packages listings and minted (with pygments), which both claim to include Mathematica syntax highlighting. There is also a separate Mathematica lexer for pygments on github.

Having looked at the output from these packages, I'm not entirely happy.

I was hoping to obtain a result resembling as closely as possible Mathematica's native syntax highlighting or the highlighting used here on mma.SE (is that halirutan's prettify extension?)

My question is: What are users' preferred ways to include Mathematica code in $\LaTeX$ files that preserve syntax highlighting?

-
Thanks for the link @belisarius: The answer in that thread concludes with "This code reduces your problem to implementing the syntax highlighter in Mathematica, or finding a LATEX package to do it for you" which is precisely what I'm asking for. I'm happy to manually copy and pasty Mathematica expressions into a LaTeX file, a process that the linked thread seems to automate. –  Eckhard Feb 23 '14 at 21:34
@Eckhard The short answer is: there is no such thing, because the highlighting as done by Mathematica requires a lot of work which is not done by any of the listing, minted, etc packages. Even the highlighter on SE that I wrote is only faking, especially the highlighting for pattern variables will not work reliably. The best way I see is to use my IDEA plugin and write an action to export highlighted and indented code. In IDEA, I have everything at hand and the complex highlighting is real. –  halirutan Feb 24 '14 at 4:35
@Eckhard This is because the IDEA plugin understand Mathematica syntax and semantic and can highlight and annotate very complex code constructs correctly. The hard part is: Even if I have all characters, their coloring and spaces, then this needs to be converted to colored LaTeX text where every character appears exactly as I want. I had already a look into the listing package and creating such output in TeX goes really beyond my user knowledge of TeX. –  halirutan Feb 24 '14 at 4:39
If I had the knowledge how to convert annotated code text into TeX commands so that the output is correct, one could use IDEA to copy Mathematica code there, autoamtically indent it correctly and then with one key-press you would have the LaTeX code in your clipboard ready to paste it into your document. –  halirutan Feb 24 '14 at 4:42

To get syntax highlighting for Mathematica in a latex code listing, try this:

\usepackage{listings}
\usepackage{color}
\definecolor{listinggray}{gray}{0.9}
\definecolor{graphgray}{gray}{0.7}
\definecolor{blue}{rgb}{0,0,1}
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}

% define a custom mathematica language for syntax highlighting
\lstdefinelanguage{myMMA}{
keywords={SetDirectory, NotebookDirectory, Exp, IdentityMatrix, Eigenvalues,
ListPlot, PlotRange, PlotStyle, Directive, PointSize, AspectRatio, Blue, Graphics, Line,
Nintegrate, For, DataRange, AxesLabel, PlotLabel, Transpose, Export, Plot, Append, Infinity},
keywordstyle=\color{black},
stringstyle=\color{mymauve},
identifierstyle=\color{blue},
sensitive=false,
comment=[l]{(*},
morecomment=[s]{/*}{*/},
morestring=[b]',
morestring=[b]"
}


Keep in mind that the keywords I've listed here are far from exhaustive. I tried to find a list of Mathematica keywords but gave up. So I just used the keywords that I actually used in my code.

## Edit

Here is a list of the keywords in a .txt file: https://www.dropbox.com/s/i3m8do7uof5uval/keywords.txt?dl=0

I found them by using

Names["System*"]
`
-
Have a look at Names. BTW welcome here at Mathematica.SE! You might want to consider changing your user name as it coincides with one of the top users here and it may create some confusion. –  Sjoerd C. de Vries May 19 at 5:47
Thanks a ton, that was frustrating me. And I'll change the name as soon as SE lets me. –  Belisarius May 19 at 6:02