Rmarkdown Beamer 中的代码块和输出背景颜色和边框颜色

问题描述 投票:0回答:1

我正在尝试突出显示(背景颜色和边框)R Markdown 中的代码块,输出格式为 beamer presentation。我使用大都市主题。示例代码如下

---
title: "Introduction"
author: ""
date: '`r Sys.Date()`'
output:
  beamer_presentation:
    keep_tex: yes
    theme: metropolis
    latex_engine: xelatex
    slide_level: 2
    incremental: no
fontsize: 12pt
classoption: compress
header-includes:
  \setbeamercolor{frametitle}{bg=darkgray}
    \hypersetup{colorlinks,citecolor=orange,filecolor=red,linkcolor=brown,urlcolor=blue}
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{css, echo=FALSE}
.normal-code {
  background-color: darkgray;
  border: 3px solid red;
  font-weight: bold;
}
```
## First Slide

```{r class.source="normal-code",warning=FALSE,message=FALSE,eval=TRUE,echo=TRUE}
# Load R package
library(tidyverse)
```

目前,代码周围既没有背景颜色的变化也没有边框。

r latex r-markdown beamer
1个回答
2
投票

CSS 不会对投影仪输出产生任何影响。您需要面向乳胶的解决方案。

我提出了两个解决方案;第一个更简单,而第二个变得有点复杂(但看起来更好且可定制)。

方法一

第一个真的很简单。只需用您喜欢使用的任何颜色重新定义

shadecolor
\definecolor{shadecolor}{RGB}{225, 225, 225}
改变
Shaded
environemnt 的 bg 颜色并使用 chunk opiton
class.output="shaded"

注意:

Shaded
被 rmarkdown 用于代码块,如果您使用
class.output
块选项,
Shaded
也用于代码输出。

---
title: "Introduction"
author: ""
date: '`r Sys.Date()`'
output:
  beamer_presentation:
    keep_tex: yes
    theme: metropolis
    latex_engine: xelatex
    slide_level: 2
    incremental: no
fontsize: 12pt
classoption: compress
header-includes:
  - \setbeamercolor{frametitle}{bg=darkgray}
  - \hypersetup{colorlinks,citecolor=orange,filecolor=red,linkcolor=brown,urlcolor=blue}
---

## First Slide


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

\definecolor{shadecolor}{RGB}{225, 225, 225}

```{r class.output="shaded"}
a <- c(1,2,3,4,5)
b <- c(1,2,3,4,5)
df <- data.frame(a, b)

# take a look at our data frame
df
```


方法二

现在要进行更多定制,我们可以尝试使用

tcolorbox
乳胶包。所以在第二种方法中,我做了以下事情,

  • 首先,使用

    \newtcolorbox
    定义了一个彩色框以及 bg 颜色和边框颜色的选项(有关详细信息,请参阅包文档)

  • 然后,修改了source and output knitr hooks,使源代码和输出包裹在先前定义的颜色框中。

---
title: "Introduction"
author: ""
date: '`r Sys.Date()`'
output:
  beamer_presentation:
    keep_tex: yes
    theme: metropolis
    latex_engine: xelatex
    slide_level: 2
    incremental: no
fontsize: 12pt
classoption: compress
header-includes:
  - \setbeamercolor{frametitle}{bg=darkgray}
  - \hypersetup{colorlinks,citecolor=orange,filecolor=red,linkcolor=brown,urlcolor=blue}
  - \usepackage{tcolorbox}
---

## First Slide


```{=latex}

\definecolor{shadecolor}{RGB}{249, 247, 223}

\newtcolorbox{codebox}{
    colback=shadecolor,
    colframe=orange,
    boxsep=2pt,
    arc=2pt}
```


```{r setup, include=FALSE}
library(knitr)

default_source_hook <- knit_hooks$get('source')
default_output_hook <- knit_hooks$get('output')

knit_hooks$set(
  source = function(x, options) {
    paste0(
      "\n::: {.codebox data-latex=\"\"}\n\n",
      default_source_hook(x, options),
      "\n\n:::\n\n")
  }
)

knit_hooks$set(
  output = function(x, options) {
    paste0(
      "\n::: {.codebox data-latex=\"\"}\n\n",
      default_output_hook(x, options),
      "\n\n:::\n\n")
  }
)

knitr::opts_chunk$set(echo = TRUE)
```


```{r}
a <- c(1,2,3,4,5)
b <- c(1,2,3,4,5)
df <- data.frame(a, b)

# take a look at our data frame
df
```


© www.soinside.com 2019 - 2024. All rights reserved.