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

huangapple go评论67阅读模式
英文:

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: &quot;Introduction&quot;
    author: &quot;&quot;
    date: &#39;`r Sys.Date()`&#39;
    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=&quot;normal-code&quot;,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&#39;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=&quot;shaded&quot;`

*Note: `Shaded` is used by rmarkdown for code blocks and if you use `class.output` chunk option, `Shaded` used for code output too.*


~~~
---
title: &quot;Introduction&quot;
author: &quot;&quot;
date: &#39;`r Sys.Date()`&#39;
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 &lt;- c(1,2,3,4,5)
b &lt;- c(1,2,3,4,5)
df &lt;- data.frame(a, b)

# take a look at our data frame
df

&lt;hr&gt;

[![bg color for code blocks and output][1]][1]

&lt;hr&gt;

## 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 &lt;- knit_hooks$get(&#39;source&#39;)
default_output_hook &lt;- knit_hooks$get(&#39;output&#39;)

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

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

knitr::opts_chunk$set(echo = TRUE)
a &lt;- c(1,2,3,4,5)
b &lt;- c(1,2,3,4,5)
df &lt;- data.frame(a, b)

# take a look at our data frame
df

&lt;hr&gt;

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



huangapple
  • 本文由 发表于 2023年4月17日 20:17:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定