我正在使用knitr将R代码和输出嵌入到我正在编写的教科书中。在我的教科书中,我首先使用深灰色背景编写R代码,然后在具有较亮背景的下一个块中运行R代码。我基本上做了以下事情:
<<eval = F, background = "gray90">>=
2+2
@
\vspace{-.6cm}
<<eval = T, echo = F, background = "gray95">>=
2+2
@
我使用\vspace{-.6cm}
使两个块之间的间距变小。
但是,这并不能确保块之间的垂直间距相同,因为它似乎受到当前页面上文本,图像等数量的影响。
下面的屏幕截图显示了此问题。
前两个块是由:
创建的<<eval = F, background = "gray90">>=
cor(latent_est)
@
\vspace{-.6cm}
<<eval = T, echo = F, background = "gray95">>=
cor(latent_est)
@
最后两个块是:
<<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)
@
我知道这可能不是插入垂直空间的最佳方式,但我想知道是否有办法在块后一致地添加相同的垂直空间?
以下是重现此内容的示例。请注意\section{}
这次会混淆垂直间距。如何在大块后总是得到相同的间距?
\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}