I am a newbie in Latex. I have two simple questions regarding including R code in Latex.

Q1: For a block of R code, I use 'listings' package, as follows.

> dpareto(4, 2, 3)
[1] 0.09375
> dpareto(4, -2, 3)
[1] NaN
> dpareto(seq(-1, 4, 1), 2, 3)
[1] 0.0000000 0.0000000 0.0000000 0.0000000 0.2962963 0.0937500

I feel it's not professional. What is the best way to present the block of R code?

Q2: For code pieces in the paragraph, for example I wish to include a R function, or variable, or package name in my Latex context, what is the best way to do that?

  • 1
    Welcome to TeX.SX! Asking one question in a post is preferred ;-) – user31729 Jan 30 '15 at 5:24
  • Using \lstinclude you can include the code from external files, this could make sense in your case. Besides that I think that knitR or the older Sweave are the top tools to combine R and LaTeX. – Uwe Ziegenhagen Jan 30 '15 at 5:34
  • @ChristianHupfer, thanks for the comments. I feel my two questions are highly related. So I put them into one post. – Aaron Zeng Jan 30 '15 at 16:31

Better than list R code, you can make a my_sweave_file.Rnw file and compile it using knitr to execute the code, list it and show real results.


Knitr test in \LaTeX.


dpareto <- function(x, location, shape, log = FALSE) {
    if (!is.logical(log.arg <- log) || length(log) != 1)
        stop("bad input for argument 'log'")

    L = max(length(x), length(location), length(shape)) 
    x = rep(x, length.out = L);
    location = rep(location, length.out = L);
    shape = rep(shape, length.out = L)

    logdensity = rep(log(0), length.out = L)
    xok = (x > location)
    logdensity[xok] = log(shape[xok]) + 
            shape[xok] * log(location[xok]) -
            (shape[xok]+1) * log(x[xok])
    if (log.arg) logdensity else exp(logdensity)

dpareto(4, 2, 3)
dpareto(4, -2, 3) 
dpareto(seq(-1, 4, 1), 2, 3)

To compile this .Rnw file you can do:

Rscript -e "library(knitr); knit('my_sweave_file.Rnw')"
pdflatex my_sweave_file.tex

But with editors like RStudio or LyX you can compile this files directly.

Note that the listed R code of you MWE is confusing and not reproducible, as you do not explain where dpareto() come from, so. If you use the library PtProcess instead of the listed function above, the result will be ratter different:



Knitr test in \LaTeX.

<<Test, tidy=TRUE>>= 
dpareto(4, 2, 3)
dpareto(4, -2, 3) 
dpareto(seq(-1, 4, 1), 2, 3)

Done. The true first result is \Sexpr{dpareto(4, 2, 3)}, not 0.09375
as showed in the MWE.


Or with the library actuar:


  • 1
    Thanks a lot. That's really helpful. One follow-up question. So essentially I need first write .Rnw file, and then compile it to .tex file, then to .pdf file. Is that correct? Additionally, if I wish to put R variable name or function name within a paragraph, what is the font/stype/format to do that? For example, I wish to say "here is the R function dpareto", then How I can format "dpareto" in the context to mark it as R code? – Aaron Zeng Jan 30 '15 at 16:37
  • That is, but with some editors is just push one bottom. Search also about Sweave (the still working ancestor of knitr). The last example show the two ways to include R code: between paragraphs with <<>>= and @ (that is like copy & paste chunks from the R console) and within paragraphs with \Sexpr{} that is formatted as plain LaTeX text. Read also about xtable and LaTeX ouput of Hmisc and others R packages here. – Fran Jan 30 '15 at 17:47

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.