使用Beamer类在Rmarkdown中居中绘图的问题

3

我在使用Rmd文档输出类型为“beamer演示文稿”时,无法将图表居中。但是当输出类型设置为“pdf_document”时,相同的代码可以正常工作。在渲染文档时,出现以下错误:

! Missing $ inserted.
<inserted text> 
                $
l.74 \end{frame}

pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43

奇怪的是,当我使用命令render("slideshow.Rmd",output_file = "Demo slideshow.pdf")进行编织时,我会得到这个错误,而在Rstudio GUI中按下“Knit”时,我会看到一个PDF文件,其中的图表仍然居中显示。

编辑:事后看来,这可能不是真的。通过控制台语句进行呈现时,输出在控制台的“控制台”选项卡中打印。通过“Knit”按钮进行呈现时,输出将被打印到控制台的“R markdown”选项卡中,并在完成后自动返回到“控制台”选项卡,在那里仍然显示先前呈现语句的错误。 enter image description here

我已经尝试过。
```{r, results = 'asis', echo = FALSE, fig.width=5, warning=FALSE, message=FALSE}

如果不出现错误,则绘图将左对齐。将此代码块包装在\begin{center}\end{center}中会再次导致相同的错误。

非常感谢您的帮助,感谢任何建议。


slideshow.Rmd

---
title: Oh man
subtitle: this issue is
author: really bugging me
fontsize: 10pt
output: beamer_presentation

---

```{r setup, include=FALSE, warning=FALSE, message=FALSE}
knitr::opts_chunk$set(echo = T)

df=data.frame(x=c(1,2,3),y=c(1,2,3))

```


### This is a title
And this is some text 

```{r, results = 'asis', echo = FALSE, out.width='80%', warning=FALSE, message=FALSE, fig.align='center'}

plot(df$x,df$y)

```
1个回答

0

供将来参考,如果其他人遇到这个问题,我认为我已经解决了它。我这样设置'keep_tex=TRUE:

output:
  beamer_presentation:
    keep_tex: true

我在 tex 文件中的第 74 行附近进行了调查(因为该行在错误 l.74 \end{frame} 中被提到)。在那之前的两行是:

\begin{center}\includegraphics[width=0.8\linewidth]
  {--path--/Demo slideshow_files/figure-beamer/unnamed-chunk-1-1} \end{center}

这里的 --path-- 是缩写。正如我在生成的文件名中指定的那样,Demo 和 slideshow 之间有一个空格。这似乎导致了错误。

render("slideshow.Rmd",output_file = "Demo_slideshow.pdf")

运行良好。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接