簡體   English   中英

突出顯示 RMarkdown 文檔中的一些參考資料?

[英]Highlighting some references in RMarkdown documents?

是否有可能在papaja .Rmd 文檔中強調(例如,用粗體顯示)一些包含特定字符串(例如,特定作者的名字)的引用(其中引用來自 bib 文件並使用 apa7. .csl 文件)?

我可以基於pandoc lua 過濾器提出此解決方案,它不僅適用於 pdf,而且適用於 html output,並且不需要手動編輯 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

ref-bold.lua

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

該演示將所有對tidyverse package 的引用加粗,如果我們想將對 base-R 的引用加粗,我們將ref-bold.lua中的第二行修改為pandoc.utils.stringify(el.content) == "[@R-base]"並且所有對base-R的引用實例都將以粗體顯示(突出顯示)。

pdf output


pdf輸出


html output


輸出


發布一個解決方案,以防它對其他人也有用。 我們可以先從 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")

Lua-Filter 將是最優雅的解決方案。 如果您使用 BibLaTeX 和 biber,您可以使用通用注釋功能(請參閱此 SO 答案)。

在序言中包含以下內容:

\renewcommand*{\mkbibnamegiven}[1]{%
  \ifitemannotation{bold}
    {\textbf{#1}}
    {#1}}

\renewcommand*{\mkbibnamefamily}[1]{%
  \ifitemannotation{bold}
    {\textbf{#1}}
    {#1}}

在您的 bib 文件中,使用Author+an字段來定義要突出顯示的作者:

@Misc{pawel2022power,
  title = {Power Priors for Replication Studies},
  author = {S Pawel and F Aust and L Held and E J Wagenmakers},
  year = {2022},
  eprinttype = {arxiv},
  eprint = {2207.14720},
  url = {https://arxiv.org/abs/2207.14720},
  Author+an = {2=bold}
}

現在,使用 XeLaTeX 渲染 R Markdown 文件,保留中間文件和 TeX 文件,並使用 biber 再次渲染 TeX 文件:

rmarkdown::render("paper.Rmd", clean = FALSE)
tinytex::xelatex("academic.tex", bib_engine = "biber")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM