GeneralGetting latex to output Mathematica code as it's written

LaTeX specific issues not fitting into one of the other forums of this category.
Particle_Physicist
Posts: 6
Joined: Mon Mar 09, 2015 12:17 am

Getting latex to output Mathematica code as it's written

Post by Particle_Physicist »

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.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Getting latex to output Mathematica code as it's written

Post by Johannes_B »

Sure, package listings is usually used for the job.

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.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Getting latex to output Mathematica code as it's written

Post by Stefan Kottwitz »

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
LaTeX.org admin
Particle_Physicist
Posts: 6
Joined: Mon Mar 09, 2015 12:17 am

Re: Getting latex to output Mathematica code as it's written

Post by Particle_Physicist »

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?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Getting latex to output Mathematica code as it's written

Post by Johannes_B »

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.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Getting latex to output Mathematica code as it's written

Post by Stefan Kottwitz »

Particle_Physicist wrote:how to get macros to work - could you explain please?
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. ;-) The link of Johannes is very good.

While you are learning LaTeX, feel free to ask specific questions here.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Getting latex to output Mathematica code as it's written

Post by Cham »

Just for the fun of it, here's a complete MWE of some Mathematica code in a LaTeX document :

Code: Select all

\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}}
	}

\begin{lstlisting}[caption={Some description}]
 r1 := 0
 r2 := 12		(* La coordonnee radiale, en unites de a = 2GM/c^2 *)

 epsilon := 1	(* epsilon = 1 ou 0 pour les particules de matiere et les photons *)
 Newton := 0	(* Le potentiel est newtonien si Newton = 1 *)

 L[n_] := {0, 1, 2, 3, 4, 5}[[n]]	(* Le moment angulaire, en unites de a *)

 CouleurCourbe[n_] := {
   	RGBColor[0.2, 0.2, 0.2, 0.5],
   	RGBColor[1.0, 0.0, 0.0, 0.8],
   	RGBColor[0.2, 0.5, 0.2, 1.0],
   	RGBColor[1.0, 0.0, 0.0, 0.4],
   	RGBColor[0.0, 0.0, 1.0, 0.4],
   	RGBColor[1.0, 0.0, 1.0, 0.4]
 }[[n]]

 PotentielEffectif[r_,n_,epsilon_,Newton_] :=
	epsilon(1 - 1/r) + L[n]^2/r^2 - (1 - Newton)L[n]^2/r^3

 IdCourbe[n_,epsilon_] := Inset[
	Style[n - 1,FontSize -> 16,CouleurCourbe[n]],
	{n + 1,PotentielEffectif[n + 1,n,epsilon,Newton]},
	Background -> Directive[White,Opacity[0.75]]
	]

 Courbe[n_,epsilon_,Newton_] := ParametricPlot[
	{r,PotentielEffectif[r,n,epsilon,Newton]},
	{r,r1,r2},PlotStyle -> {Thick,CouleurCourbe[n]},
	RegionFunction -> Function[{r,PotentielEffectif},
		-1 < PotentielEffectif && r < 10
		]
	]

 Graphique = Show[Table[Courbe[n,epsilon,Newton],{n,1,6}],
	PlotRange -> {{0,10},{-1,4}},
	AspectRatio -> 1,
	Frame -> True,
	Axes -> True,
	AxesOrigin -> {0,0},
	AxesStyle -> GrayLevel[0.6],
	LabelStyle -> Directive[FontSize -> 12],
	FrameLabel -> {Style[r/a,FontSize -> 14],
	Style[2U,FontSize -> 14],
	Style["Le potentiel gravitationnel effectif",Bold,FontSize -> 18]},
	RotateLabel -> False,
	GridLines -> Automatic,
	GridLinesStyle -> Directive[GrayLevel[0.7],Dashed],
	Epilog -> Table[IdCourbe[n,epsilon],{n,1,6}]
	]
\end{lstlisting}

\end{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.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Getting latex to output Mathematica code as it's written

Post by Johannes_B »

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.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Getting latex to output Mathematica code as it's written

Post by Cham »

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.
Really ? I'm using both packages and never saw a clash or a problem between these packages (yet !).

Maybe it depends on the order sequence in the preamble ?
User avatar
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

Post by Johannes_B »

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.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply