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?
Thanks
AB

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,
                     align=c("lrrrrr"),
                     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) %>%
      add_footnote(captionSrc("ACS",ACS))

Some of this code is internal functions, but this provides a sense of what's going on.
Cheers--
AB

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

\FloatBarrier

 if(outHouse){
    # Housing
     pophval <<- HouseVal(fips=fips3,ctyname=placeName, ACS=oACS, oType="latex")
     print(pophval)
 }

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'}
 if(outHouse){
    # Housing
     pophval <<- HouseVal(fips=fips3,ctyname=placeName, ACS=oACS, oType="latex")
     print(pophval)
 }

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].

2 Likes

Hi jdb,
This worked. Thanks!
AB

1 Like