2

I would like to output a latex-table as jpg from R. For example:

df <- data.frame(variable = c("a","b","c"), n=1:3)
xtable(df)

I could use knitr with:

\documentclass{article}
\usepackage[english,ngerman]{babel}

\begin{document}

<<echo=FALSE, results="asis">>=
xtable(xdf)
@

\end{document}

But this produces a pdf-file. I could screenshot the table and save it as jpg but I would like to avoid this work around.

My preference would be to output the latex-table from R into a windows

windows(width=20,height=15)

and then to save as jpg-file

savePlot("latex_table", type="jpg")

But I suppose that this is not possible since it has to be first interpreted by latex.

The reason why I want to do it in this way: I would like to produce regulary updated graphs and tables as jpg to insert them manually into a web-page. I want to use latex-table to have all the capacity of design to create nice tables.

giordano
  • 2,227
  • 3
  • 29
  • 49

1 Answers1

2

You may use ImageMagick's convert tool (executed with the system2() function in R) to convert a PDF file to JPEG. Before that you might wish to crop the margins in your LaTeX document (see pdfcrop). I cannot help you with explaining how to install these tools on Windows but e.g. on Fedora Linux you just sudo yum install texlive-pdfcrop-bin ImageMagick. Then you call:

  1. knitr::knit2pdf(...)
  2. system2('pdfcrop', 'input.pdf output.pdf')
  3. system2('convert', 'output.pdf output.jpg')

Everything can be set up to run in batch mode, without any user interaction, so you might even think of trying to automate uploading the figure to a web server.

EDIT: You may also try with \documentclass[11pt]{standalone} in LaTeX to produce a PDF page with minimal margins.

gagolews
  • 12,140
  • 2
  • 43
  • 71
  • Many thanks for your answer. I didn't know these tools. I installed them on my Windows 7 but I got two problems: (1) On R system2('pdfcrop.pl', ...) and system2('convert', ...) don't work and the latter gives "Invalid Parameter - output.jpg". (2) On DOS both commands produce the outputs but the former (pdfcrop) did only resize the table but didn't crop the white border. It will take me some time to find out how to use them (and why system2() does not apply). Anyway, these tools have some potentials. – giordano Feb 25 '14 at 13:47
  • Please make sure `convert` is in your PATH, see [here](http://stackoverflow.com/questions/9546324/). I've added a tip how to reduce margins by using the `standalone` document class and without `pdfcrop`. – gagolews Feb 25 '14 at 15:53