LaTeX 中 tikzDevice 和 ggplot2 生成的标题和图形之间有太多白色 space

Too much white space between caption and figure produced by tikzDevice and ggplot2 in LaTeX

我目前正在使用R的ggplot2tikzDevice包来制作图形并在LaTeX文档中介绍它们,但我正在为由此产生的大白而苦恼spaces 在图形和标题之间,如果你比较图像就会看到(我手动突出显示 spaces 以使其更清晰):

这是我的 MWE:

R代码:

library(ggplot2)
library(tikzDevice)

set.seed(1)
x <- rnorm(200)

tikz(file = "Rplots.tex", width = 4, height = 4)
qplot(x, geom = "histogram")
dev.off()

和 LaTeX 代码:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{figure}
\centering
\include{Rplots}
\caption{\texttt{ggplot2} plot.}
\end{figure}

\begin{figure}
\centering
\begin{tikzpicture}[scale=3]
    \clip (-0.1,-0.2)
    rectangle (1.8,1.2);
    \draw[step=.25cm,gray,very thin]
    (-1.4,-1.4) grid (3.4,3.4);
    \draw (-1.5,0) -- (2.5,0);
    \draw (0,-1.5) -- (0,1.5);
    \draw (0,0) circle (1cm);
    \filldraw[fill=green!20!white,
    draw=green!50!black]
    (0,0) -- (3mm,0mm)
    arc (0:30:3mm) -- cycle;
\end{tikzpicture}
\caption{\texttt{tikz} plot.}
\end{figure}

\end{document}

我想知道如何去除标题和图形之间的巨大 space via ggplot2.

PS。 R 版本:3.2.3,ggplot2 版本:2.1.0,tikzDevice 版本:0.10-1。我从 Tobias Oetiker 的 The Not so Short Introduction to LaTeX 2e,5.05 版,第 116 页中获取了第二个情节的代码。

听起来更像是一道 LaTeX 题;我认为调整 space 的标准方法是设置 \abovecaptionskip

\documentclass{article}
\usepackage{tikz}
\setlength{\abovecaptionskip}{-15pt} 
\begin{document}

\begin{figure}
\centering
\include{Rplots}
\caption{\texttt{ggplot2} plot.}
\end{figure}

\end{document}

我仍然不明白你的问题,但让我建议这个完整的例子,它显示了在 knitr 文档中使用时 tikz 和标准 pdf 设备的间距完全相同。

---
title: "Untitled"
header-includes:
- \usepackage{caption}
output: 
  pdf_document: 
    fig_caption: yes
---

First, we try this


```{r pdf, echo=FALSE, dev='pdf', fig.height=2, fig.width=2, fig.cap='This is a standard graphic.'}
library(ggplot2)
ggplot() + theme(plot.background=element_rect(colour = "red"))
```

Next, we try this

```{r tikz, echo=FALSE, dev='tikz', fig.height=2, fig.width=2, fig.cap='This is a tikz graphic.'}
ggplot() + theme(plot.background=element_rect(colour = "red"))
```

\newpage

## Fixing space

\captionsetup{skip=0pt}

```{r pdf2, echo=FALSE, dev='pdf', fig.height=2, fig.width=2, fig.cap='This is a standard graphic.'}
library(ggplot2)
ggplot() + theme(plot.background=element_rect(colour = "red"))
```

```{r tikz2, echo=FALSE, dev='tikz', fig.height=2, fig.width=2, fig.cap='This is a tikz graphic.'}
ggplot() + theme(plot.background=element_rect(colour = "red"))
```

好久没问这个问题了,想回答一下到底是什么问题

正如您在 differences between \input{} and \include{} 上的这个问题中看到的那样,问题与 \include{}\include{} 之前和之后执行 \clearpage 这一事实有关,生成我正在谈论的白色 space。

另一方面,使用 \input{} 相当于将代码复制并粘贴到 LaTeX 文档中,这样可以防止白色 spaces.