Mikael Jagan
Mikael Jagan

Reputation: 11336

Interaction between \parskip and tikzDevice affecting plot margins in .Rnw

I am knitting an .Rnw file and, unexpectedly, vertical space is lost from the bottom inner margin of my plots and added to the top outer margin.

Here is a minimal working example:

\documentclass{article}

\setlength{\parskip}{1em}

\begin{document}

<<mwe-plot, dev="tikz", dev.args=list(bg="grey95")>>=
par(mar=rep(2,4), oma=rep(0,4))
x <- seq(0, 2*pi, length.out=21)
plot(x, sin(x), type="l")
box("inner")
@

\end{document}

knit() generates mwe-plot-1.pdf, which looks like this:

plot with clipped margins, but what I expect is this:

correct plot.

The issue is apparently due to an interaction between \parskip and tikzDevice, since it is corrected by setting \parskip to zero in the preamble or switching from dev="tikz" to the default dev="pdf". While these are possible solutions, I am looking for a fix that doesn't require doing either.

Note that I tried adding \setlength{\parskip}{0em} immediately before the chunk (after \begin{document}) but this didn't affect the output.

Any hints are appreciated...

Upvotes: 1

Views: 167

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226761

Your proximal problem is that the \setlength{\parskip}{1em} statement is being included in the tikz .tex file, which is throwing off the spacing. The solution is to move it from your preamble into the body of your document, i.e.

\documentclass{article}
\begin{document}
\setlength{\parskip}{1em}
...

I figured this out by descending through the layers - first looking at the .tex output created by knit (before the LaTeX -> PDF conversion), then looking at figure/mwe-plot-1.pdf (and realizing, as you suggested, that the problem isn't with stuff that knitr puts in the LaTeX file, but with stuff that tikzDevice puts in the tikz-.tex file), then looking at figure/mwe-plot-1.tex and realizing that it had the \parskip statement embedded. I looked around for a tikzDevice option that would override it, but then realized that it was probably only including stuff from the preamble ...

Upvotes: 2

Related Questions