2013-10-29 27 views
4

使用knitr創建pdf,codechunks根據分頁符中斷。通常這正是我想要的,但在某些情況下,我希望能夠避免這種情況。例如。通過使代碼塊跳轉到下一頁,如果它不適合當前頁面。我寧願如果這可以在一個塊選項中完成,我不使用例如。 \ newpage等。避免編碼塊破壞Knitr? (最好使用塊選項)

以下是一個破解代碼塊的示例。我如何避免這種情況?

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

不言而喻,但我會說它無論如何。您是否曾嘗試在塊前添加'\ newpage'或'\ clearpage'?不過,如果這是自動完成的(通過塊選項控制),那將會很好。 –

+0

謝謝。我將重申這個問題。 –

+3

您可以將塊嵌入浮動環境中,例如'\ begin {figure} [!Htbp] ... \ end {figure} {}'? – baptiste

回答

3

我在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}