# How to remove the blank page when omitting comments and messages in knitr

I am using the following code to create a plot, I want to omit message, comments and warnings and everything else but the plot.

\documentclass{article}
\usepackage{pdflscape}
\pagestyle{empty}
\begin{document}

\begin{landscape}

<<echo=FALSE, message=FALSE, comment=NA, warning=FALSE>>=
x1 <- runif(10)
y1 <- runif(10)
x2 <- runif(10)
y2 <- runif(10)
x <- cbind(x1,x2)
y <- cbind(y1,y2)
plot(y ~ x)
@

\end{landscape}
\end{document}


A blank page is created before the plot. I think there is something related to the landscape that create the blank page. Is there any way to get rid of it? Thanks a lot!

-

## migrated from stackoverflow.comDec 10 '12 at 19:29

This question came from our site for professional and enthusiast programmers.

This doesn't appear to have anything to do with knitr. A file with just the following lines: \documentclass{article}\usepackage{graphicx}\usepackage{pdflscape} \begin{document} \begin{landscape}\includegraphics{figure/unnamed-chunk-1} \end{landscape} \end{document} still creates a blank page. It appears to be the fault of pdf1scape. –  David Robinson Dec 7 '12 at 16:53
Yes it is, I have reported the issue to latex. Thanks! –  Lin Dec 7 '12 at 19:44
@DavidRobinson: I'm unable to reproduce the problem you suggest. Using your MWE produces a single page in landscape format without a blank page inserted. –  Werner Dec 10 '12 at 19:39
@Werner: How large is your figure (what I put as figure/unnamed-chunk-1)? I've noticed it depends on the figure size. The defaults from knitr appear (to me) to lead to a blank page. –  David Robinson Dec 10 '12 at 20:43
@DavidRobinson: This may be the cause, since I inserted content that could fit within the text block boundary. –  Werner Dec 10 '12 at 20:55

If your image is too large (doesn't fit within the text block defined by \textwidth and \textheight), it will cause an overfull box warning, initiate a page break and roll over onto a subsequent page. If the default behaviour of your document construction causes this overflow, the following would take care of discarding the first page (always):

\documentclass{article}
\usepackage{graphicx,atbegshi,pdflscape}% http://ctan.org/pkg/{graphicx,atbegshi,pdflscape}
\pagestyle{empty}
\begin{document}
\begin{landscape}
\includegraphics[height=1.1\textheight]{example-image-a}
\end{landscape}
\end{document}


atbegshi provides \AtBeginShipoutDiscard which discards the currently stored page and should be executed \AtBeginShipout. We test for the value of the page counter and discard the page if its 1. The above document produces a single page, despite the two-page output caused by the oversized image.

If you have this problem only sometimes and want to automate the procedure, you should use the lastpage package to keep track of the last page within your document. If that last page is anything other than 1, discard the first page. Here's a take on that:

\documentclass{article}
\usepackage{graphicx,atbegshi,pdflscape}% http://ctan.org/pkg/{graphicx,atbegshi,pdflscape}
\usepackage{lastpage,refcount}% http://ctan.org/pkg/{lastpage,refcount}
\newcounter{lastpage}
\AtBeginShipout{%
\setcounterpageref{lastpage}{LastPage}
\ifnum\value{page}=1\relax

The refcount package bridges the gap between references and counters by allowing one to assign values of the former to the latter.
Ps. I've used graphicx's \includegraphics in order to place an image of oversized proportions. Your use in knitr would be similar but may not require this package.