# Tip [1] Insert R code in LaTeX

If you want to insert R code in your LaTeX document, you might want to consider using the “listings” package.

I have written some commands to display my R code in LaTeX in a similar format that my RStudio presents.

\documentclass[a4paper, 12pt]{article}
\usepackage[svgnames]{xcolor}

\usepackage{listings}
\lstset{language=R,                                     % set programming language
basicstyle=\small\ttfamily,                         % basic font style
stringstyle=\color{DarkGreen},
otherkeywords={0,1,2,3,4,5,6,7,8,9},
keywordstyle=\color{Blue},                          % keyword style
backgroundcolor=\color{AliceBlue},                  % background colour
numbers=left,                                       % display line numbers on the left side
numberstyle=\ttfamily\color{Gray}\footnotesize,     % line numbers
}

\title{LaTeX}
\author{handsonkeyboard}

\begin{document}
\maketitle

\begin{lstlisting}
m1 = matrix(c(1,2,3,4,5,6), nrow=3, ncol=2, byrow = TRUE)
m1 # print the matrix
\end{lstlisting}

\end{document}