【问题标题】:Too much white space between caption and figure produced by tikzDevice and ggplot2 in LaTeXLaTeX 中 tikzDevice 和 ggplot2 生成的标题和图形之间的空白过多
【发布时间】:2018-09-22 03:41:08
【问题描述】:

我目前正在使用 R 的 ggplot2tikzDevice 包来生成图形并在 LaTeX 文档中介绍它们,但是我正在努力解决在图形和标题之间产生的大空白,因为你可以看看你是否比较图像(我已经手动突出显示空间以使其更清晰):

这是我的 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}

我想知道如何消除标题和图形之间的巨大空间通过 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 页中获取了第二个情节的代码。

【问题讨论】:

  • 您是否尝试过通过 ggplot2 主题更改绘图边距?我仍然不明白它是如何特定于 tikz 设备的。如果您查看我在下面发布的示例,则会为 pdf 设备获得完全相同的边距。

标签: r ggplot2 latex tikz


【解决方案1】:

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

正如您在differences between \input{} and \include{} 上的这个问题中看到的那样,问题与\include{}\include{} 之前和之后执行\clearpage 的事实有关,这会产生我所说的空白。

另一方面,使用\input{} 相当于将代码复制并粘贴到 LaTeX 文档中,这样可以防止出现空格。

【讨论】:

    【解决方案2】:

    这听起来更像是一个 LaTeX 问题;我认为调整空间的标准方法是设置\abovecaptionskip

    \documentclass{article}
    \usepackage{tikz}
    \setlength{\abovecaptionskip}{-15pt} 
    \begin{document}
    
    \begin{figure}
    \centering
    \include{Rplots}
    \caption{\texttt{ggplot2} plot.}
    \end{figure}
    
    \end{document}
    

    【讨论】:

    • 感谢您的回复!这起到了作用,但它改变了文档中的每个数字。显然,只有用tikzDevice 创建的 tikz 数字(据我所知)在它们和标题之间有巨大的空白,所以我认为这可能是通过tikzDevice 解决这个问题的某种方法。我错了吗?
    • 在上面的例子中我没有注意到异常大的间距。您是否有一个示例表明标准 pdf 设备产生的边距比 tikz 图形输出的相同绘图要小得多?
    【解决方案3】:

    我仍然不明白您的问题,但让我建议这个完整的示例,显示在 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"))
    ```
    

    【讨论】:

    • 嗨 baptiste,抱歉回复晚了。我更新了我的问题,让问题更清楚。
    猜你喜欢
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 2021-05-19
    • 2019-03-22
    • 2018-05-02
    • 1970-01-01
    相关资源
    最近更新 更多