在 PDF/LaTeX 输出中缩进 R Markdown 代码块

Indent R Markdown code block in PDF/LaTeX output

我目前正在用 RMarkdown 编写文档并将输出编织成 pdf。我希望缩进代码块以与我的其余格式保持一致。

这是写的文件:

我希望块的灰色部分符合"This is a sentence."

有没有办法在 R markdown 中做到这一点,也许是通过下降到 LaTeX 中?

如果要更改框的格式,LaTeX模板中的关键行是:

\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}

snugshade 命令在 framed LaTeX package 中定义,这对于创建包含在 RMarkdown 中的阴影框很有用。但是,它缺少许多控件来简单地编辑样式。

基于 this answer, you could consider using the mdframed package which offers more advanced controls than the framed package. As explained here 可以使用 renewenvironment 命令调整边距,我们将使用该命令重新定义具有自定义样式的 Shaded 函数:

为了让它在 RMarkdown 中工作,我们需要用我们的新定义替换 Shaded 环境。这可以通过以下方式实现:

---
output: 
  pdf_document:
    keep_tex: TRUE
header-includes:
  - \usepackage{mdframed}
  - \definecolor{shadecolor}{gray}{.95}
  - \renewenvironment{Shaded}{\begin{mdframed}[
      backgroundcolor=shadecolor,
      linecolor = shadecolor,
      leftmargin=\dimexpr\leftmargin-2pt\relax,
      innerleftmargin=1.6pt,
      innertopmargin=5pt,
      skipabove=10pt,skipbelow=3pt
    ]}{\end{mdframed}}
---

Some Text

```{r cars}
summary(cars)
```

注意:由于您没有提供可重现的示例,您必须微调设置才能使其与您的配置一起使用。

If you start getting more LaTeX commands, you can considering saving this as a separate .tex file as explained here