Use Tex and Markdown in R



## I am a title

Bold text: **bold**

Italic text: *italic*

- first item
- second item
- third item


| Tables | Are | Cool |
| ------------- |:-------------:| -----:|
| col 1 | Hello | $1600 |
| col 2 | Hello | $12 |
| col 3 | Hello | $1 |

Below is an example of Latex:

f(x;\mu,\sigma^2) = \frac{1}{\sigma\sqrt{2\pi}} e^{ -\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2 }

Then, this is the inline version: $\alpha+\beta=\gamma$, $e=mc^2$.

Below is an example of R:
```{r block2, fig.width=8, fig.height=5, echo=FALSE}
Then, this is the inline version: `r 2*pi`.
%% LyX 2.0.3 created this file.  For more info, see
%% Do not edit unless you really know what you are doing.
 breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=false]
 pdfstartview={XYZ null null 1}}
<<setup, include=FALSE, cache=FALSE>>=
# set global chunk options
opts_chunk$set(fig.path='figure/minimal-', fig.align='center','hold')

\title{A Minimal Demo of knitr}

\author{Yihui Xie}

You can test if \textbf{knitr} works with this minimal demo. OK, let's
get started with some boring random numbers:


The first element of \texttt{x} is \Sexpr{x[1]}. Boring boxplots
and histograms recorded by the PDF device:

<<boring-plots, fig.width=4, fig.height=4, out.width='.4\\linewidth'>>=
## two plots side by side (option'hold')

Do the above chunks work? You should be able to compile the \TeX{}
document and get a PDF file like this one: \url{}.
The Rnw source of this document is at \url{}.
%% for inline R code: if the inline code is not correctly parsed, you will see a message
\newcommand{\rinline}[1]{SOMETHING WRONG WITH knitr}
%% begin.rcode setup, include=FALSE
% library(knitr)
% opts_chunk$set(fig.path='figure/latex-', cache.path='cache/latex-')
%% end.rcode

Boring stuff as usual:

%% a chunk with default options
%% begin.rcode
% 1+1
% x=rnorm(5)
%% end.rcode

For the cached chunk below, you will need to wait for 10 seconds for
the first time you compile this document, but it takes no time the
next time you run it again.

%% chunk options: cache this chunk
%% begin.rcode my-cache, cache=TRUE
% set.seed(123)
% x = runif(10)
% sd(x)  # standard deviation
% Sys.sleep(10) # test cache
%% end.rcode

Now we know the first element of x is \rinline{x[1]}. And we also know
the 26 letters are \rinline{LETTERS}. An expression that returns a
value of length 0 will be removed from the output, \rinline{x[1] =
  2011; NULL} but it was indeed evaluated, e.g. now the first element
of x becomes \rinline{x[1]}.

How about figures? Let's use the Cairo PDF device (assumes R $\geq$

%% begin.rcode cairo-scatter, dev='cairo_pdf', fig.width=5, fig.height=5, out.width='.8\\textwidth'
% plot(cars) # a scatter plot
%% end.rcode

Warnings, messages and errors are preserved by default.

%% begin.rcode
% sqrt(-1) # here is a warning!
% message('this is a message you should know')
% 1+'a'  # impossible
%% end.rcode