【问题标题】:Avoid code-chunks from breaking in Knitr? ( preferably using a chunk option )避免代码块破坏 Knitr? (最好使用块选项)
【发布时间】:2013-11-08 10:30:01
【问题描述】:

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

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

\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}

【问题讨论】:

  • 这是不言而喻的,但我还是要说。您是否尝试在块前添加\newpage 或\clearpage?不过,如果这是自动完成(通过块选项控制),那就太好了。
  • 谢谢。我会改写这个问题。
  • 能否将块嵌入到浮动环境中,例如\begin{figure}[!Htbp]...\end{figure}{}?

标签: r pdf break knitr floating


【解决方案1】:

为此,我在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.3\paperheight]{1\columnwidth}%
nothing
\end{minipage}}

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

\begin{figure}
\caption{Another one.}
\end{figure}

\end{document}

【讨论】:

    猜你喜欢
    • 2019-01-16
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多