【问题标题】:knitr not aligning figures to center in pdf output?knitr 没有将数字对齐到 pdf 输出的中心?
【发布时间】:2016-04-09 15:51:56
【问题描述】:

使用 knitr 制作 pdf,使用 fig.align='center' 选项时,数字不显示:

require(knitr)
opts_chunk$set(fig.align='center') 

或

```{r chunkname, fig.align='center'}
...code that makes figure...
```

无论哪种方式,按下 knit PDF 按钮时,pdf 上都不会出现任何数字。但是我删除了 fig.align 选项,然后数字出现了,左对齐。

这是一个老问题,应该用 1.8 knitr 版本解决,但仍然有同样的问题:(

有什么想法吗?

谢谢!

【问题讨论】:

  • 你能提供一个reproducible example吗?请注意,全局选项仅适用于设置它们的块 之后 (AFAIK)。

标签: r latex rstudio knitr pdflatex


【解决方案1】:

您需要为此设置全局选项。 我将此标题作为我的文档的标准,并根据需要进行更改:

### Set global and markdown global options

```{r global.options, include = TRUE}
knitr::opts_chunk$set(
    cache       = TRUE,     # if TRUE knitr will cache the results to reuse in future knits
    fig.width   = 10,       # the width for plots created by code chunk
    fig.height  = 10,       # the height for plots created by code chunk
    fig.align   = 'center', # how to align graphics in the final doc. 'left', 'right', 'center'
    fig.path    = 'figs/',  # file path to the directory where knitr shall store the graphics files
    results     = 'asis',   # knitr will pass through results without reformatting them
    echo        = TRUE,     # in FALSE knitr will not display code in the code chunk above it's results
    message     = TRUE,     # if FALSE knitr will not display any messages generated by code
    strip.white = TRUE,     # if FALSE knitr will not remove white spaces at the beg or end of code chunk
    warning     = FALSE)    # if FALSE knitr will not display any warning messages in the final document
```

另外,请检查您是否在文档开头设置了输出类型:

---
title:  "blah-blah"
author: "Denis Rasulev"
date:   "November 2015"
output: pdf_document
---

【讨论】:

  • knitr::opts_chunk$set() 并不比opts_chunk$set()“更全球化”。
【解决方案2】:

问题解决了! 这是一个非常“简单”的问题,可能是由于 laTex 规则(我不能在 laTex 中编程,所以我不确定)。 问题出在块标识符中。我放了一些由多个单词组成的标识符,一个单词和另一个单词之间有空格:

'''{r #identifier one}   #before
code
'''

'''{r #identifier-one}   #after
code
'''

在单词之间打勾解决了允许 Markdown 将文件呈现为 pdflatex 的问题。

还是非常感谢你!

【讨论】:

    猜你喜欢
    • 2014-10-25
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    相关资源
    最近更新 更多