{"word_count": 246, "character_count": 2150}
A nice post was recently published on the rsnippets blog, about the tikzDevice R package. This package is - indeed - awesome. Even if it has been removed from the CRAN website. Of course, it can be download from the archive folder, on http://cran.r-project.org/..., but also (for a more recent version)  on http://download.r-forge.r-project.org/.... But first, it is necessary to install the following package.&#62; install.packages("filehash")Then, we download on of the tikzDevice.zip files, and load it, e.g. using (on Mac)Then, we can load the library&#62; library("tikzDevice")If we want to use nice LaTeX formulas, it might be necessary to upload some (LaTeX) libraries and to specify the encoding format&#62; "options(tikzMetricPackages = c("\\usepackage[utf8]{inputenc}",+ "\\usepackage[T1]{fontenc}", "\\usetikzlibrary{calc}", "\\usepackage{amssymb}"))(this is detailed, e.g. in  http://yihui.name/...), then, we write a code to plot a graph. The idea is to produce a tex file which contains the graph, or more precisely which will produce a pdf graph when we compile it. We start with&#62; tikz("normal-dist.tex", width = 8, height = 4, + standAlone = TRUE,+ packages = c("\\usepackage{tikz}",+ "\\usepackage[active,tightpage,psfixbb]{preview}",+ "\\PreviewEnvironment{pgfpicture}",+ "\\setlength\\PreviewBorder{0pt}",+ "\\usepackage{amssymb}"))We will produce a 8x4 graph. The graph is the following,&#62; u=seq(-3,3,by=.01)&#62; plot(u,dnorm(u),type="l",axes=FALSE,xlab="",ylab="",col="white")&#62; axis(1)&#62; I=which((u&#62;=0)&#38;(u&#60;=1))&#62; polygon(c(u[I],rev(u[I])),c(dnorm(u)[I],rep(0,length(I))),col="red",border=NA)&#62; lines(u,dnorm(u),lwd=2,col="blue")We can add text (or TeX based text)&#62; text(-1.5, dnorm(-1.5)+.17, "$\\textcolor{blue}{X\\sim\\mathcal{N}(0,1)}$", cex = 1.5)&#62; text(1.75, dnorm(1.75)+.25, + "$\\textcolor{red}{\\mathbb{P}(X\\in[0,1])=\\displaystyle{\\int_0^1 \\varphi(x)dx}}$", cex = 1.5)And we end the file with a standard&#62; dev.off()This will produce a .tex file. If we compile this file, we can generate a pdf file, that can be inserted in lecture notes, slides or articles,Nice, isn't it ?