如何引用带有rmarkdown原生引文的数字标题?

时间:2017-12-17 23:16:03

标签: r latex r-markdown pandoc

在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

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

我怎么可能实现这个目标?

备注:

  • 我也试过\[@...或没有括号但是没有用。
  • 我还尝试了来自this answer的此\caption{Captiontextfoo \cite{[@hawking_thermodynamics_1983]}},但效果不佳,仅显示[?]
  • R版本3.4.3(2017-11-30)
  • 平台:x86_64-w64-mingw32 / x64(64位)
  • 在以下位置运行:Windows 7 x64(内部版本7601)Service Pack 1

2 个答案:

答案 0 :(得分:2)

此问题似乎已解决,可以直接在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)
```

enter image description here

如果这行不通(不能完全确定为什么不行,但有些人似乎对上述内容有疑问),则可以使用文本引用(请参见Section 2.2.4 of the bookdown book)。

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

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

答案 1 :(得分:0)

如果您只生成 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
  

有关完整的软件包文档,请参阅here。另外,请注意this关闭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
相关问题