英文:
Code block and output background color and border color in Rmarkdown Beamer
问题
我正在尝试在R Markdown中为beamer演示文稿输出格式中的代码块添加高亮(背景颜色和边框)。我使用的是metropolis主题。示例代码如下:
---
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)
.normal-code {
background-color: darkgray;
border: 3px solid red;
font-weight: bold;
}
第一张幻灯片
# Load R package
library(tidyverse)
目前,代码周围既没有背景颜色的更改,也没有边框。
<details>
<summary>英文:</summary>
I am trying to highlight (background colors and border) a code block in R Markdown with the output format being beamer presentation. I use the metropolis theme. The sample code is as follows
---
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)
```
Currently, there is neither a change of background color nor a border around the code.
</details>
# 答案1
**得分**: 3
CSS在beamer输出上不会产生任何效果。您需要使用与LaTeX相关的解决方案。
我提出了两种解决方案;第一种更简单,而第二种则更复杂(但看起来更好且可自定义)。
## 第一种方法
第一种方法非常简单。只需使用`\definecolor{shadecolor}{RGB}{225, 225, 225}`重新定义`shadecolor`,将其更改为您喜欢的任何颜色,这将更改`Shaded`环境的背景颜色,并使用块选项`class.output="shaded"`。
*注意:`Shaded`被rmarkdown用于代码块,如果您使用`class.output`块选项,`Shaded`也用于代码输出。*
<details>
<summary>英文:</summary>
CSS won't have any effect on beamer output. You need latex-oriented solutions.
I have proposed two solutions; the first one is simpler whereas the second one gets a bit complicated (but looks better and customizable).
## Approach One
First one is really simple. Just redefine the `shadecolor` with whatever color you like using, `\definecolor{shadecolor}{RGB}{225, 225, 225}` which changes the bg color of `Shaded` environemnt and use chunk opiton `class.output="shaded"`
*Note: `Shaded` is used by rmarkdown for code blocks and if you use `class.output` chunk option, `Shaded` used for code output too.*
~~~
---
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}
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
<hr>
[![bg color for code blocks and output][1]][1]
<hr>
## Approach Two
Now for more customization, we may try with [`tcolorbox`](https://www.ctan.org/pkg/tcolorbox) latex package. So in this second approach, I have done the followings,
- Firstly, defined a colored box using `\newtcolorbox` along with options for bg color and border color (Refer to package docs for the details)
- And then secondly, modified the [source and output knitr hooks](https://yihui.org/knitr/hooks/#output-hooks), so that source code and output is wrapped within the previously defined color box.
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
\definecolor{shadecolor}{RGB}{249, 247, 223}
\newtcolorbox{codebox}{
colback=shadecolor,
colframe=orange,
boxsep=2pt,
arc=2pt}
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)
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
<hr>
[![customized code block and output block in beamer][2]][2]
[1]: https://i.stack.imgur.com/6yGTr.png
[2]: https://i.stack.imgur.com/G0sVn.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论