Search code examples
r-markdownknitrquarto

Rmarkdown code chunk format for BibTeX entry


I have a quarto markdown document (test.qmd). It contains a toggle button named BibTeX which expands into a specified $\BibTeX$ entry when expanded.

The test.qmd file reprex is as follows:

---
title: "test"
format: html
---

<!-- BibTeX -->
<button class="btn btn-info" type="button" data-bs-toggle="collapse" data-bs-target="#test2022paper" aria-expanded="false" aria-controls="test2022paper">
  BibTeX
</button>
<div class="collapse" id="test2022paper">
  <br>
  <div class="card card-body">
```{r, eval=FALSE}
@misc{test2022paper,
    title        = {
        A test' paper
    },
    author       = {Test authora, Test authorb},
    year         = 2022,
    eprint       = {arXiv:test}
}
```
  </div>
</div>

The issue is that the BibTeX entry is formatted as an R code chunk, which causes strange highlighting since the test' apostrophe is interpreted as the start of an R string, not a literal BibTeX entry text. See the image below, for the knitted output:

sample output from knitted qmd

Is there a way to display this BibTeX entry code chunk using bib file syntax highlighting?


Solution

  • Option 1

    You can simply use bibtex as code chunk header to get BibTeX syntax highlighting.

    Also since bibtex is the first code chunk in this MWE, you may want to specify knitr as engine, otherwise Quarto will try to render this document using jupyter.

    ---
    title: "test"
    format: html
    engine: knitr
    ---
    
    <!-- BibTeX -->
    <button class="btn btn-info" type="button" data-bs-toggle="collapse" data-bs-target="#test2022paper" aria-expanded="false" aria-controls="test2022paper">
      BibTeX
    </button>
    <div class="collapse" id="test2022paper">
      <br>
      <div class="card card-body">
      
    ```{bibtex}
    
    @misc{test2022paper,
        title        = {
            A test' paper
        },
        author       = {Test authora, Test authorb},
        year         = 2022,
        eprint       = {arXiv:test}
    }
    ```
      </div>
    </div>
    

    Option 2

    Alternatively, if for some reason you want to use r-code chunk, but still want to get bibtex syntax highlighting, a possible way would be writing that bib as output and specifying sourceCode bibtex as output class.

    ---
    title: "test"
    format: html
    ---
    
    <!-- BibTeX -->
    <button class="btn btn-info" type="button" data-bs-toggle="collapse" data-bs-target="#test2022paper" aria-expanded="false" aria-controls="test2022paper">
      BibTeX
    </button>
    <div class="collapse" id="test2022paper">
      <br>
      <div class="card card-body">
      
    ```{r}
    #| echo: false
    #| class-output: "sourceCode bibtex"
    
    bib <- 
    "@misc{test2022paper,
        title        = {
            A test' paper
        },
        author       = {Test authora, Test authorb},
        year         = 2022,
        eprint       = {arXiv:test}
    }"
    
    cat(bib, sep = "\n")
    ```
      </div>
    </div>
    

    bibtex syntax highlighting