Control order of output chunks in PDF document


I am building a PDF document using r markdown, tinytex and xelatex. I cannot maintain the order of the output chunks. The document has text chunks, table chunks, and plot chunks. I am especially having trouble with table chunks. I have tried to use the “\FloatBarrier” command to control some of the float issues, but I cannot fit a table and plot on a single page (event though there is room on the page) or maintain the output order.

Can someone point me to resources for controlling output order in r markdown documents that produce a PDF document?


What style of tables are you using? There are several different table generation packages and they likely have different methods of dealing with this.

For example, if you are using xtable, then you can use the table.placement argument in the print.xtable function.


My tables are produced by knitr and kableExtra (kable and kable_styling). Some example code:

 tabOut <-  kable(m.HouseVal,
                     col.names = names_spaced,
                     caption="Comparison of Housing Values", row.names=FALSE,
                     format="latex", booktabs=TRUE)  %>%
      kable_styling() %>%
      row_spec(0, align = "c") %>%
      row_spec(0, align = "c") %>%
      row_spec(1, bold = TRUE, italic = TRUE) %>%
      row_spec(4, bold = TRUE, italic = TRUE) %>%
      column_spec(1, width = "3in",bold = T) %>%
      column_spec(2, width = "0.5in") %>%
      column_spec(3, width ="0.6in") %>%
      column_spec(4, width ="0.6in") %>%
      column_spec(5, width ="0.6in") %>%
      column_spec(6, width ="0.6in") %>%
      add_header_above(header=tblHead1) %>%

Some of this code is internal functions, but this provides a sense of what’s going on.


One more thing. here is the R markdown chunk for this table:


    # Housing
     pophval <<- HouseVal(fips=fips3,ctyname=placeName, ACS=oACS, oType="latex")


Trying again to show the markdown chunk definition:

```{r popHouse_housval, out.height=4, out.width=6, warning=FALSE, message=FALSE, echo=FALSE, results='asis'}
    # Housing
     pophval <<- HouseVal(fips=fips3,ctyname=placeName, ACS=oACS, oType="latex")


Without a reproducible example, I’m going to take a shot in the dark. If you’re using kableExtra, you should be able to specify options through the latex_options in kable_styling(). Looking at the documentation, you could try hold_position:

hold_position will “hold” the floating table to the exact position. It is useful when the LaTeX table is contained in a table environment after you specified captions in kable(). It will force the table to stay in the position where it was created in the document. A stronger version: HOLD_position requires the float package and specifies [H].


Hi jdb,
This worked. Thanks!