我真的不知道如何将 R 代码包含到 latex 中,所以我只能提供我的 R 代码。我想把它放在附录中。我真的很期待你的帮助。谢谢!代码将显示我想要包含 R 代码的位置。
\documentclass[12pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\begin{document}
This is the main part of the dissertation.
\newpage
\appendix
\section{The codes of equilibrium distribution.} \label{app:Codes}
\subsection{The analytical method.}
Here is the R codes.
\section{The codes of analysing the parameters in the model.} \label{app:Analysis}
Here is the R codes as well.
\end{document}
下面是 R 代码。
pi<-rep(1,n)
#Geometric
pi[2]<-a/(lambda*q+a*q+d)
for (i in 3:n){
f.1<-1
f.2<-1
for (j in 1:(i-2)){
f.1<-f.1*(a+j*lambda)
}
for (k in 1:(i-1)){
f.2<-f.2*(a+k*lambda+k*d/q)
}
pi[i]<-f.1*a/(f.2*(q^(i-1)))
}
pi[1]<-1/sum(pi)
for (i in 2:n){
pi[i]<-pi[i]*pi[1]
}
答案1
您可以使用listings
包裹:
代码
\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{lmodern}% better font than default
\usepackage{tgcursor}% tt font with bold/italic styles
\usepackage{listings}
\lstset{
language=R,
basicstyle=\ttfamily
}
\begin{document}
\appendix
\chapter{Some R code}
Some code I wrote. Hello World!
\begin{lstlisting}
pi<-rep(1,n)
#Geometric
pi[2]<-a/(lambda*q+a*q+d)
for (i in 3:n){
f.1<-1
f.2<-1
for (j in 1:(i-2)){
f.1<-f.1*(a+j*lambda)
}
for (k in 1:(i-1)){
f.2<-f.2*(a+k*lambda+k*d/q)
}
pi[i]<-f.1*a/(f.2*(q^(i-1)))
}
pi[1]<-1/sum(pi)
for (i in 2:n){
pi[i]<-pi[i]*pi[1]
}
\end{lstlisting}
Some code in an external file.
\lstinputlisting{mycode.r}
\end{document}
有关详细信息,请参阅包装手册……