knitr有时会不适当地将代码块包装起来

3
在生成LaTeX beamer演示文稿的.Rnw文件中,我发现有时候knitr会导致代码块换行,就好像它是连续的文本一样,即使chunk选项tidy=FALSE已经生效(我已将其设置为默认值)。
大多数代码块都格式正确。以下是一些被包装的代码块示例。
  It is often useful to plot the data for the binary distinction between $y_i = 0$
  vs. $y_i > 0 $ as in logistic regression models.

<<phd-zero, h=5, w=5, out.width='0.5\\textwidth', size='footnotesize' >>=
plot(factor(articles==0) ~ mentor, data=PhdPubs,
    ylevels=2:1, ylab="Zero articles",
    breaks=quantile(mentor, probs=seq(0,1,.2)), cex.lab=1.25)
@

以下是打印效果:

在此输入图片描述

这里有另一个例子:

    For simplicity, we use all predictors for both the zero model
    and the non-zero model.

<<size='footnotesize', tidy=FALSE>>=
library(countreg)
phd.zip <- zeroinfl(articles ~ ., data=PhdPubs, dist="poisson")
phd.znb <- zeroinfl(articles ~ ., data=PhdPubs, dist="negbin")

phd.hp  <- hurdle(articles ~ ., data=PhdPubs, dist="poisson")
phd.hnb <- hurdle(articles ~ ., data=PhdPubs, dist="negbin")
@

这个例子如下所示:

enter image description here

(请注意,这两个示例均使用模型公式,并且在两个输出中,~字符不会出现在输出中。但是,其他使用模型公式的情况的效果符合预期。)
正如我所提到的,几乎所有其他代码块都可以正常打印,并遵守代码块源中的间距。这可能是什么原因导致的?
我还检查了有问题的代码块中是否有额外的空格,但没有发现。
如果有任何区别,请注意,我使用knitr::knit2pdf()编译演示文稿。
1个回答

0
当你在帧中忘记使用 [fragile] 选项时,就会出现这种行为。通常会导致错误,但在这种情况下却会神秘地导致观察到的错误格式。
这个是可行的:
\begin{frame}[fragile]
  \frametitle{Example: Phd Publications}
    For simplicity, we use all predictors for both the zero model
    and the non-zero model.

<<fit-models, size='footnotesize', tidy=FALSE>>=
library(countreg)
phd.zip <- zeroinfl(articles ~ ., data=PhdPubs, dist="poisson")
phd.znb <- zeroinfl(articles ~ ., data=PhdPubs, dist="negbin")

phd.hp  <- hurdle(articles ~ ., data=PhdPubs, dist="poisson")
phd.hnb <- hurdle(articles ~ ., data=PhdPubs, dist="negbin")
@
\end{frame}

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