[R] Bounding box on plotting files inserted into a LaTeX document

Marc Schwartz m@rc_@chw@rtz @end|ng |rom me@com
Fri May 17 01:52:43 CEST 2019



> On May 16, 2019, at 7:04 PM, Patrick Connolly <p_connolly using slingshot.co.nz> wrote:
> 
> I'm trying to write basic latex code to insert a pdf graphic into a
> document.  I can use Rstudio to knit an Rmd file successfully
> inserting the plot into the document.  I can get the latex code if the
> "save tex" box is ticked, so I get the correct syntax used.  
> 
> I don't need all the fancy things that .Rmd files can handle.  I just
> want a simple .tex file.  However, if I try to use a pdf file normally
> produced by using the pdf device, I get a message about a missing
> bounding box.  So I thought it might work to use R to make an eps
> file, which pdflatex automatically converts to a pdf.  It does import
> the graphic, but it takes up a whole page, which indicates that it
> ignores the bounding box.
> 
> What does work is to use Rstudio interactively to export the graphic
> to a pdf file which pdflatex handles correctly.  But that's a clunky
> procedure which I'd like to avoid.  It must be possible since graphs
> were included in LaTeX documents for decades before there was Rstudio.
> 
> TIA


Patrick,

Are you explicitly calling postscript() in an R session to create the figure?

If so, see the Details section of ?postscript, which notes the following:

The postscript produced for a single R plot is EPS (Encapsulated PostScript) compatible, and can be included into other documents, e.g., into LaTeX, using \includegraphics{<filename>}. For use in this way you will probably want to use setEPS() to set the defaults as horizontal = FALSE, onefile = FALSE, paper = "special". Note that the bounding box is for the device region: if you find the white space around the plot region excessive, reduce the margins of the figure region via par(mar = ).

So essentially:

postscript("YourPlot.eps", width = 6.0, height = 6.0,
           horizontal = FALSE, onefile = FALSE, paper = "special")

Plot code here...

dev.off()



Note that instead of pdflatex, you can also use latex, followed by dvips and then ps2pdf, if you want to go that way with the CLI workflow. Might depend upon whether or not you use other PS specific markup in the .tex file, like pstricks. For example:

latex YourTeXFile.tex
dvips YourTeXFile -o YourTexFile.ps
ps2pdf YourTeXFile.ps YourTeXFile.pdf


Regards,

Marc Schwartz



More information about the R-help mailing list