使用knitr、HTML/markdown和PDF/Latex实现一致的图形字体大小。

时间:2021-11-02 20:04:44

I am trying to troubleshoot a noticeable difference in font size when comparing plots generated for a knitr document. HTML/markdown output appears to be what I would consider the correct size while the PDF/Latex output is approximately 2-4 pts larger (e.g., if I was expecting 12pt, the output is closer 14 or 16 pts). Note the issue seems to be limited to the plot/figure text and doesn't seem to manifest with other text in the output document.

在比较为knitr文档生成的图时,我试图排除字体大小的明显差异。HTML/markdown输出似乎是我认为正确的大小,而PDF/Latex输出大约大2-4点(例如,如果我预期为12pt,输出接近14或16点)。注意,这个问题似乎仅限于图/图文本,并没有与输出文档中的其他文本一起出现。

The simplest way for me to demonstrate this is with the default content that RStudio (version 0.99.329) populates when a new *.Rmd file is created. A side-by-side comparison is available via this link. To my eye, the HTML version is properly sized and the PDF/Latex version is not.

我演示这一点的最简单方法是使用RStudio(0.99.329版)在新*时填充的默认内容。限制型心肌病创建文件。可以通过这个链接进行并排的比较。在我看来,HTML版本大小合适,而PDF/Latex版本则不然。

To my knowledge, I don't have any custom settings within my .Rprofile or other locations that could be impacting this issue. I have tried disabling/enabling pdf crop and that does not seem to impact the issue either. I have duplicated the issue on a colleagues machine (also running OS X and the same version of RStudio).

据我所知,在我的. rprofile或其他可能影响这个问题的位置,我没有任何自定义设置。我已经尝试禁用/启用pdf作物,这似乎也没有影响到问题。我在同事机器上复制了这个问题(也运行OS X和RStudio的相同版本)。

Outside of the rmarkdown framework (i.e., just running R code) there does not appear to be such a difference between PDF and PNG output. For example, the following code produces output that is fairly similar:

在rmarkdown框架之外(即:) PDF和PNG输出之间似乎没有如此大的差异。例如,以下代码产生的输出非常相似:

library(ggplot2)
r <- ggplot(data = diamonds, 
    aes(x = carat, y = price, color = cut, group = cut))
r + geom_smooth(size = 2) + 
    ggtitle("Plant growth with\ndifferent treatments")

ggsave(file="test.pdf")
ggsave(file="test.png")

Given that this code above produces output plots that are fairly similar, I'm suspicious the issues I'm seeing are related to knitr or the rmarkdown/pandoc conversion process.

鉴于上面的代码产生了相当相似的输出图,我怀疑我看到的问题与knitr或rmarkdown/pandoc转换过程有关。

So, my main question is whether the level of differences I'm seeing between output formats is expected? Am I the only one? Is it unique to my system?

因此,我的主要问题是,我看到的输出格式之间的差异是否在预期之内?我是唯一的吗?它是我的系统独有的吗?

If it is expected behavior, how are folks reconciling the issue? For ggplots, I've been using theme_bw(8.5) to downscale the fonts for PDF/latex output. This works, but it adds another level of complexity when creating output for multiple platforms/uses from the same *.Rmd file ... one of the key benefits of rmarkdown.

如果是预期的行为,那么人们如何协调这个问题呢?对于ggplot,我一直在使用theme_bw(8.5)降低PDF/latex输出的字体大小。这是可行的,但是当为多个平台创建输出时,它又增加了另一层复杂性。限制型心肌病文件…rmarkdown的一个关键好处是。

Specifications of my setup and system

我的设置和系统规格

  • OS X 10.10.2, MacBook Pro (Retina, 15-inch, Early 2013)
  • OS X 10.10.2, MacBook Pro (Retina, 15英寸,2013年初)
  • RStudio Version 0.99.329
  • 0.99.329 RStudio版本
  • R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet"
  • R版3.1.2(2014-10-31)——“南瓜头盔”
  • rmarkdown 0.5.1
  • rmarkdown 0.5.1
  • pandoc 1.13.1
  • pandoc 1.13.1

1 个解决方案

#1


2  

Have you tried this solution, which sets different output devices for different output formats? Here it is:

您尝试过为不同的输出格式设置不同的输出设备的这种解决方案吗?这里是:

Solution to producing pdf and html output from a unique Markdown file by setting specific options to knitr in the Makefile:

通过在Makefile中设置knitr的特定选项,从惟一的Markdown文件生成pdf和html输出的解决方案:

$(PDF): $(SRC) Makefile
Rscript \
  -e "library(knitr)" \
  -e "opts_chunk[['set']](dev = 'pdf')" \
  -e "pat_gfm()" \
  -e "knit('$<', 'temp.md')"
$(PANDOC) temp.md -o $@
rm temp.md

This answer also has interesting ideas.

这个答案也有有趣的想法。

Here it is:

这里是:

Try putting this code chunk at the beginning of the Rmd document.

尝试将此代码块放在Rmd文档的开头。

```{r setup, cache=FALSE, include=FALSE}
library(knitr)
output <- opts_knit$get("rmarkdown.pandoc.to")
if (output=="html") opts_chunk$set(fig.width=11, fig.height=11)
if (output=="docx") opts_chunk$set(fig.width=6,  fig.height=6)
```

One of the package options returned by opts_knit$get() is markdown.pandoc.to. This is evidently set to "html", "docx", or "latex" depending on the chosen output format (HTML, Word, or PDF). So you can test that and set the chunk options fig.width and fig.height accordingly.

opts_knit$get()返回的包选项之一是markdown.pandoc.to。显然,根据所选择的输出格式(html、Word或PDF),它被设置为“html”、“docx”或“latex”。因此,您可以测试它,并相应地设置块选项fig.width和fig.height。

#1


2  

Have you tried this solution, which sets different output devices for different output formats? Here it is:

您尝试过为不同的输出格式设置不同的输出设备的这种解决方案吗?这里是:

Solution to producing pdf and html output from a unique Markdown file by setting specific options to knitr in the Makefile:

通过在Makefile中设置knitr的特定选项,从惟一的Markdown文件生成pdf和html输出的解决方案:

$(PDF): $(SRC) Makefile
Rscript \
  -e "library(knitr)" \
  -e "opts_chunk[['set']](dev = 'pdf')" \
  -e "pat_gfm()" \
  -e "knit('$<', 'temp.md')"
$(PANDOC) temp.md -o $@
rm temp.md

This answer also has interesting ideas.

这个答案也有有趣的想法。

Here it is:

这里是:

Try putting this code chunk at the beginning of the Rmd document.

尝试将此代码块放在Rmd文档的开头。

```{r setup, cache=FALSE, include=FALSE}
library(knitr)
output <- opts_knit$get("rmarkdown.pandoc.to")
if (output=="html") opts_chunk$set(fig.width=11, fig.height=11)
if (output=="docx") opts_chunk$set(fig.width=6,  fig.height=6)
```

One of the package options returned by opts_knit$get() is markdown.pandoc.to. This is evidently set to "html", "docx", or "latex" depending on the chosen output format (HTML, Word, or PDF). So you can test that and set the chunk options fig.width and fig.height accordingly.

opts_knit$get()返回的包选项之一是markdown.pandoc.to。显然,根据所选择的输出格式(html、Word或PDF),它被设置为“html”、“docx”或“latex”。因此,您可以测试它,并相应地设置块选项fig.width和fig.height。