knitr有时会不恰当地将代码包装成块

时间:2017-11-21 02:22:35

标签: r latex knitr beamer

在生成LaTeX beamer演示文稿的.Rnw文件中,我发现偶尔knitr会导致一个块中的代码换行,就像它是连续文本一样,即使chunk选项{{1}有效(我将其设置为默认值)。

大多数代码块都已正确格式化。以下是几个被包裹的块​​的例子。

tidy=FALSE

这样打印:

enter image description here

这是另一个例子:

  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)
@

这个看起来像这样:

enter image description here

(请注意,这两个示例都使用模型公式,并且在两个输出中都使用了 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") @ 字符未出现在输出中。但是,其他情况 使用模型公式按预期工作。)

正如我所提到的,几乎所有其他代码块通常打印,尊重块中源的间距。可能导致这个问题的原因是什么?

我还检查过违规区块的行尾是否有额外的空格,但是没有。

如果它有所不同,我使用~

编译演示文稿

1 个答案:

答案 0 :(得分: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}
相关问题