r语言 - 避免代码块在 Knitr 中中断?(最好使用块选项)



使用 knitr 创建 pdf,代码块会根据分页符中断。通常这正是我想要的,但在某些情况下,我希望能够避免这种情况。例如,如果代码块不适合当前页面,则通过代码块跳转到下一页。我宁愿这可以在块选项中完成,即不使用 eg。\新页等

下面是中断的代码块的示例。如何避免这种情况?

documentclass{article}
usepackage[english]{babel}
usepackage{lipsum}
begin{document}
lipsum[1-3] textbf{The following chunk will break. How do I avoid this breaking? }

<<echo=TRUE>>=
(iris)[1:20,]
@

end{document}

我为此在knitr设计中留下了一个knitrout的空环境。您可以重新定义此环境以实现所需的目标。有许多 LaTeX 环境是不可破坏的,例如figure环境。下面我以minipage环境为例:

documentclass{article}
renewenvironment{knitrout}{begin{minipage}{columnwidth}}{end{minipage}}
% alternatively, you can use `figure`
% renewenvironment{knitrout}{begin{figure}}{end{figure}}
begin{document}
begin{figure}
caption{One figure.}
end{figure}
% placeholder
framebox{begin{minipage}[t][0.3paperheight]{1columnwidth}%
nothing
end{minipage}}
<<echo=TRUE>>=
(iris)[1:20,]
@
begin{figure}
caption{Another one.}
end{figure}
end{document}

最新更新