想要把 R Markdown pdf 格式中的代码块修饰成下图的效果 (背景色, 边框, 阴影)
作为 latex 小白各种求助之后, 目前粗糙的办法是用 output hook 直接插入 tcolorbox 的指令,例如
---
title: "Untitled"
output: pdf_document
header-includes:
- \usepackage[skins]{tcolorbox}
---
```{r setup, include=FALSE}
knitr::knit_hooks$set(
source = function(x, options) {
x <- paste0(xfun::split_lines(x), collapse = "\n\n")
paste0(
"\\begin{tcolorbox}[enhanced,fontupper=\\large,drop shadow southeast, sharp corners,colback=white,colframe=lightgray]\\texttt{",
x, "}\\end{tcolorbox}"
)
}
)
```
```{r}
1 + 1
```
然而现在的办法不兼容注释(猜测是 #
在latex代码里的问题 ),会吞掉 {
这样的符号,后续如果需要实现代码高亮似乎也很麻烦。所以来求助是否有更好的办法实现这种效果,比如是否能绕过直接插入 latex 代码 通过在 header 引入一些包的设置解决