突出显示RMarkdown文档中的一些引用?

lyr7nygr  于 2022-12-30  发布在  其他
关注(0)|答案(2)|浏览(124)

papaja.rmd文档(其中的引用取自bib文件并使用apa7.csl文件)中,是否可以强调(例如,用粗体显示)某些包含特定字符串(例如,特定作者的姓名)的引用?

slhcrj9b

slhcrj9b1#

我可以提出这个基于pandoc lua filter的解决方案,它不仅适用于pdf,也适用于html输出,而且不需要手动编辑latex或html文件。

---
title: "The title"
bibliography: "r-references.bib"
output: 
  pdf_document: 
    pandoc_args: [ "--lua-filter", "ref-bold.lua"]
  html_document: 
    pandoc_args: [ "--lua-filter", "ref-bold.lua"]
---

```{r setup, include = FALSE}
library("papaja")
r_refs("r-references.bib")

We used R [@R-base] and Tidyverse [@R-tidyverse] for all our analyses. Especially [@R-tidyverse] made things easy.

\vspace{10mm}

References


**参考-粗体.lua**

function Cite(el)
if pandoc.utils.stringify(el.content) == "[@R-tidyverse]" then
return (pandoc.Strong(el))
end
end


此演示将粗体显示`tidyverse`包的所有引用,如果我们希望粗体显示base-R的引用,我们可以将`ref-bold.lua`中的第二行修改为`pandoc.utils.stringify(el.content) == "[@R-base]"`,并且所有引用`base-R`的示例都将粗体显示(突出显示)。

**pdf输出**

![](https://i.stack.imgur.com/mvid3.png)

**html输出**

![](https://i.stack.imgur.com/rQlQg.png)
efzxgjgh

efzxgjgh2#

发布一个解决方案,以防它对其他人也有用。我们可以首先从RMarkdown文档渲染LaTeX文件,然后找到并替换所有需要强调的名称,最后从修改后的LaTeX文件生成pdf。

# knitting the original Rmd file (with "keep_tex: true" in YAML)
rmarkdown::render(input = "some_file.Rmd")

# reading the generated LaTeX file
tex_file <- readLines(con = "some_file.tex")

# putting a particular author in bold in the references list
new_tex_file  <- gsub(pattern = "James, W.", replace = "\\textbf{James, W.}", x = tex_file, fixed = TRUE)

# writing the (updated) LaTeX file
writeLines(text = new_tex_file, con = "some_file.tex")

# generating the pdf file (may need to be ran twice)
system(command = "xelatex some_file.tex")

相关问题