4

Using knitr to create a pdf, codechunks break according to page breaks. Usually this is exactly what I want, but in some cases I would like to be able to avoid this. E.g. by making a code-chunk jump to the next page if it does not fit the current page. I would prefer if this could be done in a chunk option, I.E not using eg. \newpage etc.

The following is an example of a code-chunk that breaks. How do I avoid this?

\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}
Rasmus Larsen
  • 5,721
  • 8
  • 47
  • 79
  • It goes without saying, but I'll say it anyway. Have you tried adding `\newpage` or `\clearpage` in front of the chunk? It would be nice if this was done automatically (controlled through chunk options), though. – Roman Luštrik Oct 29 '13 at 10:20
  • Thanks. I will rephrase the question. – Rasmus Larsen Oct 29 '13 at 10:38
  • 3
    can you embed the chunk into a floating environment such as `\begin{figure}[!Htbp]...\end{figure}{}`? – baptiste Oct 29 '13 at 11:01

1 Answers1

3

I left an empty environment knitrout in the knitr design for such purposes. You can redefine this environment to achieve what you want. There are many LaTeX environments that are non-breakable, such as a figure environment. Below I use the minipage environment as an example:

\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}
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419