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

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?

share|improve this question
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

Your Answer


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

Browse other questions tagged or ask your own question.