3

I really have no idea how to include the R code into latex, so I only can provide my R code. I would like to put it in the appendix. I am really looking forward to your help. Thx! The codes will show the place where I would like to include the R codes.

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

Below is the R code.

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]
}
6
  • 2
    You were asked to open a new question with a minimal working example (MWE). This is not an MWE. I cannot compile it to produce a document showing the issue you are experiencing. (Indeed, you don't say what the problem you are having even is.) Nobody can help you effectively if you don't make an effort to provide at least some information in your questions. – cfr Aug 25 '14 at 22:39
  • @cfr Sorry, I will re-edit the post, and I will explain the question as clear as possible. I really have no idea of how to include the R code in the latex, so I am afraid that I cannot provide the latex code. – Charles Yan Aug 25 '14 at 22:42
  • 2
    @CharlesYan perhaps you can post a LaTeX example and then in the place where you want to put your R code you can put a comment "Put R code here". This will at least show your document class and other important features of your document, and it will also show that you put some time into it. – scottkosty Aug 25 '14 at 22:45
  • @scottkosty Okay!!! – Charles Yan Aug 25 '14 at 22:54
  • 1
    @CharlesYan in addition, you might want to check out the listings package. If you read the listings package documentation (mirrors.ctan.org/macros/latex/contrib/listings/listings.pdf) and make an attempt to solve your question, even if you fail (but you explain precisely what fails), that -1 score will probably go away. – scottkosty Aug 25 '14 at 22:59
8

You could use the listings package:

result

Code

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

See the package manual for further information …

1
  • You're welcome. If my answer solves your problem you may accept it by clicking the check mark below the voting arrows. :-) – Tobi Aug 26 '14 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.