RMarkdown: Customize Color and Font Size of (automated) Citations

Viewed 44

I am fairly new to RMarkdown and trying to apply some LaTeX logic here. Currently, I am creating a PDF presentation with RMarkdown (beamer_presentation). Is it possible to change the font size and color of automatic citations ([@shortname]) so that e.g., all citations appear slightly smaller than the normal text and in gray? In LaTeX beamer presentations, I managed to re-define the cite command and apply this. I was not yet able to recreate this in RMarkdown however.

When I was trying to manually (I'd much prefer automatic a solution) change the font color of citations \textcolor{gray}{[e.g., @shortname]} (also $\textcolor{gray}{[e.g., @shortname]}$), I received this error message:

I was unable to find any missing LaTeX packages from the error log slides.log. ! Missing $ inserted. $ l.182 \end{frame} Try to find the following text in slides.Rmd: \end{frame} You may need to add $ $ around a certain inline R expression r in slides.Rmd (see the above hint). See https://github.com/rstudio/rmarkdown/issues/385 for more info.

The link in the error message did not help me either.

I am grateful for your help! I'll include a MWE below.

Till

---
title: "A title"
author: "Till"
date: 25.07.2022
output:
  beamer_presentation:
    incremental: true # bullet points on multiple pages
    toc: false # add generated page listing sections
    slide_level: 2 # depth level for render content
    latex_engine: lualatex 
    keep_tex: TRUE
bibliography: literature.bib
csl: american-sociological-association.csl
header-includes:  \usepackage{xcolor} 
---

```{r setup, include=FALSE} 
knitr::opts_chunk$set(echo = TRUE)
options(tinytex.verbose = TRUE) #  for debugging
```

# First Section

## Example Slide

- Some text which will be followed by a citation \textcolor{gray}{[e.g., @shortname]}
- Some more text, now without a citation 

The literature.bib file could look somewhat like this:

@article{shortname,
    title = {citation title},
    journaltitle = {Journal},
    shortjournal = {Journal},
    author = {Lastname, Firstname},
    date = {2022},
}
1 Answers

The problem with your \textcolor approach is that you can't use markdown syntax in the argument of a latex macro. You can avoid this problem by using \begingroup \color{gray} [e.g., @shortname] \endgroup:

---
title: "A title"
author: "Till"
date: 25.07.2022
output:
  beamer_presentation:
    incremental: true # bullet points on multiple pages
    toc: false # add generated page listing sections
    slide_level: 2 # depth level for render content
    latex_engine: lualatex 
    keep_tex: TRUE
bibliography: literature.bib
csl: american-sociological-association.csl
header-includes:  \usepackage{xcolor} 
---

#```{r setup, include=FALSE} 
# I did not know how to include R-Chunks in the code block withouth breaking the formatting, so just remove the Hashtags before the three ticks and this endless comment
knitr::opts_chunk$set(echo = TRUE)
options(tinytex.verbose = TRUE) #  for debugging
#```

# First Section

## Example Slide

- Some text which will be followed by a citation \begingroup \tiny\color{gray} [e.g., @shortname] \endgroup
- Some more text, now without a citation 

enter image description here

Related