gpt4 book ai didi

r - 在 knitrout 环境之后确保相同的垂直空间

转载 作者:行者123 更新时间:2023-12-04 11:50:09 25 4
gpt4 key购买 nike

我正在使用 knitr 嵌入 R 代码并输出到我正在编写的教科书中。在我的教科书中,我首先用深灰色背景编写 R 代码,然后在下一个 block 中用浅色背景运行 R 代码。我基本上执行以下操作:

<<eval = F, background = "gray90">>=
2+2
@

\vspace{-.6cm}
<<eval = T, echo = F, background = "gray95">>=
2+2
@

我使用 \vspace{-.6cm} 来缩小两个 block 之间的间距。

但是,这并不能确保 block 之间的垂直间距相同,因为它似乎受到当前页面上的文本、图像等数量的影响。

下面的屏幕截图显示了这个问题。

enter image description here

前两个 block 由以下人员创建:

<<eval = F, background = "gray90">>=
cor(latent_est)
@

\vspace{-.6cm}
<<eval = T, echo = F, background = "gray95">>=
cor(latent_est)
@

最后两个 block 是:

<<eval = F, background = "gray90">>=
rmsd(latent_est$MLE, latent_est$MAP)
rmsd(latent_est$MLE, latent_est$EAP)
rmsd(latent_est$MAP, latent_est$EAP)
@

\vspace{-.6cm}
<<eval = T, echo = F, background = "gray95">>=
rmsd(latent_est$MLE, latent_est$MAP)
rmsd(latent_est$MLE, latent_est$EAP)
rmsd(latent_est$MAP, latent_est$EAP)
@

我知道这可能不是插入垂直空间的最佳方式,但我想知道是否有一种方法可以在 block 后始终添加相同的垂直空间?

下面是一个重现这个的例子。请注意,\section{} 这次弄乱了垂直间距。我怎样才能在一个 block 之后始终获得相同的间距?

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\begin{document}

% Misbehaving chunks!
\begin{equation}
RMSD=\sqrt{\frac{\sum_{j=1}^{N}(\hat{\theta}_{1j}-\hat{\theta}_{2j})^2}{N}},
\end{equation}
\blindtext

<<eval = F, background = "gray90">>=
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
summary(lm.D9)
@

\vspace{-.6cm}
<<eval = T, echo = F, background = "gray95">>=
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
summary(lm.D9)
@

Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text

\section{Model Diagnostics}

Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text

\newpage


% Correct chunks!
\begin{equation}
RMSD=\sqrt{\frac{\sum_{j=1}^{N}(\hat{\theta}_{1j}-\hat{\theta}_{2j})^2}{N}},
\end{equation}
\blindtext

<<eval = F, background = "gray90">>=
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
summary(lm.D9)
@

\vspace{-.6cm}
<<eval = T, echo = F, background = "gray95">>=
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
summary(lm.D9)
@

Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text

Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text

\end{document}

最佳答案

在序言中添加 \usepackage{parskip}

Misbehaving chunk

Correct chunk

关于r - 在 knitrout 环境之后确保相同的垂直空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46980162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com