# [Latex / Sweave] - Are they easy to learn/use?

#### trinker

##### ggplot2orBust
Bryan and anyone else who cares to advise; the following is not really part of the original thread but the original thread has been solved and we seem to be veering here anyway...

I hear alot about Latex & sweave(sometimes used in [R]. Tell me more about it. Why should I learn it? How difficult is it to learn and then use once it's learned? Where are some good initial learning resources? Anything else you care to add about Latex.

#### jwxie518

##### New Member
Re: Small problem, calculating probability.

http://www.codecogs.com/latex/eqneditor.php

After a while you will be familiar with the general rules... they are not 100% standard... which you can pick up from latex's official.

A lot of time you use Latex to write mathematical formulas, let say you are writing a paper. Office's math engine bugs me. It's pretty solve. I am not sure about anything with R. So... okay hahaha

Another good option is MathType, but to me it is poorly integrated with Office, if you use MS Office to write documents.

#### bryangoodrich

##### Probably A Mammal
Re: Small problem, calculating probability.

The benefit of Sweave (which I haven't taken advantage of because I only started using Latex regularly was when I joined here and the math help forums) is that you can integrate R output directly into your document. For instance, you write your document in latex, including the formatting, symbols, etc., and where you want to print out graphs you enter special code that will run R code and replace it with R output. This is good if you're doing tables (see ?xtable I believe) and plots. But as I said, I haven't had a chance to use it. I don't produce output like that enough. However, when it comes to report writing, I can definitely see how it can streamline the analysis-to-report process that can otherwise be a pain (and that I am very familiar with lol).

#### Dason

I have used sweave before. It can be nice for the reason that bryangoodrich mentioned - streamlining reports. If you find yourself writing the same thing over and essentially just replacing the output with new output from a slightly different dataset then sweave can be a godsend because it will streamline the entire process for you. On the other hand it can be a major pain to get going. If your datasets are large and you don't take advantage of caching intermediate results if you find a small mistake in the report and want to make a small change in the R output then you have to let the whole thing run again. If it's just a small change in the latex you can modify the created latex document directly but it can still be annoying.

Some people used sweave to do their writeups for homeworks in a couple of classes here. I saw it as too much of a hassle to get working for homeworks.

As far as LaTeX itself goes - I think it's very valuable and is something everybody writing any sort of mathematics should be able to produce. It's not too bad to learn (I wish I could have convinced myself of this in undergrad though) and it's not that bad to use. Remember all it is is just a language for producing documents. It does that very well and if you're going to include any sort of mathematics in your document then in my opinion LaTeX is the only way to go (Microsoft Word? No way I'm using that...)

#### bryangoodrich

##### Probably A Mammal
That's one of the drawbacks, and why I never used Sweave. It has a big learning curve just to set up a simple report! Unless you know it will be efficient in what you are or will be doing, it is hard to find the motivation to use it. But it has economies of scale: once you have everything set up, your latex and code established, you can just reuse and reuse with minor tweaking. That is the problem though, getting up to that scale.

#### manoa

##### New Member
I use Latex on a daily basis to create reports, slides and all sorts of things, basically any documents, with math equations or not. MS WORD just doesn't do the job as good as Latex does. Latex is not hard to learn at all. A good text editor might help you get comfortable of use Latex. By the way, I think there are several packages in R that allow you to generate latex tables directly, but I have never used them. I always create the table manually--not fun though. What is Sweave you guys have been talking about? Is it also a R package?

#### Dason

xtable is probably the package you're talking about to create latex tables using R. I would definitely suggest giving it a try because it's really easy to use and can save you A LOT of work.

Sweave is actually just a function provided in the utils package. It accepts a certain file type that contains parts LaTeX code and parts R code and compiles it to LaTeX. It allows you to easily embed R code into the file so that you don't have to worry about moving the output to the LaTeX file directly. This can be really helpful for reports where you essentially do the same thing or have a standard template but run it on a different data set.

#### trinker

##### ggplot2orBust
OK I've got time to play with Latex a bit using the website jwxie518 provided. Pretty easy to work with thus far. It created the following html code. How do I not make it appear in posts as the actual formula? I tried Insert Image but I assume I'm not doing this correctly. If there's a link on this site to direct me how to do this please just direct me there.

<a href="http://www.codecogs.com/eqnedit.php?latex=\LARGE \fn_phv \bar{x}=\frac{\sum x_{i}}{n}" target="_blank"><img src="http://latex.codecogs.com/pdf.latex?\LARGE \fn_phv \bar{x}=\frac{\sum x_{i}}{n}" title="\LARGE \fn_phv \bar{x}=\frac{\sum x_{i}}{n}" /></a>

#### Dason

[noparse]$$\bar{x} = \frac{1}{n} \sum_{i=1}^nx_i$$[/noparse]

should give

$$\bar{x} = \frac{1}{n} \sum_{i=1}^nx_i$$

You just wrap the latex code you want to produce with math tags and you're good to go.

#### trinker

##### ggplot2orBust
Great success!:wave:

$$\textup{d}=\pi \textup{r}^2$$

Now I'm playing around with it for [R] using xtable. I get it to print out the table as either an html or tex file but am unsure of where to go from there.

How can a open a Latex file created by [R]? (it's .tex)
Is the idea to then transport this into a word document for publication?(an [R] created file)
If this is the case how do you get the html or tex output into word. (an [R] created file)
(I know i can google this stuff but you guys seem to have a good handle on this already)

Thanks for your help with this everyone. I'm like a kid with a new toy.

Last edited:

#### Dason

Wait... why are you moving tex over to work?

You typically compile tex/latex to a pdf document directly.

#### trinker

##### ggplot2orBust
Oh I get it. So that's how people create the manuals for [R]. I'm not sure if I'm ready to go this far with it yet. for right now I'll keep tinkering around with it and looking at the [R] resources and move at a very slow pace. Thanks again for the help.