4

I tried all answers posted here or on other pages, but nothing worked. I need to set the parameters for listings package to simulate R code, so:

\usepackage{listings}
\lstset
{
language=R,
% parameters here
{

All the suggested questions/answers gave me some strange-looking code. Maybe R has different code coloring for different versions? I have R studio, it's a relatively new R environment.

5
  • if nothing satisfactory exists you can write your own language bindings, it's not particularly difficult. Start with the keywords and comment characters, and go on from there.
    – alexis
    Commented Nov 28, 2013 at 23:16
  • 2
    Have you tried SweaveListingUtils or knitr?
    – hpesoj626
    Commented Nov 29, 2013 at 0:14
  • @hpesoj626 I haven't tried them. How do I actually use those packages? Do they enable me to include R files into my document? Or they enable me to type R-like code in my document?
    – Vidak
    Commented Nov 29, 2013 at 23:00
  • 1
    Personally, I use the knitr package by Yihui, who is also a member of this site. Yihui has some demo files in knitr's official site: yihui.name/knitr. What you do is you write your LaTeX file as you usually do but save it as a .Rnw file instead of .tex. Of course you should install knitr in R first using install.packages. You then load it via library(knitr) and knit your document by running knit("yourfile.Rnw") in your R console. I usually use knitr with Rstudio.
    – hpesoj626
    Commented Nov 30, 2013 at 0:51
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers.
    – jub0bs
    Commented Jan 13, 2014 at 14:35

1 Answer 1

4

Here is a simple example to get you started.

\documentclass{article}
\usepackage{listings}
\lstset{language=R,literate={<-}{{$\gets$}}1}
\begin{document}
\begin{lstlisting}
  a <- c(1,2,3)
  b <- data.frame(this=a,that=c(3,4,5))
\end{lstlisting}
\end{document}

The "literate" part gives you nicer assignment arrows, which is useful if you use "<-" for assignment, and there is a great deal more customisation that you can do. For full details you should read the fine manual on CTAN.

2
  • that's nice, thank you, but could you maybe suggest more parameters? for a complete syntax? :) if i knew how to create them, I would've done so on my own :P
    – Vidak
    Commented Nov 28, 2013 at 23:51
  • 1
    @Vidak Does this help?
    – masu
    Commented Nov 29, 2013 at 7:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .