0

Is it possible to emphasise (e.g., put in bold) some references that contain a particular string (e.g., the name of a particular author) in a papaja .Rmd document (where the refs are taken from a bib file and using the apa7.csl file)?

Ladislas Nalborczyk
  • 725
  • 2
  • 5
  • 20

3 Answers3

2

I can propose this solution based on pandoc lua filter which would work for not just pdf but also html output and doesn't require manual editing of latex or html file.

---
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

This demo bolds all of the reference to tidyverse package, if we would wanted to bold the reference to base-R, we would modify the second line in ref-bold.lua as pandoc.utils.stringify(el.content) == "[@R-base]" and all instances of references to base-R would be bold (highlighted).

pdf output


pdf output


html output


html output


shafee
  • 15,566
  • 3
  • 19
  • 47
  • That's great, thank you! In this example, only in-text citations are put in bold (but not references in the references list). Is there a way to only put the references in the references list in bold (but not the in-text citations)? – Ladislas Nalborczyk Dec 30 '22 at 10:05
  • Though I have not tested it myself but you may [try this](https://tex.stackexchange.com/questions/53214/elegant-way-to-make-entire-citation-bold?noredirect=1&lq=1) – shafee Dec 30 '22 at 10:42
1

A Lua-Filter would be the most elegant solution. If you use BibLaTeX and biber, you can use the general annotation feature (see this SO answer).

Include the following in your preamble:

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

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

In your bib-file, use the Author+an field to define which author to highlight:

@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}
}

Now, render the R Markdown file with XeLaTeX, keep the intermediate files and the TeX file, and render the TeX file again using biber:

rmarkdown::render("paper.Rmd", clean = FALSE)
tinytex::xelatex("academic.tex", bib_engine = "biber")
crsh
  • 1,699
  • 16
  • 33
0

Posting a solution in case it can be useful to others as well. We can first render the LaTeX file from the RMarkdown document, then find and replace all instances of the name to be emphasised, and finally generate the pdf from the modified LaTeX file.

# 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")
Ladislas Nalborczyk
  • 725
  • 2
  • 5
  • 20