使用rmarkdown本地引用如何在图题中引用?

6
在R Markdown中,我想要一个带有R Markdown本地引用样式[@ref]的图标题和链接引用。然而,当我将[@hawking_thermodynamics_1983]片段插入到标题末尾时,它只会抛出一个错误:
! Missing $ inserted.
<inserted text> 
                $
l.94 ...iontextfoo [@hawking_thermodynamics_1983]}

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

例子:

这是我的代码:

---
title: "Untitled"
author: "Author"
output: pdf_document
# bibliography: bibliography.bib
references:
- id: hawking_thermodynamics_1983
  author:
  - family: Hawking
    given: S. W.
  - family: Page
    given: Don. N.
  publisher: Communications in Mathematical Physics
  title: Thermodynamics of Black Holes in Anti-de Sitter Space.
  volume: 87
  type: article-journal
  issued:
    year: 1983
---

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

## R Markdown

\begin{figure}[h]
\centering
\includegraphics[width=13cm]{example.jpg}
\caption{Captiontextfoo}\label{fig1}
\end{figure}

[@hawking_thermodynamics_1983]

# Bibliography

以下是输出结果:

enter image description here

我希望括号内的引用出现在图标题中,并且有指向参考文献的链接。参考文献应像往常一样自动出现。

我该如何实现这一点?


