简体   繁体   English

如何在 Rmarkdown 生成的 pdf 中适合控制台输出?

[英]How to fit console output in pdf generated by Rmarkdown?

在此处输入图像描述 在此处输入图像描述

I am trying to print str(df) in pdf output of rmarkdown.我正在尝试在 rmarkdown 的 pdf 输出中打印str(df) How do I fit the wide output in pdf?如何在 pdf 中适应宽输出?

For PDF output is a bit trickier to wrap lines.对于 PDF 输出,换行有点棘手。 One option is using the LaTeX package listings .一种选择是使用 LaTeX 包listings I will use mtcars dataset as example.我将使用mtcars数据集作为示例。 Your Rmarkdown should enable the package via the Pandoc argument --listings like this:您的 Rmarkdown 应该通过 Pandoc 参数--listings启用包,如下所示:

---
title: "Test"
output:
  pdf_document:
    pandoc_args: --listings
    includes:
      in_header: preamble.tex
---

```{r, echo=FALSE, warning=FALSE, message=FALSE}
options(width = 300)
str(mtcars)
```

In preamble.tex , you set an option of the package in a different file:preamble.tex中,您在不同的文件中设置包的选项:

\lstset{
  breaklines=true
  basicstyle=\ttfamily
}

This is the output:这是输出:

在此处输入图像描述

For more info check this link: https://bookdown.org/yihui/rmarkdown-cookbook/text-width.html有关更多信息,请查看此链接: https ://bookdown.org/yihui/rmarkdown-cookbook/text-width.html

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

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