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
Cheers in advance.
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
Other packages exist as well, for example minted.
- Stefan Kottwitz
- Site Admin
- Posts: 10335
- Joined: Mon Mar 10, 2008 9:44 pm
Getting latex to output Mathematica code as it's written

Stefan
-
- Posts: 6
- Joined: Mon Mar 09, 2015 12:17 am
Re: Getting latex to output Mathematica code as it's written
- Johannes_B
- Site Moderator
- Posts: 4182
- Joined: Thu Nov 01, 2012 4:08 pm
Getting latex to output Mathematica code as it's written
- Stefan Kottwitz
- Site Admin
- Posts: 10335
- 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
Getting latex to output Mathematica code as it's written
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}
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
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.
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
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.