1

I have a Quarto (RMarkdown) document which is supposed to display a table showing task completion. The code works well but there is excessive white space displayed, to the point where the table is displayed on page 2 where a single page would have sufficed and page 1 is mostly white space. How can I make the table fit on one page? The code is as below:

---
title: "Debug"
author: "Data Kilimba"
format: pdf
editor: visual
---

## FAAB Market Survey Task Completion


```{r setup}
#| include: false
library(tidyverse)

monthly_submissions = tibble::tribble(
  ~Year, ~Month,               ~FAAB, ~Submissions,
  2022L,     3L,      "Simon",           0L,
  2022L,     3L,  "Kabula",           0L,
  2022L,     3L,  "Innocent",           0L,
  2022L,     3L, "Renatus",           0L,
  2022L,     3L,  "Staphord",           2L,
  2022L,     3L, "Salome",           0L,
  2022L,     3L, "Imani",           0L,
  2022L,     3L,       "Petro",          10L,
  2022L,     3L,   "Mihayo",           0L,
  2022L,     3L,     "Baraka",           0L,
  2022L,     3L,     "Swaum",           0L
  )


```

```{r}
#| label: tbl-stats
#| tbl-cap: "FAAB Market Survey Task completion"
#| echo: false

monthly_submissions %>% 
  kableExtra::kable(escape = FALSE,format = "latex", booktabs = T) %>% 
  kableExtra::kable_styling(latex_options="scale_down",font_size = 2)

```

UPDATE:: After editing the document as suggested by Lucas, I add the YAML header and yet there seem to be syntax errors of some kind ... `enter image description here

Tumaini Kilimba
  • 329
  • 2
  • 12

3 Answers3

2

I have arrived at a solution, although I have to say there are some weird goings-on with regards to my YAML. There were two issues.

  1. This is my YAML (this worked)

    ---
    title:"Debug"
    author: "Data Kalimba"
    format: pdf
    editor: visual
    header-includes:
       \usepackage{float}
       \floatplacement{table}{H}
    ---
    

As you can see...

header-includes:
   - \usepackage{float}
   - \floatplacement{table}{H}

is what shoul have worked according not only to @Lucas, but countless other examples online (notice the minus signs before the backslashes). However this for me resulted in the errors in the below screenshot. What did work was removing the minus signs.

enter image description here.

But this was not enough...

  1. Secondly was the specification within kable_styling's latex_option. What Lucas had suggested was

kableExtra::kable_styling(latex_options="scale_down",font_size = 2,position = "float_left")

However what I found to work was the float_left argument WITHIN the latex_options argument list, so the below amendment worked:

kableExtra::kable_styling(latex_options=c("scale_down","float_left"),font_size = 2)

So, the entire (working) code is:

---
title: "Debug"
author: "Data Kilimba"
format: pdf
editor: visual
header-includes:
   \usepackage{float}
   \floatplacement{table}{H}
---
  
## FAAB Market Survey Task Completion
  
  
```{r setup}
#| include: false
library(tidyverse)

monthly_submissions = tibble::tribble(
  ~Year, ~Month,               ~FAAB, ~Submissions,
  2022L,     3L,      "Simon",           0L,
  2022L,     3L,  "Kabula",           0L,
  2022L,     3L,  "Innocent",           0L,
  2022L,     3L, "Renatus",           0L,
  2022L,     3L,  "Staphord",           2L,
  2022L,     3L, "Salome",           0L,
  2022L,     3L, "Imani",           0L,
  2022L,     3L,       "Petro",          10L,
  2022L,     3L,   "Mihayo",           0L,
  2022L,     3L,     "Baraka",           0L,
  2022L,     3L,     "Swaum",           0L
)


```

```{r}
#| label: tbl-stats
#| tbl-cap: "FAAB Market Survey Task completion"
#| echo: false

monthly_submissions %>% 
  kableExtra::kable(escape = FALSE,format = "latex", booktabs = T) %>% 
  kableExtra::kable_styling(
    # Change happens in YAML and here, float_left argument is WITHIN 
    # latex_options arguments, 
    latex_options=c("scale_down","float_left"),font_size = 2)

```
karel
  • 5,489
  • 46
  • 45
  • 50
Tumaini Kilimba
  • 329
  • 2
  • 12
  • 1
    Very good summary! I think the differences in execution may depend on the R / RStudio version installed on the depending system... – Lucas Apr 25 '22 at 09:23
2

The YAML header causing an error shouldn't be occurring -- Quarto was in this case being too picky about it during validation. We've just made a change to permit use of multiple values for header-includes here: https://github.com/quarto-dev/quarto-cli/commit/5a685aee484f4d5914e7a899968f654584e24904

jjallaire
  • 331
  • 1
  • 3
1

The latex placement can be tricky. One option is to tell kable_styling the position argument:

kableExtra::kable_styling(latex_options="scale_down",font_size = 2,position = "float_left")

Also I am not sure about the yaml header. The following works for me:

---
title: "Debug"
author: "Data Kilimba"
output: pdf_document
header-includes:
   - \usepackage{float}
   - \floatplacement{table}{H}
---
Lucas
  • 409
  • 2
  • 10
  • Thanks, but just adding the aditional `position = "float_left"` seems to cause compilation errors.... `! LaTeX Error: Not in outer par mode.` ... what could be going wrong? – Tumaini Kilimba Apr 12 '22 at 11:54
  • try this: https://stackoverflow.com/questions/50879745/latex-kable-side-by-side-tables-not-in-outer-par-mode#comment113681243_50879745 – Lucas Apr 12 '22 at 12:48
  • I am not sure if that post is trying to solve the same issue, the link refers to placing tables side by side, whereas I just want to get rid of whitespace. Additionally, it seems a rather "hacky" solution with lots of raw latex (which I am not familiar nor comfortable with) interspersed with R code. I would rather avoid hacky solutions if possible and hope/pray that the libraries `kable`, `kableExtra` have built-in solutions? As a starting point, did adding the option `position = "float_left"` work for you? If yes, then I need to find out why it is not working for me. – Tumaini Kilimba Apr 13 '22 at 09:33
  • Yes. It works perfectly for me. I get one pdf page with the table! Refering to the link: There is a comment "Adding - \usepackage{float} and - \floatplacement{table}{H} to the header". can you try this? – Lucas Apr 13 '22 at 11:20
  • I added your suggestions to the YAML, however look at the edited original post, there are still issues which have me flummoxed – Tumaini Kilimba Apr 14 '22 at 15:05
  • I am not sure why. But can you try to edit your YAML header like shown here: https://bookdown.org/yihui/rmarkdown-cookbook/latex-extra.html – Lucas Apr 19 '22 at 13:37
  • I am not seeing an obvious way of transforming this format `header-includes: - \usepackage{float} - \floatplacement{table}{H}` into something akin to `extra_dependencies: caption: ["labelfont={bf}"]`. My limited knowledge is that `float` is a package and `floatplacement` is a function within the `float` package, but how this fits the `extra_dependencies` syntax is not yet clear to me :-( – Tumaini Kilimba Apr 20 '22 at 09:09
  • so I wrote the YAML using the format prescribed in the link `--- title: "Soya Demo Outcomes" author: "SAKiRP" format: pdf output: pdf_document: extra_dependencies: float: null table: ["floatplacement={H}"] editor: visual ---` but still the table is pushed to the very bottom – Tumaini Kilimba Apr 24 '22 at 08:51