[Tex/LaTex] How to include Mathematica code in a document


I want to include some Mathematica code in a LaTeX document.

It's along the lines of
f[a_,b_]:=a^2+b^2+4b^3+25a*b*c ;

However, when I build my PDF, the _ and ^ always render. Is there a way to get it to just show up as it shows above?

Best Answer

As rdhs mentionned, you can use the listings package. A sample example is:




f[a_,b_]:=a^2+b^2+4b^3+25a*b*c ;

For inline stuff you can use \lstinline$f[a_,b_]:=a^2+b^2+4b^3+25a*b*c ;$
 instead of the lstlisting environment. 

Note the verb like delimiters for lstinline. One thing however, the listings manual indicates verb like delimiters, but in all of my work I use the usual { and } and it works fine (maybe a quirk, but I find it easier to read and will continue using it as long as possible)

enter image description here

There are other ways also. For example the minted package does what you what, but it requires that you install Pygments, which is a Python library.

Related Question