Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn a LaTeX Sweave file (Rnw) into HTML?

Tags:

html

r

sweave

I searched for this, but couldn't find a solution.

I understand that one could use the HTML markdown with Sweave, and then output it to HTML using:

library(R2HTML) 
Sweave('temp1.rnw', driver = RweaveHTML)

What I am wondering though, is if there a way to turn the .tex file that is created into an HTML file, but through R.

p.s: I am looking for a solution for windows. I've seen that other OS already has their own solutions.

Thanks.

like image 487
Tal Galili Avatar asked Dec 28 '22 13:12

Tal Galili


2 Answers

TeX to HTML is a non-trivial task, because TeX is so general. As @richiemorrisroe said, mk4ht is available on Windows. So is tth (the other method suggested at the Vanderbilt page you linked to). I don't think you want to write a TeX parser in R ... Can you tell us why you want a pure-R solution? Is it just for the sake of having the solution self-contained?

I don't think the installation is really that hard. This should get you most of the way there ...

TTHurl <- "http://hutchinson.belmont.ma.us/tth/tth-noncom/tth_exe.zip"
SWconvurl <- "http://biostat.mc.vanderbilt.edu/wiki/pub/Main/SweaveConvert/sweave2html"
download.file(TTHurl,dest="tth.zip")
unzip("tth.zip") ## creates tth_exe
download.file(SWconvurl,dest="sweave2html")
Sys.chmod(c("tth_exe","sweave2html"),mode="0755") ## ???

You will need ImageMagick (binary downloads here) too if you want to convert PDF to PNG on the fly ...

tth is a little less general than mk4ht, which contains a complete (La)TeX parser, but it's also more lightweight -- useful if you want to give this recipe to other users to install and don't want them to have to download oodles of stuff (unfortunately ImageMagick is pretty big -- these days, you can probably concoct a solution where you generate the images in PNG in Sweave in the first place).

like image 160
Ben Bolker Avatar answered Dec 30 '22 08:12

Ben Bolker


Well, its not really a pure windows solution, but you can use the tex4ht package, and call htlatex on the latex file after sweaving.

Something like system("htlatex somesweavedfile.tex") following running Sweave from within the R GUI (which is what I presume you mean). Incidentally, this html can also be opened by open office and then converted to word, which is always useful.

I always did this (on Windows) from a command line, and the help page for ?system notes that some commands may not work properly on Windows. From my reading of the relevant help page, it appears that this one will. The only difficulty might be if the htlatex command has a problem and tries to let you know, then I'm not sure if the readings from stderr will come back to the R GUI.

Just to note Tal, the mk4ht package is also available on Windows, but I can see how you might not have gotten that impression from the webpage, which is very Linux-specific (and also quite useful to me, thanks for the link!)

EDIT: in response to Tal's comment below.

If you install MikTeX on windows, it will give you a package manager which you can use to install mk4ht. This should (all paths being set correctly) allow you to carry out my answer.

like image 33
richiemorrisroe Avatar answered Dec 30 '22 06:12

richiemorrisroe