table caption adds [H] when i use HOLD_position option in kable_styling in pdf output
I am using Quarto to prepare a document. I want to render it as PDF/Latex document. In this document, I need some tables, which I am formatting using kableExtra package. I want to HOLD the position of the table, so that they don't appear on the next page. But when I add "HOLD_position" option in the kable_styling, it adds an extra [H] above the table. Let me share a minimal example of this.
title: "Table Position" format: pdf editor: visual
Quarto
Load the packages required
library(tidyverse) #optional
library(knitr)
library(kableExtra)
Experiment with the data
#| echo: false
#| label: tbl-cars
#| tbl-cap: "Some cars"
head(mtcars) |> kableExtra::kbl(booktabs = TRUE) |> kable_styling(latex_options = c("striped", "bordered","HOLD_position"),full_width = FALSE) %>% column_spec(2, width = "30em")
If I use the option tbl-cap-location: bottom, then the table caption comes at the bottom of the table, but this [H] still remains at the top of the table. When I remove the "HOLD_position" option, this [H] goes away. When I use "hold_position" instead of "HOLD_position", it adds [!h] above the table instead of [H]. How can I get rid of these extra additions to the table?
I just solved it by removing the "HOLD_position" argument, and putting the following code in the preamble:
\AtBeginDocument{
\floatplacement{table}{H}
}
I'm running into this too with kableExtra 1.4.0.4.
I don't see this in the equivalent R Markdown document, so it's likely some small difference in the way LaTeX is generated between the two systems. I don't use Quarto, so someone else will have to come up with a PR.
I confirm this is a Quarto issue for when the table is cross reference (meaning label has a tbl-prefix)
I am also using Quarto and this issue arises. I found that @bhattmaulik 's solution worked. Specifically, in the preamble I included:
format: pdf: header-includes: - \AtBeginDocument{\floatplacement{table}{H}}
@Prio-man There is another solution also. This is mentioned by @cshield in another issue. The idea is to drop "HOLD_position" argument inside the kable_styling function. Instead, add the tbl-pos: H in the YAML block of the document or project. For example,
#| echo: false
#| label: tbl-cars
#| tbl-cap: "Some cars"
#| tbl-pos: H
head(mtcars) |> kableExtra::kbl(booktabs = TRUE) |> kable_styling(latex_options = c("striped", "bordered"),full_width = FALSE) %>% column_spec(2, width = "30em")
The link to the issue is https://github.com/quarto-dev/quarto-cli/issues/9522.
I hope you find this helpful.
The idea is to drop "HOLD_position" argument inside the kable_styling function. Instead, add the tbl-pos: H in the YAML block of the document or project.
This could be a way to handle it at kablextra level:
- detect when
HOLD_positionis used in Quarto context - Either warn or set the
tbl-posoption if not already.
It would be the same for other options. Basically in Quarto it is best to leverage the built-in cross ref feature when they are used so when the kableExtra function are used within a cell with label tbl-<name>
That would be possible, but isn't this a Quarto bug? H is a legal option to the "table" environment (given the right packages). kableExtra isn't the only way to produce \begin{table}[H] at the start of a table.
For example,
kable(head(mtcars), position="H", format="latex", caption="Some caption")
doesn't involve kableExtra at all.
Yes Quarto should do better to detect this. However, it will either try to handle it, or could also warn or throw error to user.
if a user is setting label: tbl-<name>, the expectation is that Quarto should consider a cross-referenced table. And in that case, Quarto is the one to handle \begin{table} environment and position.
Concretely, LaTeX table environment should not be emitted in output within Quarto document where the table are expected to be processed by Quarto.
So what currently happens is that when Quarto sees Raw LaTeX in output, it will try to find the \begin{table} and remove it to adds its own. The current issue is that if fails to detect when option is set and remove it.
Hope it clarifies the situation. I'll try to partially fix in Quarto, but it may means that position will be lost. Only tbl-pos option will matters.