| 1 |
% From mitthesis package |
| 2 |
% Version: 1.02, 2025/10/24 |
| 3 |
% Documentation: https://ctan.org/pkg/mitthesis |
| 4 |
|
| 5 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 6 |
|
| 7 |
%% NB: the listings package is not compatible with pdf tagging |
| 8 |
%% under TeX Live 2025 and earlier formats! Fatal errors may |
| 9 |
%% result from combining these! |
| 10 |
|
| 11 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 12 |
|
| 13 |
|
| 14 |
\chapter{Code listing} |
| 15 |
|
| 16 |
This example uses the \texttt{listings} package. |
| 17 |
|
| 18 |
\bigskip |
| 19 |
|
| 20 |
\lstdefinestyle{mystyle}{ |
| 21 |
backgroundcolor=\color{CadetBlue!15!white}, |
| 22 |
commentstyle=\color{Red3}, |
| 23 |
numberstyle=\tiny\color{gray}, |
| 24 |
stringstyle=\color{Blue3}, |
| 25 |
basicstyle=\small\ttfamily, |
| 26 |
breakatwhitespace=false, |
| 27 |
breaklines=true, |
| 28 |
numbers=left, |
| 29 |
numbersep=5pt, |
| 30 |
showspaces=false, |
| 31 |
showstringspaces=false, |
| 32 |
showtabs=false, |
| 33 |
tabsize=2 |
| 34 |
}% |
| 35 |
\lstset{language=[5.3]Lua,style={mystyle}}% |
| 36 |
|
| 37 |
\begin{lstlisting} |
| 38 |
\begin{luacode*} |
| 39 |
function print_rate(kappa,xMin,xMax,npoints,option) |
| 40 |
local c = 1-kappa*kappa |
| 41 |
local croot = (1-kappa*kappa)^(1/2) |
| 42 |
local logx = math.log(xMin) |
| 43 |
local psi = 0 |
| 44 |
|
| 45 |
local xstep = (math.log(xMax)-math.log(xMin))/(npoints-1) |
| 46 |
|
| 47 |
arg0 = math.sqrt(xMin/c) |
| 48 |
psi0 = (1/c)*math.exp((kappa*arg0)^2)*(erfc(kappa*arg0)-erfc(arg0)) |
| 49 |
|
| 50 |
if option~=[[]] then |
| 51 |
tex.sprint("\\addplot+["..option.."] coordinates{") |
| 52 |
-- addplot+ for color cycle to work |
| 53 |
else |
| 54 |
tex.sprint("\\addplot+ coordinates{") |
| 55 |
end |
| 56 |
tex.sprint("("..xMin..","..psi0..")") |
| 57 |
|
| 58 |
for i=1, (npoints-1) do |
| 59 |
x = math.exp(logx + xstep) |
| 60 |
arg = math.sqrt(x/c) |
| 61 |
karg = kappa*arg |
| 62 |
if karg<5 then |
| 63 |
-- this break compensates for exp(karg^2), which multiplies the error in the erf approximation... |
| 64 |
logpsi = -math.log(croot) + karg^2 + math.log(erfc(karg)-erfc(arg)) |
| 65 |
psi = math.exp(logpsi) |
| 66 |
else |
| 67 |
psi = (1/(karg) - 1/(2*(karg^3)) + 3/(4*(arg^5)) )/(1.77245385*croot) |
| 68 |
-- this is the large x asymptote of the reaction rate |
| 69 |
end |
| 70 |
logx = math.log(x) |
| 71 |
tex.sprint("("..x..","..psi..")") |
| 72 |
end |
| 73 |
tex.sprint("}") |
| 74 |
end |
| 75 |
\end{luacode*} |
| 76 |
\end{lstlisting} |