سؤال

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}
هل كانت مفيدة؟

المحلول

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}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top