简体   繁体   English

Quarto PDF output:代码块行间距

[英]Quarto PDF output: code block line spacing

This Github repo , hosts a.qmd file of my dissertation template.这个Github repo包含我的论文模板的 .qmd 文件。 In config/preamble.tex I've set \onehalfspacing and \linespread{1.5} which I thought would affect only plain text and not also code blocks.config/preamble.tex我设置\onehalfspacing\linespread{1.5}我认为这只会影响纯文本而不影响代码块。

代码块

Is it possible to change monofont spacing individually (or inversely, set space for mainfont only)?是否可以单独更改单字体间距(或相反,仅为主字体设置空间)?

More quarto way to do this actually modifying the knitr chunk hook.更多 quarto 方法实际上修改了knitr chunk hook。

---
title: ""
format:
    pdf:
      include-in-header:
        text: |
          \usepackage{lipsum}
          \usepackage{setspace}
          \onehalfspacing
          \linespread{2}
      df-print: kable
      highlight-style: zenburn
fontsize: 12pt
geometry: margin=1in
---

```{r}
#| label: setup
#| include: false


chunk_hook  <- knitr::knit_hooks$get("chunk")
knitr::knit_hooks$set(chunk = function(x, options) {
  x <- chunk_hook(x, options)
  paste0("\\linespread{0.5}\n", x, "\n\n\\linespread{2}")
})

```

## Different linespacing for text and code

When you click the **Render** button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

```{r}
library(dplyr, quietly = TRUE)

mtcars %>% 
  group_by(am) %>% 
  summarise(
    disp = mean(disp),
    mpg = mean(mpg)
  )
```

\lipsum[1]


代码和文本的不同行空间


Here I have used linespace 0.5 for code and linespace 2 for text.在这里,我为代码使用了行空间 0.5,为文本使用了行空间 2。 Change these as you need.根据需要更改这些。

Code blocks are defined in Shaded environment.代码块在Shaded环境中定义。 So, the fix was simply redefining it in preamble.tex using singlespace environment and \linespread{1} :因此,修复只是使用单空间环境和 \ singlespace \linespread{1}preamble.tex中重新定义它:

\renewenvironment{Shaded}
    {\begin{snugshade}
    \begin{singlespace}
    \linespread{1}
    }
    {\end{singlespace}
    \end{snugshade}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM