2

I'm writing a large latex document with lots of plots generated by R's tikzDevice package. Currently, I'm experiencing LaTeX error "TeX capacity exceeded".

I've managed to fix this issue temporarily by following the remedy from Leo Liu's answer here (I've also tried the accepted answer from the same source), but this solution is temporary as I said, because eventually, I will be adding many more plots to my document with at least 1 meg per plot. Some people say that LuaTex dynamically allocates memory so it is never an issue with LuaTex (same source); however, I have a big document and limited time available, so converting to LuaTex is not an option (unless there is an automatic conversion software).

During my search, I found this post by chance talking about the same problem but with matlab's matlab2tikz package. The solution there is to reduce the number of samples in the plot and thus reduce the size of the resulting file. I looked up R's tikzDevice documentation (here and here) for a similar option, but was not able to find one unfortunately.

So the question is: How can I control the size of the plots (tikzpictures) generated by tikzDevice?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    2 comments: i) without code and data, it's impossible to tell what strategy you could use in R/ggplot2 to "simplify" (aggregate/bin/smaple/summarise,...) the data before plotting; ii) you could use luatex to process the individual figures, and include the resulting pdfs in your final document. I find it easier than having to debug tikz code embedded in a larger document. Another option, albeit of little practicality, is to convert the data [layer as embedded raster](http://stackoverflow.com/a/42059772/471093). – baptiste Apr 23 '17 at 00:19

1 Answers1

0

One workaround is to export the file produced by tikzDevice with option standAlone = TRUE. Compile the resulting file with lualatex to avoid the error message "TeX capacity exceeded". This can be done directly from R, using e.g.

tikz(figure.tex, standAlone = TRUE)
...
dev.off()
system("lualatex figure.tex; rm *.aux; rm *.log")

Then, include the file in LaTeX as a graphic using \includegraphics.

To reduce the size of the resulting pdf, you may wish to convert the image to a lossy compression format such as JPEG. Converting to postscript and back can also create smaller files, albeit at the cost of a loss of resolution.

lbelzile
  • 175
  • 8