如何在带有 rmarkdown 本地引用的图形标题中引用?

jay*_*.sf 4 latex r pandoc r-markdown

在 R Markdown 中,我想要一个带有 R Markdown native citation style 链接引用的图形标题[@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
Run Code Online (Sandbox Code Playgroud)

示例

这是我的代码:

---
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
Run Code Online (Sandbox Code Playgroud)

使用此输出:

在此处输入图片说明

我希望括号中的引文出现在图标题内,并带有参考书目的工作链接。参考书目应该像往常一样自动出现。

我怎么可能做到这一点?


笔记:

  • 我也试过\[@...或不带括号但没有用。
  • 我也\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

Mic*_*per 5

这个问题似乎是固定的,引用可以直接包含在fig.cap参数中:

这是一个最小示例,它bib.bib在与文件相同的目录中创建一个.Rmd文件。它还包括使用的图形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)
```
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

如果这不起作用(不完全确定为什么不起作用,但有些人似乎对上述问题有疑问),您可以使用文本引用(参见bookdown book 的第 2.2.4 节)。

(ref:caption) A plot [@R-rmarkdown]

```{r echo=FALSE, fig.cap="(ref:caption)"}
plot(cars)
```
Run Code Online (Sandbox Code Playgroud)


Kev*_*eau -1

如果您只是生成pdf输出,则可以latex直接使用。这是一个最小的可重现示例:

参考书目

@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/},
}
Run Code Online (Sandbox Code Playgroud)

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)
```
Run Code Online (Sandbox Code Playgroud)

生产:

在此输入图像描述

希望在不久的将来尘埃落定时rmarkdownpandoc下面的原始解决方案将再次适用于最新版本。

原来的

如果您使用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
Run Code Online (Sandbox Code Playgroud)

有关完整的包文档,请参阅此处。另外,请注意这个已关闭的 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
Run Code Online (Sandbox Code Playgroud)