General ⇒ Getting latex to output Mathematica code as it's written
-
- Posts: 6
- Joined: Mon Mar 09, 2015 12:17 am
Getting latex to output Mathematica code as it's written
So I'm writing my thesis and want to copy and paste the Mathematica code I used into the appendix. Problem is that latex doesn't like all the _'s and {'s etc, and I'd really prefer to not go through pages and pages sorting out each one by hand. How can I get around this? Is there something I can wrap the code in that means latex will present it exactly as written?
Cheers in advance.
Cheers in advance.
NEW: TikZ book now 40% off at Amazon.com for a short time.

- Johannes_B
- Site Moderator
- Posts: 4182
- Joined: Thu Nov 01, 2012 4:08 pm
Getting latex to output Mathematica code as it's written
Sure, package listings is usually used for the job.
Other packages exist as well, for example minted.
Other packages exist as well, for example minted.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
- Stefan Kottwitz
- Site Admin
- Posts: 10345
- Joined: Mon Mar 10, 2008 9:44 pm
Getting latex to output Mathematica code as it's written
Also I use
listings for such codes. Besides displaying as it is, monospaced, it offers language specific syntax highlighting and can be customized regarding design.
Stefan

Stefan
LaTeX.org admin
-
- Posts: 6
- Joined: Mon Mar 09, 2015 12:17 am
Re: Getting latex to output Mathematica code as it's written
Thank you. Such is my profound ignorance and newbness that I don't actually know/understand how to get macros to work - could you explain please?
- Johannes_B
- Site Moderator
- Posts: 4182
- Joined: Thu Nov 01, 2012 4:08 pm
Getting latex to output Mathematica code as it's written
Well, read some introductory material and learn how the LaTeX basics work. We won't explain you the very basic stuff, i could just copy a whole book in here, but that would be quite useless.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
- Stefan Kottwitz
- Site Admin
- Posts: 10345
- Joined: Mon Mar 10, 2008 9:44 pm
Getting latex to output Mathematica code as it's written
That's a very broad question. We could explain it on many pages, but I guess it doesn't matter if you would read our explanation or the explanation of a book author. The latter may be well-prepared and polished. It would be good to read the basics in a book or ebook. I could recommend my Beginner's Guide.Particle_Physicist wrote:how to get macros to work - could you explain please?

While you are learning LaTeX, feel free to ask specific questions here.
Stefan
LaTeX.org admin
Getting latex to output Mathematica code as it's written
Just for the fun of it, here's a complete MWE of some Mathematica code in a LaTeX document :
There aren't enough examples on the net about this kind of stuff, so I'm contributing a little here, if it can be usefull to someone (as it was for me).
It's a little sad that selecting and copying the code in the compiled PDF document and pasting back in a Mathematica document doesn't give a completely compilable code, without editing the lines. Unless there's some LaTeX or PDF tricks that I'm unaware of.
After some work in Mathematica, we can make that Mma code working back, though.
Code: Select all
Code, edit and compile here:
\documentclass[12pt,letterpaper,twoside]{book}\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}\usepackage[dvipsnames,table]{xcolor}\usepackage{subfig}% For Mathematica codes :\usepackage{listings}\captionsetup[lstlisting]{margin=0cm,format=hang,font=small,format=plain,labelfont={bf,up},textfont={it}}\renewcommand*{\lstlistingname}{Code \textcolor{violet}{\textsl{Mathematica}}}\definecolor{gris245}{RGB}{245,245,245}\definecolor{olive}{RGB}{50,140,50}\definecolor{brun}{RGB}{175,100,80}\begin{document}\section{Some Mathematica codes...}\lstset{tabsize=4,frame=single,language=mathematica,basicstyle=\scriptsize\ttfamily,keywordstyle=\color{black},backgroundcolor=\color{gris245},commentstyle=\color{gray},showstringspaces=false,emph={r1,r2,epsilon,epsilon_,Newton,Newton_},emphstyle={\color{olive}},emph={[2]L,CouleurCourbe,PotentielEffectif,IdCourbe,Courbe},emphstyle={[2]\color{blue}},emph={[3]r,r_,n,n_},emphstyle={[3]\color{magenta}}}
It's a little sad that selecting and copying the code in the compiled PDF document and pasting back in a Mathematica document doesn't give a completely compilable code, without editing the lines. Unless there's some LaTeX or PDF tricks that I'm unaware of.
After some work in Mathematica, we can make that Mma code working back, though.
- Johannes_B
- Site Moderator
- Posts: 4182
- Joined: Thu Nov 01, 2012 4:08 pm
Getting latex to output Mathematica code as it's written
Few hints and advice, package subfig doesn't play very nice with package hyperref, links are often broken. Better to use package subcaption of the caption bundle.
You can save your definition of mathematica code appearance globally, or even in an own file. That makes it much easier to re-use the code, especially for multiple documents.
You can save your definition of mathematica code appearance globally, or even in an own file. That makes it much easier to re-use the code, especially for multiple documents.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Getting latex to output Mathematica code as it's written
Really ? I'm using both packages and never saw a clash or a problem between these packages (yet !).Johannes_B wrote:Few hints and advice, package subfig doesn't play very nice with package hyperref, links are often broken. Better to use package subcaption of the caption bundle.
Maybe it depends on the order sequence in the preamble ?
- Johannes_B
- Site Moderator
- Posts: 4182
- Joined: Thu Nov 01, 2012 4:08 pm
Re: Getting latex to output Mathematica code as it's written
As both are designed to do the same job, it would be better for document maintainability to decide for one.
Critical decision to make now, as i know your document is quite advanced. Either investing time and effort now to get everything unified, or hope and trust that everything will work as well in the future. Either way, i trust you in backing up your work on a regular basis.
Critical decision to make now, as i know your document is quite advanced. Either investing time and effort now to get everything unified, or hope and trust that everything will work as well in the future. Either way, i trust you in backing up your work on a regular basis.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.