如何在 Quarto 中更改代码块的字体大小?

3

我正在使用quarto来准备Beamer演示文稿。我想要减小给定代码块中代码的字体大小,以便一切都更好地适应。请参考下面的示例:

enter image description here

生成该块的代码如下:
```{python}
# Import packages
import numpy as np
```

```{python}
#| echo: true
w = np.array([0.7, 0.3])
Sigma = np.array([[0.09, 0.1], [0.1, 0.25]])

var_rp = w.T @ Sigma @ w
print("Variance of our example portfolio {:.4f}".format(var_rp))
print("Standard Deviation of our example portfolio {:.2f}".format(np.sqrt(var_rp)))
```

有没有办法改变这个块的字体大小,使得所有代码行都能在 PDF 中单行显示?提前感谢!

2个回答

4

使用monofontoptions指定monofont并缩小字体。请查看Quarto Beamer参考文档以了解这些字体选项

我使用了Source Code Pro字体,来自LaTeX字体目录。您可以在那里查找其他等宽字体。

---
title: "Small code font"
format: beamer
jupyter: python3
pdf-engine: lualatex
monofont: 'Source Code Pro'
monofontoptions: 
  - Scale=0.55
---

## Portfolio

Let's do this computation in python.....


```{python}
# Import packages
import numpy as np
```

```{python}
#| echo: true
w = np.array([0.7, 0.3])
Sigma = np.array([[0.09, 0.1], [0.1, 0.25]])

var_rp = w.T @ Sigma @ w
print("Variance of our example portfolio {:.4f}".format(var_rp))
print("Standard Deviation of our example portfolio {:.2f}".format(np.sqrt(var_rp)))
```


code font size reduced


注意:此解决方案将减小所有代码块和代码输出的字体大小。如果您想要特定代码块的更小字体大小,请查看samcarter_is_at_topanswers.xyz提供的答案。


1

您可以使用\AddToHookNext{env/Highlighting/begin}{\tiny}来更改下一个代码块的字体大小,而不影响其他代码块:

---
format: beamer
---

```{python}
# Import packages
import numpy as np
```

\AddToHookNext{env/Highlighting/begin}{\tiny}
```{python}
#| echo: true
w = np.array([0.7, 0.3])
Sigma = np.array([[0.09, 0.1], [0.1, 0.25]])

var_rp = w.T @ Sigma @ w
print("Variance of our example portfolio {:.4f}".format(var_rp))
print("Standard Deviation of our example portfolio {:.2f}".format(np.sqrt(var_rp)))
```

enter image description here


(如果您想更改所有代码块的字体大小,请在头文件中包含\AddToHook{env/Highlighting/begin}{\tiny}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接