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.
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. $\endgroup$