给出以下可重现的示例:

---
title: "pdf output by user defined tex template"
output:
    pdf_document:
      template: default.latex
#bibliography: bib.bib
---

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

# About Pandoc metadata

https://pandoc.org/MANUAL.html#variables-for-latex

# Default tex templates

https://bookdown.org/yihui/rmarkdown-cookbook/latex-template.html

The default LaTeX template of Pandoc can be found at.

https://github.com/jgm/pandoc/tree/master/data/templates (named default.latex).

If you want to create your own template, you may want to start with this template.

# A reference to be cited in bibliography
here it is: @R-base

# References
  • 当我取消注释"bibliography"yaml元数据时,我得到以下错误:

好了!LaTeX错误:有些地方不对劲--可能是遗失了一件物品.

另一方面:

  • 当我注释掉"模板"yaml元数据,但不注释"参考书目"时,我得到了相关部分中参考文献的正确列表

我使用的模板(从开始)是PanDoc的默认LaTeX模板,可在以下位置找到: https://github.com/jgm/pandoc/blob/main/data/templates/default.latex

BIB文件仅包含以下条目:

@Manual{R-base,
  title = {R: A Language and Environment for Statistical
           Computing},
  author = {{R Core Team}},
  organization = {R Foundation for Statistical Computing},
  address = {Vienna, Austria},
  year = {2019},
  url = {https://www.R-project.org},
}

我的问题是:

在用户定义的"模板"的情况下,如何获得正确的引用列表(稍后我计划使用其他变量根据我的需要进行定制)?

我应该在模板文件中输入一些内容吗? 最终会怎样做呢?

如有任何帮助,非常感谢

推荐答案

您必须:

[ R Markdown Document ]
   |                |
   |--- [YAML Metadata]
   |       |--- [PDF Output]
   |            |--- [Custom Template: default.latex]
   |            |--- [Bibliography: bib.bib (Commented/Uncommented)]
   |
   |--- [Document Content]
         |--- [Citations]

您应该确保您的定制LaTeX模板(default.latex)包括处理书目所需的命令.

您提到的Pandoc default LaTeX template个例子展示了PandocTM如何处理LaTeX中的参考书目,它与传统的LaTeX使用有所不同.

在这个模板中,书目处理是有条件的,并与Pandocs的模板系统集成.它可容纳natbibbiblatex个书目管理程序包.

  • If natbib is used, the bibliography style is set with \bibliographystyle{}, and the bibliography files are included with \bibliography{}.
    Example:

    $if(natbib)$
    \usepackage[$natbiboptions$]{natbib}
    \bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
    $endif$
    
  • If biblatex is used, bibliography files are added with \addbibresource{} and printed with \printbibliography.
    Example:

    $if(biblatex)$
    \usepackage[$if(biblio-style)$style=$biblio-style$,$endif$$for(biblatexoptions)$$biblatexoptions$$sep$,$endfor$]{biblatex}
    $for(bibliography)$
    \addbibresource{$bibliography$}
    $endfor$
    $endif$
    

文献中包含的参考书目如下:

  • 对于natbib美元,它包括在\bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}中.
  • 对于biblatex,在相关条件块内为\printbibliography.

因此,请确保您的R Markdown文件的YAML头指定您正在使用的书目包(natbibbiblatex).Pandoc将使用这些信息来正确处理书目.

如果您需要进一步定制模板,请关注与参考书目相关的部分.您可以添加或修改特定于natbibbiblatex的条件语句或设置.


判断R标记文件中的YAML头文件:取消对参考书目行的注释,并确保指向.bib文件的路径正确.

---
title: "pdf output by user defined tex template"
output:
pdf_document:
    template: default.latex
bibliography: bib.bib
---

进行这些更改后,请编译R Markdown文档.如果使用书目命令正确设置了自定义模板,它应该会生成具有正确格式的引用的PDF.

如果遇到错误,请查看LaTeX日志(log)中的特定消息.确保.bib文件的格式正确,并且引文关键字与文档中使用的关键字匹配.

一旦您有了基本的参考书目,您就可以继续自定义模板.

R相关问答推荐

跨列应用多个摘要函数:summerise_all:列表对象无法强制为double类型'

如何在四进制仪表板值框中显示值(使用shiny 的服务器计算)

无法运行通过R中的Auto.arima获得的ARIMA模型

根据shiny 应用程序中的数字输入更改图标 colored颜色

R for循环返回到先前值

如何在R中合并两个基准点?

在R中使用download. file().奇怪的URL?

如何编辑gMarginal背景以匹配绘图背景?

仅在R中的数据集开始和结束时删除所有 Select 列的具有NA的行

R -如何分配夜间GPS数据(即跨越午夜的数据)相同的开始日期?

如何使用同比折线图中的个别日

我们如何在R中透视数据并在之后添加计算

如何筛选截止年份之前最后一个测量年度的所有观测值以及截止年份之后所有年份的所有观测值

有没有办法通过str_Detect()或其他字符串匹配函数来连接两个长度不等的数据帧?

R中的Desolve:返回的导数数错误

使用dqur在不同变量上创建具有多个条件的变量

是什么打破了此Quarto仪表板中的工具提示?

在R中添加要打印的垂直线

对数据帧中的列进行子集设置以通过迭代创建新的数据帧

Gggvenn为Venn增加了不存在的价值