I am using knitr to generate a PDF writeup. I want to print a series of tables with section headers in between. I am doing this in an R code chunk. Unfortunately though, what happens is that the first header prints, then a figure, then the rest of the headers fit on that page and the rest of the tables come after rather than being interspersed amongst the headers as desired.
After this page there are a series of 5 more tables on their own pages.
This is the code that I am using:
dfList <- list(alc_top, alc_bottom, cpg_home_top, cpg_home_bottom, electronics_top, electronics_bottom)
labels <- c("Premium Liquor Brand - Top Performers", "Premium Liquor Brand- Bottom Performers", "CPG Home - Top Performers", "CPG Home - Bottom Performers", "Electronics - Top Performers", "CPG Home - Bottom Performers")
for (i in 1:length(dfList)) {
df <- dfList[[i]]
product = "test"
cat(paste("\\section{",labels[i],"}", sep=""))
print(xtable(df,size="\\tiny"))
}
I've tried adding a new line, cat("\\newpage")
within the loop. this adds a new page for each label, but all of the graphs are after the new section again.
I think I need to specify a positioning value (H or h or something like that in LaTex) for the table, but I am not really sure how to do that with xtable and knitr.
The problem here is not the order in which the elements are written to the TEX file. The "wrong order" in the PDF is due to the fact that the tables are wrapped in floating environments and therefore the position of their TEX code in the source file does not necessarily correspond to the table's position in the PDF.
Here are three options that keep the tables at a fixed position. Each one has its pros and cons:
print.xtable
has a floating
argument (which defaults to TRUE
). Setting this argument to FALSE
results in a table that is not wrapped in a floating environment (default: table
).
print.xtable
ignores the caption
and label
arguments on xtable
if floating = FALSE
.print.xtable
has a table.placement
argument that can be used to pass a custom float placement specifier to the floating environment. The specifier H
"places the float at precisely the location in the LaTeX code" (source: Wikibooks). Note that this requires \usepackage{float}
.
The LaTeX package placeins
offers a \FloatBarrier
command which forces that all floats that are not displayed up to this point are printed.
\FloatBarrier
commands that need to be inserted after each table – unless (at least in the specific case of this question) the following feature is used:The package even provides an option to change the definition of
\section
to automatically include a\FloatBarrier
. This can be set by loading the package with the option[section]
(\usepackage[section]{placeins})
. [source: Wikibooks]
\documentclass{article}
\usepackage{float}
\usepackage{placeins}
\begin{document}
<<results = "asis", echo = FALSE>>=
library(xtable)
# This table floats.
print(
xtable(head(cars),
caption = "Floating",
label = "tab:floating"), table.placement = "b"
)
# This table won't float but caption and label are ignored.
print(
xtable(head(cars),
caption = "Not floating",
label = "tab:not-floating"),
floating = FALSE)
# Placement "H". (requires "float" package)
print(
xtable(head(cars),
caption = "Non-floating float",
label = "tab:not-actually-floating"),
table.placement = "H")
cat("Text before the barrier. (text 1)")
# Floats won't float beyond this barrier (requires "placeins" package)
cat("\\FloatBarrier\n")
cat("Text after the barrier. (text 2)")
@
Add \texttt{table.placement = "b"} to the first table to see that it will be located at the bottom of page 1 (after `text 1') and `text 2` will come \emph{after} it (on page 2), althogh there would be plenty of space on page 1. This is because the float cannot `pass' the barrier.
\end{document}