Making a couple of R plots in my LaTeX document on Overleaf. Every R plot is being placed inside of a figure environment:



  1. Plot margins too wide for every plot. Need to reduce whitespace around, and not through manual code like par(mar=c(1,1,1,1) in every chunk.
  2. No echoing of code in any chunk. Need to toggle echo=F globally across chunks.

Tried using dev.args but Overleaf probably does not support it yet. dev.args had some bugs with LaTeX too; knitr probably fixed them but maybe they're not updated in Overleaf's repository.

Any solutions? Any alternative routes?

  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Apr 11 at 17:50

1 Answer 1




knitr::opts_chunk$set(echo = FALSE, fig.dim = c(5, 3), fig.pos="h",out.width = "106%", dev='tikz')

See fig. \ref{labelA} and   \ref{labelB}. \lipsum[1][1]

<<figA, fig.cap="normalPlot \\label{labelA} " >>=


<<figB, fig.cap="simplePlot \\label{labelB} " >>=


  • Thanks for helping out! I found a workaround to the margin cropping part, using graphicx package's trim feature. All in all the code looks like this: knitr::opts_chunk$set(echo = FALSE, fig.dim=c(8,5), out.extra='trim={0 0.65cm 1cm 2.05cm},clip')
    – BeBlunt
    Apr 11 at 21:29
  • 1
    But I am not sure that crop all plots margins globally is really a good idea, not only because the risk of lost part of the image of some plots for some reason (e.g., your settings "as is" in my MWE are fine, but try now without removing the tikz device) but also because IMHO is more aesthetic have plot margins to highlight the plot even when this make it a bit smaller (i,e., I think that my MWE look better removing out.width).
    – Fran
    Apr 12 at 6:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .