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

标签 latex r-markdown knitr

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

这是写好的文档:

example document

我希望 block 的灰色部分与“This is a sentence”一致。

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

最佳答案

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

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

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

基于 this answer ,您可以考虑使用 mdframed 包,它提供比 framed 包更高级的控件。正如解释的那样 here可以使用 renewenvironment 命令调整边距,我们将使用该命令以自定义样式重新定义 Shaded 函数:

enter image description here

为了让它在 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)
```

enter image description here

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

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

关于latex - 在 PDF/LaTeX 输出中缩进 R Markdown 代码块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49929273/

相关文章:

python - 如何在另一个 tk 小部件中使用 matplotlib 之外的 matplotlib 的 mathtext 渲染?

LaTex,在对齐 block 之间对齐对齐字符

r - 使用 knit 和 Rstudio 自动调整 LaTeX 表格宽度以适合 pdf

Rmarkdown beamer 目录幻灯片,加上非当前部分的灰色

latex - 未定义的控制序列

r-markdown - 在 shiny-server 上托管 html 页面

r - 以不同顺序评估 Rmd 中的代码块

r - 在 R Markdown 中为代码块添加换行符

r - Sweave 缓存包

r - 停止表格从观星者的页面末尾消失