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}

\lstset{language=R,                                     % set programming language
    basicstyle=\small\ttfamily,                         % basic font style
    keywordstyle=\color{Blue},                          % keyword style
    commentstyle=\ttfamily \color{DarkGreen},           % comment style
    backgroundcolor=\color{AliceBlue},                  % background colour
    numbers=left,                                       % display line numbers on the left side
    numberstyle=\ttfamily\color{Gray}\footnotesize,     % line numbers



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



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s