The answer by @R.M. is what I would recommend to anyone who has the ability to install the required prerequisites. But as requested by @murray, here is an example of a complete $\LaTeX$ document that should have all the commands required for use with regular `pdflatex`

(i.e., it doesn't require `xelatex`

):

```
\documentclass[11pt,english]{scrartcl}
\usepackage{babel}
\usepackage{beramono}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{color}
\definecolor{identifiercolor}{rgb}{.4,.6,.56}
\definecolor{stringcolor}{gray}{0.5}
\definecolor{inactivecolor}{rgb}{0.15,0.15,0.5}
\usepackage{listings}
\lstset{basicstyle={\footnotesize\def\fvm@Scale{.85}\fontfamily{fvm}\selectfont},
breaklines=true,
escapeinside={\%*}{*)},
keywordstyle={\bfseries\color{inactivecolor}},
stringstyle={\bfseries\color{stringcolor}},
identifierstyle={\bfseries\color{identifiercolor}},
language=Mathematica,
otherkeywords={DiscretizeRegion},
showstringspaces=false}
\renewcommand{\lstlistingname}{Listing}
\begin{document}
Here I tell Mathematica to make a wave function plot:
\begin{lstlisting}[extendedchars=true,language=Mathematica]
Block[
{region=DiscretizeRegion[Polygon[{{0,0},{-1/2,Sqrt[3]/2},{1/2,Sqrt[3]/2}}]]},
ContourPlot[
2 Cos[4 Pi x] Sin[(4 Pi y)/Sqrt[3]] - Sin[(8 Pi y)/Sqrt[3]],
{x,y} %*$\in$*) region,
PlotPoints ->70,
Contours ->10,
AspectRatio ->Automatic,
FrameLabel ->{"x","y"},
PlotLabel ->"Excited state of the equilateral triangle"
]
]
\end{lstlisting}
To get some characters such as \textbackslash{}[Element] in the output,
I manually have to escpape from the listings environment and use the corresponding \LaTeX{} command.
\end{document}
```

Save this as `listingsExample.tex`

and run `pdflatex listingsExample`

. Make sure your editor doesn't automatically convert quotes `"`

to
$\LaTeX$ code (emacs does this by default). We want the code to be copied verbatim because it's supposed to be a source listing. The output should look like this:

I used the `beramono`

font to get the arrows `->`

to come out in a form that allows the code to work directly when copied back to *Mathematica*. With the default font, the arrows look OK in the PDF but don't get translated back correctly inside *Mathematica*.

Also, I use the line `basicstyle={\footnotesize\def\fvm@Scale{.85}\fontfamily{fvm}\selectfont},`

to switch the font in the listing from serif to something closer to the *Mathematica* style. This font switching code comes from this answer on TeX.SE by Jubobs.

I also added a keyword not yet recognized by the package in its current version, using the line `otherkeywords={DiscretizeRegion}`

.

For simplicity, the colors were chosen to look like the notebook display *before* any evaluation (i.e., keywords are blue). That way, I don't have to think about different colors for symbols that already have values.

The line `escapeinside={\%*}{*)}`

defines two character sequences that are recognized as delimiters surrounding the escape to $\LaTeX$ code inside the `listings`

environment.

1

possible duplicate of Saving a notebook as a $\LaTeX$ file, with syntax highlighting preserved

– Dr. belisarius – 2014-02-23T21:27:46.5501Thanks 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 – 2014-02-23T21:34:11.703

2

@Eckhard The short answer is: there is no such thing, because the highlighting as done by

– halirutan – 2014-02-24T04:35:27.230Mathematicarequires 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.3@Eckhard This is because the IDEA plugin understand

Mathematicasyntax 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 myuser knowledgeof TeX. – halirutan – 2014-02-24T04:39:27.5072If 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 – 2014-02-24T04:42:03.893

@Eckhard after a few moths, what´s your conclusion?. I´m very interested in the issue. – Mika Ike – 2014-06-21T06:15:59.130

@MikaIke Please see my answer below. – rm -rf – 2016-02-15T15:06:55.327

you can always save .nb file in pdf and use this

– egwene sedai – 2016-02-15T15:07:26.287Related: How best to embed various cell groups into a latex project?

– jkuczm – 2016-03-08T14:26:56.4201

Surprised there is no link to http://tex.stackexchange.com/questions/84748/fanciest-way-to-include-mathematica-code-in-latex/223898#223898 as a related question.

– SPPearce – 2017-03-14T09:52:54.597