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?
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?
As rdhs mentionned, you can use the listings package. A sample example is:
\documentclass{article}
\usepackage{listings}
\lstloadlanguages{[5.2]Mathematica}
\begin{document}
\begin{lstlisting}
f[a_,b_]:=a^2+b^2+4b^3+25a*b*c ;
\end{lstlisting}
For inline stuff you can use \lstinline$f[a_,b_]:=a^2+b^2+4b^3+25a*b*c ;$
instead of the lstlisting environment.
\end{document}
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)
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.
\lstinline
has \verb
-style delimiters: \lstinline$f[a_,b_]:=a^2+b^2+4b^3+25a*b*c ;$
. The big difference is that it also has an optional keyval argument.
listings
package is 5.2, whereas Mathematica is currently at version 8?
Use \verb
, as in \verb|f[a_,b_]:=a^2+b^2+4b^3+25a*b*c ;|
. For longer programs, use the listings
package. Or use Mathematica's Save selection as PDF feature and include the PDF as an image.
To include an image of your Mathematica code, you can evaluate the cell
and then select it (by clicking on the right square bracket ]
). Then, following the menu's File
>Save Selection As...
and saving as a PDF produces the PDF output:
One advantage of this approach is that it will include the embedded Mathematica font, as well as the same spacing and layout as in the actual Mathematica display. If you which to get rid of the input/output indicators, you can either clip the image using graphicx
's trim=lx ly ux uy,clip
key-value, or extract the image before it is evaluated.
In[1]
and hit Delete to get rid of it before saving to pdf.
Commented
Aug 25, 2014 at 22:28
In addition to lstlisting
solution, here is an alternative way of doing it.
In Mathematica, under save as menu, choose LaTex Document format, .tex and the code will be generated for you automatically.
For example, the following code
will be converted to
\documentclass{article}
\usepackage{amsmath, amssymb, graphics, setspace}
\newcommand{\mathsym}[1]{{}}
\newcommand{\unicode}[1]{{}}
\newcounter{mathematicapage}
\begin{document}
\begin{doublespace}
\noindent\(\pmb{f[\text{x$\_$}]\text{:=}\text{Exp}[-a*(x-y){}^{\wedge}2];}\\
\pmb{f\text{''}[x]}\)
\end{doublespace}
\begin{doublespace}
\noindent\(-2 a e^{-a (x-y)^2}+4 a^2 e^{-a (x-y)^2} (x-y)^2\)
\end{doublespace}
\end{document}
And the resulting document is like
If you would like it to be centered, use \centerline
instead of \noindent
\documentclass{article}
\usepackage{amsmath, amssymb, graphics, setspace}
\newcommand{\mathsym}[1]{{}}
\newcommand{\unicode}[1]{{}}
\newcounter{mathematicapage}
\begin{document}
\begin{doublespace}
\centerline{$\pmb{f[\text{x$\_$}]\text{:=}\text{Exp}[-a*(x-y){}^{\wedge}2];}$}
\centerline{$\pmb{f\text{''}[x]}$}
\end{doublespace}
\begin{doublespace}
\centerline{$-2 a e^{-a (x-y)^2}+4 a^2 e^{-a (x-y)^2} (x-y)^2$}
\end{doublespace}
\end{document}