Lyx and Sweave: Worth climbing the learning curve?

One of the interesting elements of using Linux is the demystification of many *nix concepts that you might have come into contact previously in a lateral manner. One of these is Latex. Any R user in Windows quickly becomes aware that Latex exists. However Latex is fundamentally a *nix thing. It has even been claimed by Windows users that Latex is a legacy typesetting paradigm!

There are certainly some issues with the complexity of Latex. However there is no denying that Latex documents look impressively formal. Personally I think it is worth the effort to look seriously at latex, even if you are a complete newcomer to Linux.

I was curious about Latex. However I readily admit that I really couldn’t be bothered to learn latex as such. Life is far too short. Fortunately I found Lyx. Lyx is Latex for the lazy. No need to learn all the details, it is advertised as a WYSWYM latex typesetting program (What you see is what you mean).

So how do you use Lyx? The first important tip is to make sure that you install ABSOLUTELY, BUT ABSOLUTELY, EVERYTHING remotely relevant to TEX and Latex before starting anything with Lyx. Fonts, languages, the lot. This  avoids later frustrations with missing sty files etc. I can’t remember offhand all the packages I needed, but do go to Synaptics and look for texlive-full plus anything else remotely related with either tex or latex. This will mean quite a long initial download but you certainly won’t regret it. Without all the Tex/Latex stuff installed the Lyx experience can be frustrating. You are likely to get at least a few annoying and incomprehensible error messages when compiling documents. This could give you the feeling that the whole system is more trouble than it is worth. You will add about 500 MB to your install in total, but that shouldn’t be a problem.

The next step before using Lyx seriously for work is to analyse your own ability with Word or Open Office. The idea of using Lyx is to save time in the long run. Experienced Office users can probably replicate everything Lyx or Latex can do. However many of us do have problems writing long documents such as theses or technical reports in Office suites. It becomes very difficult to maintain a consistent style. I now find Lyx easier to use than any office program for long document. The logic of using Lyx is to improve productivity, not make simple tasks much more difficult. Define what was most difficult for you to use in an Office suite and find a consistent way to achieve it in Lyx. My main problems were with positioning figures on the page and producing correctly structured tables of content. Both are very easy in Lyx once you have found out how, but there is an initial learning curve.

Then follow the examples in the Lyx documentation and allow some time for experimentation.

Sweave in Lyx

Most R users will  be very interested in the use of Sweave with Lyx. This has been made possible by the work of Gregor Gojanc. I have only recently realised just how cool this can be. Once all the Latex extras are installed in Ubuntu following the instructions in “INSTALL” here worked well.

A faster way of getting to this point would be to download the file below which contains my .lyx directory hidden as a zip file with fake doc extension. Replace the whole of the .lyx directory you have in your home directory by mine. Remember to use control H to see hidden files. You might want to back up your original directory first, but the replacement shouldn’t cause any issues.


Then do tools/reconfigure in Lyx and you should be able to use Sweave. My directory also has a layout incorporated for making beamer files that I got from here.

Here is an example


The source in Lyx that made this is available here (again take of the doc extension and change it to .lyx), You’ll need beamer-latex installed to compile it and you will have to provide your own logo picture.


And here is an early draft of a course for beginners to R that I am writing in Lyx/Sweave. If the documents compile then you have everything installed correctly.


Again there is a bit a sharp curve up to get Lyx and Sweave working. You will need to read the sweave manual first (A Google for Sweave will provide other material)

It is admitedly hard to get Sweave/beamer working as this is still quite an experimental concept. I found that not all the beamer features work, but you can still get some nice looking slides. The easiest way to use beamer for the time being is to adapt my template by cutting and pasting the box of “ERT”  for every new slide, changing the R code within them to what you need. Some styles such as Title and Author stop the document compiling, so don’t change the title page much. Remember to put your own logo in the graphic. The huge payoff in terms of time saved producing slides is made by \begin{frame}[shrink,containsverbatim]. The shrink option automatically adjusts R output to the page. Great as long as you don’t print a huge object. Also notice the use of xtable and <<results=tex>> in code chunks to produce formatted tables.

Inserting a histogram into a normal Lyx/Sweave document like the R course is very simple. In an ERT box you just need to write.




The great thing about this way of working is you know all the R code you are showing actually runs, as they document won’t compile if you’ve made a typo or syntax error.

I will be mentioning Lyx with bibtex in a later post as this is another potentially major productivity booster that may need some help to use at the start.

A presentation for the first class on the introductory R course I am writing is now available here


8 thoughts on “Lyx and Sweave: Worth climbing the learning curve?

  1. Es estupendo Duncan,

    tengo que preparar un curso de doctorado de análisis de datos en ecología en R y esto me va a ser de mucha utilidad.

  2. Pingback: Utilizar Sweave con la clase Beamer en LyX « Un Bioinformatiquillo

  3. worth noting that to get Lyx-Sweave examples working you must check that you have all the necessary R packages installed, otherwise it fails cryptically without defining where (although it is apparent it is in the R/Sweave elements – reference to .Rnw).
    Of course one could check all the R elements directly in R which will give more information as to the failure point.

  4. Hola Duncan,
    Hoy me tope todo el día con la parte más inclinada de la curva de aprendizaje. Me funciona cuando uso directamente TeXWorks, pero Lyx no quiso colaborar con los scritps de R.
    Ojalá podrías dar una tardecito de taller para hacer funcionar lyx.

  5. This is a really fair assessment. I think most tech topics, LaTeX included, get a “suck it up and learn” treatment — but as you say, life is too short. So I agree with everything you said above, about installation, why you’d want to switch, and taking the lazy road.

    I’ll add this: I did my most recent business plans in LaTeX and also a couple reports for clients. I agree that it looks much more professional … and as far as client reports, it’s actually much easier to type in a plain text editor (thinking about the content) and somebody else’s choices “hit the Beautify button” for you … rather than debating forever about point sizes & fonts, and debate about how long to debate about point sizes & fonts……

  6. Also good is Brent Collier’s PDF on installing lyx in ubuntu (karmic) from may 2010. Both of these are like 1-2 pages apiece.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s