2

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!

Lin
  • 145

1 Answers1

2

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}
\AtBeginShipout{\ifnum\value{page}=1\AtBeginShipoutDiscard\fi}
\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
    \ifnum\value{lastpage}>1\AtBeginShipoutDiscard\fi\fi}
\pagestyle{empty}
\begin{document}
\begin{landscape}
  \includegraphics[height=1.1\textheight]{example-image-a}
\end{landscape}
\end{document}

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.

Werner
  • 603,163