注:

  • 我也尝试了 \[@... 或不使用方括号,但都没有起作用。
  • 我也尝试了此答案中的\caption{Captiontextfoo \cite{[@hawking_thermodynamics_1983]}},但效果也不好,只显示[?]
  • R版本 3.4.3(2017-11-30)
  • 平台:x86_64-w64-mingw32/x64(64位)
  • 运行环境:Windows 7 x64(build 7601)Service Pack 1

不确定r-markdown,但是在普通的pandoc中内置了pandoc-citeproc。你似乎使用那种语法... - mb21
3个回答

7
这个问题似乎已经被解决了,引用可以直接包含在fig.cap参数中:
以下是一个最简示例,它在与.Rmd文件相同的目录中创建了一个bib.bib文件,并使用includes_graphics包含了图形:
---
output: pdf_document
bibliography: bib.bib
---

```{r setup, include=FALSE}
knitr::write_bib(x = "rmarkdown", file = "bib.bib")
```

```{r, echo = FALSE, fig.cap = "A plot [@R-rmarkdown]"}
plot(cars)
```

enter image description here

如果上述方法不起作用(不确定为什么有些人会遇到问题),您可以使用文本引用(参见《bookdown》书的第2.2.4节)。
(ref:caption) A plot [@R-rmarkdown]

```{r echo=FALSE, fig.cap="(ref:caption)"}
plot(cars)
```

我仍然遇到这个问题,但是您提出的文本引用的想法起作用了。 - mspices

0
当我尝试Michael Harper的示例时,文献参考引用正常。但是,当我将plot更改为knitr::include_graphics('myfig.jpg')时,它就无法工作,并且标题只打印了...[@R-markdown]
然后,我同时使用了plot(...)knitr::include_graphics('myfig.jpg'),当我将其编织成.pdf时,plot(...)图的标题正确,而knitr::include_graphics('myfig.jpg')的相同标题却出现了...[@R-markdown]
因此,使用knitr::include_graphics('myfig.jpg')加载外部图像似乎会破坏latex/pdf输出中的文献参考引用。对于.html或.docx文件,这是可以的 - 引用是正确的。
---
output: pdf_document
bibliography: bib.bib
---
  
```{r setup, include=FALSE}
knitr::write_bib(x = "rmarkdown", file = "bib.bib")
```   
```{r, echo=F, fig.cap="Soil profile [@R-rmarkdown]"}
plot(cars)
knitr::include_graphics('SoilProfile-Haven-1.jpg',dpi=400)
```

enter image description here


-1

如果你只需要生成 pdf 文件,你可以直接使用 latex。这里是一个最小可复现的示例:

biblio.bib

@Book{xie2015,
  title = {Dynamic Documents with {R} and knitr},
  author = {Yihui Xie},
  publisher = {Chapman and Hall/CRC},
  address = {Boca Raton, Florida},
  year = {2015},
  edition = {2nd},
  note = {ISBN 978-1498716963},
  url = {http://yihui.name/knitr/},
}

rmarkdown.Rmd

---
output: 
  bookdown::pdf_document2:
    citation_package: natbib
    toc: false
bibliography: biblio.bib
biblio-style: apalike
link-citations: yes
---

```{r nice-fig, fig.cap='Here is a nice figure! \\citep{xie2015}', out.width='80%', fig.asp=.75, fig.align='center', echo=FALSE}
par(mar = c(4, 4, .1, .1))
plot(pressure, type = 'b', pch = 19)
```

生成:

enter image description here

希望不久的将来,当 rmarkdown / pandoc 的尘埃落定之后,原始解决方案将再次与最新版本兼容。

原文

如果您使用 bookdown::pdf_document2 输出格式,则具有增强的交叉引用功能。将您的图放在代码块中也可以更轻松地以多个输出格式输出为 html 格式。

---
title: "Untitled"
author: "Author"
output: bookdown::pdf_document2
# bibliography: bibliography.bib
references:
- id: hawking_thermodynamics_1983
  author:
  - family: Hawking
    given: S. W.
  - family: Page
    given: Don. N.
  publisher: Communications in Mathematical Physics
  title: Thermodynamics of Black Holes in Anti-de Sitter Space.
  volume: 87
  type: article-journal
  issued:
    year: 1983
---

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

# R Markdown

```{r fig1, out.width='13cm', fig.cap='Captiontextfoo [@hawking_thermodynamics_1983]', echo=FALSE}
knitr::include_graphics("example.jpg")
```

# Bibliography

完整的软件包文档,请参见此处。另外,请注意这个已关闭的Github问题,展示了您所请求的输出。

会话信息

sessionInfo()

# R version 3.4.3 (2017-11-30)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows >= 8 x64 (build 9200)
# 
# Matrix products: default
# 
# locale:
# [1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252
# [3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
# [5] LC_TIME=English_Australia.1252
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base
# 
# loaded via a namespace (and not attached):
#  [1] compiler_3.4.3  backports_1.1.2 bookdown_0.5    magrittr_1.5    rprojroot_1.3-1
#  [6] htmltools_0.3.6 tools_3.4.3     yaml_2.1.16     Rcpp_0.12.14    stringi_1.1.6
# [11] rmarkdown_1.8   knitr_1.17      stringr_1.2.0   digest_0.6.13   evaluate_0.10.1

有潜力,但出现了“收集的错误摘要(可能重复其他消息):pdflatex:'pdflatex'命令返回代码1,请参阅'test71.log'以获取详细信息Latexmk:使用-f选项强制完成处理,除非错误超过了latex / pdflatex的最大运行次数。!缺少$插入。<inserted text> $l.111 ...ontextfoo [@hawking_thermodynamics_1983]} \label{fig:fig1}与您的代码一起,我将我的MiKTeX更新到2.9.6500,并从github安装了gromdevtools :: install_github('rstudio / bookdown')`。 - jay.sf
@jaySf,我无法复制您的错误,直到我使用了一个新的环境,似乎您可能被困在最新版本中受到pandoc 2发布影响的rmarkdownpandoc错误中。在这些问题得到解决之前,您最好不要使用开发版本,尽管这篇博客文章表明他们已经至少部分解决了这个问题。 - Kevin Arseneau
抱歉,之前的表述可能不够清晰。我的意思是我查看了你回答中的Github链接。在你提供的代码无法与bookdown标准版本一起使用后,我尝试了dev版本,但没有改变。我也使用旧版的pandoc 1.19.2.1和rmarkdown 1.8。现在我已经回滚到了bookdown 0.5,但你提供的代码似乎仍然无法工作。我们环境中可能存在什么差异? - jay.sf
@jaySf,我无法在新的会话中复制您的问题,我的sessionInfo已添加到我的答案中。 - Kevin Arseneau
我比较了两个会话信息,相应地更新了一些包,但仍然无法工作。我在我的问题中添加了错误消息。您使用哪个pandoc版本?在我的机器上,rmarkdown :: pandoc_version()产生1.19.2.1 - jay.sf
显示剩余2条评论

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