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

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

Code block and output background color and border color in Rmarkdown Beamer

问题

我正在尝试在R Markdown中为beamer演示文稿输出格式中的代码块添加高亮(背景颜色和边框)。我使用的是metropolis主题。示例代码如下:

  1. ---
  2. title: "Introduction"
  3. author: ""
  4. date: '`r Sys.Date()`'
  5. output:
  6. beamer_presentation:
  7. keep_tex: yes
  8. theme: metropolis
  9. latex_engine: xelatex
  10. slide_level: 2
  11. incremental: no
  12. fontsize: 12pt
  13. classoption: compress
  14. header-includes:
  15. \setbeamercolor{frametitle}{bg=darkgray}
  16. \hypersetup{colorlinks,citecolor=orange,filecolor=red,linkcolor=brown,urlcolor=blue}
  17. ---
  18. ```{r setup, include=FALSE}
  19. knitr::opts_chunk$set(echo = TRUE)
  1. .normal-code {
  2. background-color: darkgray;
  3. border: 3px solid red;
  4. font-weight: bold;
  5. }

第一张幻灯片

  1. # Load R package
  2. library(tidyverse)
  1. 目前,代码周围既没有背景颜色的更改,也没有边框。
  2. <details>
  3. <summary>英文:</summary>
  4. 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
  5. ---
  6. title: &quot;Introduction&quot;
  7. author: &quot;&quot;
  8. date: &#39;`r Sys.Date()`&#39;
  9. output:
  10. beamer_presentation:
  11. keep_tex: yes
  12. theme: metropolis
  13. latex_engine: xelatex
  14. slide_level: 2
  15. incremental: no
  16. fontsize: 12pt
  17. classoption: compress
  18. header-includes:
  19. \setbeamercolor{frametitle}{bg=darkgray}
  20. \hypersetup{colorlinks,citecolor=orange,filecolor=red,linkcolor=brown,urlcolor=blue}
  21. ---
  22. ```{r setup, include=FALSE}
  23. knitr::opts_chunk$set(echo = TRUE)
  24. ```
  25. ```{css, echo=FALSE}
  26. .normal-code {
  27. background-color: darkgray;
  28. border: 3px solid red;
  29. font-weight: bold;
  30. }
  31. ```
  32. ## First Slide
  33. ```{r class.source=&quot;normal-code&quot;,warning=FALSE,message=FALSE,eval=TRUE,echo=TRUE}
  34. # Load R package
  35. library(tidyverse)
  36. ```
  37. Currently, there is neither a change of background color nor a border around the code.
  38. </details>
  39. # 答案1
  40. **得分**: 3
  41. CSSbeamer输出上不会产生任何效果。您需要使用与LaTeX相关的解决方案。
  42. 我提出了两种解决方案;第一种更简单,而第二种则更复杂(但看起来更好且可自定义)。
  43. ## 第一种方法
  44. 第一种方法非常简单。只需使用`\definecolor{shadecolor}{RGB}{225, 225, 225}`重新定义`shadecolor`,将其更改为您喜欢的任何颜色,这将更改`Shaded`环境的背景颜色,并使用块选项`class.output="shaded"`
  45. *注意:`Shaded`rmarkdown用于代码块,如果您使用`class.output`块选项,`Shaded`也用于代码输出。*
  46. <details>
  47. <summary>英文:</summary>
  48. CSS won&#39;t have any effect on beamer output. You need latex-oriented solutions.
  49. I have proposed two solutions; the first one is simpler whereas the second one gets a bit complicated (but looks better and customizable).
  50. ## Approach One
  51. 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;`
  52. *Note: `Shaded` is used by rmarkdown for code blocks and if you use `class.output` chunk option, `Shaded` used for code output too.*
  53. ~~~
  54. ---
  55. title: &quot;Introduction&quot;
  56. author: &quot;&quot;
  57. date: &#39;`r Sys.Date()`&#39;
  58. output:
  59. beamer_presentation:
  60. keep_tex: yes
  61. theme: metropolis
  62. latex_engine: xelatex
  63. slide_level: 2
  64. incremental: no
  65. fontsize: 12pt
  66. classoption: compress
  67. header-includes:
  68. - \setbeamercolor{frametitle}{bg=darkgray}
  69. - \hypersetup{colorlinks,citecolor=orange,filecolor=red,linkcolor=brown,urlcolor=blue}
  70. ---
  71. ## First Slide
  72. ```{r setup, include=FALSE}
  73. knitr::opts_chunk$set(echo = TRUE)

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

  1. a &lt;- c(1,2,3,4,5)
  2. b &lt;- c(1,2,3,4,5)
  3. df &lt;- data.frame(a, b)
  4. # take a look at our data frame
  5. df
  1. &lt;hr&gt;
  2. [![bg color for code blocks and output][1]][1]
  3. &lt;hr&gt;
  4. ## Approach Two
  5. 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,
  6. - Firstly, defined a colored box using `\newtcolorbox` along with options for bg color and border color (Refer to package docs for the details)
  7. - 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

  1. \definecolor{shadecolor}{RGB}{249, 247, 223}
  2. \newtcolorbox{codebox}{
  3. colback=shadecolor,
  4. colframe=orange,
  5. boxsep=2pt,
  6. arc=2pt}
  1. library(knitr)
  2. default_source_hook &lt;- knit_hooks$get(&#39;source&#39;)
  3. default_output_hook &lt;- knit_hooks$get(&#39;output&#39;)
  4. knit_hooks$set(
  5. source = function(x, options) {
  6. paste0(
  7. &quot;\n::: {.codebox data-latex=\&quot;\&quot;}\n\n&quot;,
  8. default_source_hook(x, options),
  9. &quot;\n\n:::\n\n&quot;)
  10. }
  11. )
  12. knit_hooks$set(
  13. output = function(x, options) {
  14. paste0(
  15. &quot;\n::: {.codebox data-latex=\&quot;\&quot;}\n\n&quot;,
  16. default_output_hook(x, options),
  17. &quot;\n\n:::\n\n&quot;)
  18. }
  19. )
  20. knitr::opts_chunk$set(echo = TRUE)
  1. a &lt;- c(1,2,3,4,5)
  2. b &lt;- c(1,2,3,4,5)
  3. df &lt;- data.frame(a, b)
  4. # take a look at our data frame
  5. df
  1. &lt;hr&gt;
  2. [![customized code block and output block in beamer][2]][2]
  3. [1]: https://i.stack.imgur.com/6yGTr.png
  4. [2]: https://i.stack.imgur.com/G0sVn.png
  5. </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:

确定