[Tex/LaTex] knitr: How to wrap text around a table generated around code chunk

knitrwrapwrapfigure

I would like to wrap text around a table generated by a knitr code chunk. I found a solution for a regular table here, but I didn't manage to re-implement it for a table generated by a code chunk. For a figure generated by a code chunk I just enclosed the chunk into wrapfigure control sequence, but when I tried to do the same with a table and wraptable I failed.

The whole minimal code:

\documentclass[a4paper,12pt, english]{article}
\usepackage{graphicx}
\usepackage{wrapfig}

\begin{document}
sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1

\begin{wraptable}{r}{0.5\textwidth}
<<chunk_table, results="asis", echo=FALSE>>=
library(xtable)
print(xtable(head(iris[,c(1,2)])))
@
\caption [Iris]{Table iris caption, Table iris caption, Table iris caption, Table iris caption, Table iris caption, Table iris caption}
\end{wraptable}

sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2, sample text 2

\begin{wrapfigure}{r}{0.5\textwidth}
<<image,echo=FALSE, results='asis', echo=FALSE>>= 
boxplot(na.omit(iris))
@ 
\caption [Iris]{Figure iris caption, Figure iris caption,Figure iris caption, Figure iris caption, Figure iris caption, Figure iris caption}
\end{wrapfigure}

sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3, sample text 3
\end{document}

Best Answer

I think that you can accomplish your goal by adding floating = FALSE in your call to print.xtable().

\documentclass[a4paper,12pt, english]{article}
\usepackage{graphicx}
\usepackage{wrapfig}

\begin{document}
sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1

\begin{wraptable}{r}{0.5\textwidth}
<<chunk_table, results="asis", echo=FALSE>>=
library(xtable)
print(xtable(head(iris[,c(1,2)])), floating = FALSE)
@
\caption [Iris]{Table iris caption, Table iris caption, Table iris caption, Table iris caption, Table iris caption, Table iris caption}
\end{wraptable}
    sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1, sample text 1


\end{document}