英文:
Problem with R chunk output which spans more than one page in Quarto PDF
问题
R块的输出在Quarto PDF中跨越多个页面时,显示不正确,如下所示的R代码输出。
如何正确呈现它?
---
book:
title: "Title"
author: "MYaseen208"
format:
pdf:
documentclass: scrreprt
toc: true
toc-depth: 3
execute:
echo: false
warning: false
---
# 实验规划与布局
library(knitr)
library(eda4treeR)
#| label: Reading-Packages
#| output: false
## 示例2.1(第22页)
::: callout-tip
## 示例2.1(第22页)
进行了一项田间试验,比较了从种子圃(SO)获得的种子批次与从例行种群(P)收集的种子批次。每个种子批次有八块,七年龄时修剪。在15年时测量胸高直径(dbh),计算地块均值。下面是执行地块均值的完全随机分析的`R`程序:
```r
#| class-output: r
options(continue = " ", prompt = " ")
example(
topic = "Exam2.1"
, package = "eda4treeR"
, lib.loc = NULL
, character.only = c(TRUE, FALSE)[2]
, give.lines = c(TRUE, FALSE)[2]
, local = c(TRUE, FALSE)[2]
, type = c("console", "html")[2]
, echo = c(TRUE, FALSE)[1]
, verbose = getOption("verbose")
, setRNG = c(TRUE, FALSE)[1]
, ask = getOption("example.ask")
, prompt.prefix = NULL
, run.dontrun = c(TRUE, FALSE)[2]
, run.donttest = interactive()
)
:::
示例2.2(第24页)
::: callout-tip
示例2.2(第24页)
进行了一项田间试验,比较了从种子圃(SO)获得的种子批次与从例行种群(P)收集的种子批次。每个种子批次有八块,七年龄时修剪。在15年时测量胸高直径(dbh),计算地块均值。下面是执行地块均值的随机完全区组分析的R
程序:
#| class-output: r
options(continue = " ", prompt = " ")
example(
topic = "Exam2.2"
, package = "eda4treeR"
, lib.loc = NULL
, character.only = c(TRUE, FALSE)[2]
, give.lines = c(TRUE, FALSE)[2]
, local = c(TRUE, FALSE)[2]
, type = c("console", "html")[2]
, echo = c(TRUE, FALSE)[1]
, verbose = getOption("verbose")
, setRNG = c(TRUE, FALSE)[1]
, ask = getOption("example.ask")
, prompt.prefix = NULL
, run.dontrun = c(TRUE, FALSE)[2]
, run.donttest = interactive()
)
:::
<details>
<summary>英文:</summary>
The output of an R chunk, which extends beyond a single page in a Quarto PDF, does not display correctly, as shown in the following R code output.
How can I correctly render it?
---
book:
title: "Title"
author: "MYaseen208"
format:
pdf:
documentclass: scrreprt
toc: true
toc-depth: 3
execute:
echo: false
warning: false
---
# Experimental Planning & Layout
```{r}
library(knitr)
library(eda4treeR)
```
```{r}
#| label: Reading-Packages
#| output: false
```
## Example 2.1 (Pg. 22)
::: callout-tip
## Example 2.1 (Pg. 22)
A field trial was planted to compare a seedlot derived from a seed orchard (SO) with one collected from a routine population (P). There were eight plots of each seedlot, thinned at seven year of age. Tree diameters at breast height (dbh) were measured at 15 years and plot means calculated. An `R` program to carry out a Completely Randomized Analysis of plot means is given below:
```{r}
#| class-output: r
options(continue = " ", prompt = " ")
example(
topic = "Exam2.1"
, package = "eda4treeR"
, lib.loc = NULL
, character.only = c(TRUE, FALSE)[2]
, give.lines = c(TRUE, FALSE)[2]
, local = c(TRUE, FALSE)[2]
, type = c("console", "html")[2]
, echo = c(TRUE, FALSE)[1]
, verbose = getOption("verbose")
, setRNG = c(TRUE, FALSE)[1]
, ask = getOption("example.ask")
, prompt.prefix = NULL
, run.dontrun = c(TRUE, FALSE)[2]
, run.donttest = interactive()
)
```
:::
## Example 2.2 (Pg. 24)
::: callout-tip
## Example 2.2 (Pg. 24)
A field trial was planted to compare a seedlot derived from a seed orchard (SO) with one collected from a routine population (P). There were eight plots of each seedlot, thinned at seven year of age. Tree diameters at breast height (dbh) were measured at 15 years and plot means calculated. An `R` program to carry out a Randomized Complete Block Analysis of plot means is given below:
```{r}
#| class-output: r
options(continue = " ", prompt = " ")
example(
topic = "Exam2.2"
, package = "eda4treeR"
, lib.loc = NULL
, character.only = c(TRUE, FALSE)[2]
, give.lines = c(TRUE, FALSE)[2]
, local = c(TRUE, FALSE)[2]
, type = c("console", "html")[2]
, echo = c(TRUE, FALSE)[1]
, verbose = getOption("verbose")
, setRNG = c(TRUE, FALSE)[1]
, ask = getOption("example.ask")
, prompt.prefix = NULL
, run.dontrun = c(TRUE, FALSE)[2]
, run.donttest = interactive()
)
```
:::
</details>
# 答案1
**得分**: 1
你可以设置PDF选项 `listings: true` 以确保输出正确显示,但是你需要应用一些额外的样式来模仿 `class-output: r` 的样式(参见这个 [帖子](https://stackoverflow.com/questions/21402157/colour-for-r-code-chunk-in-listings-package))。
```yaml
---
format:
pdf:
documentclass: scrreprt
toc: true
toc-depth: 3
listings: true
include-in-header:
- text: |
\usepackage[usenames,dvipsnames]{color}
\lstset{
language=R, % 代码的语言
basicstyle=\tiny\ttfamily, % 用于代码的字体大小
numbers=left, % 行号放置位置
numberstyle=\tiny\color{Blue}, % 行号的样式
stepnumber=1, % 两行行号之间的步骤数。如果为1,每行都会编号
numbersep=5pt, % 行号与代码之间的距离
backgroundcolor=\color{white}, % 选择背景颜色。您必须添加 \usepackage{color}
showspaces=false, % 显示添加特定下划线的空格
showstringspaces=false, % 下划线显示字符串内的空格
showtabs=false, % 在字符串内显示制表符,添加特定下划线
frame=single, % 在代码周围添加框架
rulecolor=\color{black}, % 如果未设置,文本内的文本行断开时,框架颜色可能会更改(例如,这里是绿色的注释)
tabsize=2, % 设置默认制表符大小为2个空格
captionpos=b, % 将标题位置设置为底部
breaklines=true, % 设置自动断行
breakatwhitespace=false, % 设置是否仅在空白处自动换行
keywordstyle=\color{RoyalBlue}, % 关键字样式
commentstyle=\color{YellowGreen}, % 注释样式
stringstyle=\color{ForestGreen} % 字符串字面样式
}
---
[![输入图像描述][1]][1]
[1]: https://i.stack.imgur.com/gRFW0.png
英文:
You can set the pdf option listings: true
such that your output is displayed correctly, however, you need to apply some additional styling to mirror the style of class-output: r
(See this post).
---
format:
pdf:
documentclass: scrreprt
toc: true
toc-depth: 3
listings: true
include-in-header:
- text: |
\usepackage[usenames,dvipsnames]{color}
\lstset{
language=R, % the language of the code
basicstyle=\tiny\ttfamily, % the size of the fonts that are used for the code
numbers=left, % where to put the line-numbers
numberstyle=\tiny\color{Blue}, % the style that is used for the line-numbers
stepnumber=1, % the step between two line-numbers. If it is 1, each line
% will be numbered
numbersep=5pt, % how far the line-numbers are from the code
backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
frame=single, % adds a frame around the code
rulecolor=\color{black}, % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. commens (green here))
tabsize=2, % sets default tabsize to 2 spaces
captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
keywordstyle=\color{RoyalBlue}, % keyword style
commentstyle=\color{YellowGreen}, % comment style
stringstyle=\color{ForestGreen} % string literal style
}
execute:
echo: false
warning: false
---
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论