How can I center this table in a pdf? The line table.align = "center",
is ignored. I have tried wrapping my code with cat("\\clearpage\n\\begin{center}\n")
and cat("\n\\end{center}")
in a variety of ways with no luck. The header centers properly, but the table is aligned to the left. I know that one solution is to use kableExtra
, but it doesn't handle small tables well. I can only get it to render nicely using kableExtra
if I add a blank row to the bottom, which looks odd. I'd prefer to use gt
but I'd be happy for a solution with kableExtra
that doesn't require adding a blank row.
```{r wald_table_gt, results='asis', echo=FALSE, message=FALSE, warning=FALSE}
library(tibble)
library(dplyr)
library(gt)
# Construct table
table_data_wald <- tribble(
~Statistic, ~Value,
"t-statistic", "-80.883",
"Degrees of Freedom", "12,482",
"p-value", "< 0.001",
"Alternative Hypothesis", "true mean ≠ -1",
"Mean of RTS", "-1.201",
"95% Confidence Interval", "[-1.206, -1.196]"
)
# Build table
table_data_wald %>%
gt() %>%
tab_header(
title = md("**Table A1: Wald Test**")
) %>%
cols_label(Statistic = "Statistic", Value = "Value") %>%
tab_options(
table.align = "center",
heading.align = "center",
column_labels.font.size = px(11),
table.font.size = px(11),
table.width = pct(70),
footnotes.font.size = px(9),
footnotes.multiline = TRUE
)
```
Here is a possibility where we post-process the gt
LaTeX using as_latex()
for inserting a \centering
after the \begin{table}
:
---
format: pdf
keep-tex: true
---
```{r wald_table_gt, results='asis', echo=FALSE, message=FALSE, warning=FALSE}
library(tibble)
library(dplyr)
library(gt)
# Construct table
table_data_wald <- tribble(
~Statistic, ~Value,
"t-statistic", "-80.883",
"Degrees of Freedom", "12,482",
"p-value", "< 0.001",
"Alternative Hypothesis", "true mean ≠ -1",
"Mean of RTS", "-1.201",
"95% Confidence Interval", "[-1.206, -1.196]"
)
# Build table
latex_table <- table_data_wald %>%
gt() %>%
tab_header(
title = md("**Table A1: Wald Test**")
) %>%
cols_label(Statistic = "Statistic", Value = "Value") %>%
tab_options(
table.align = "center",
heading.align = "center",
column_labels.font.size = px(11),
table.font.size = px(11),
table.width = pct(70),
footnotes.font.size = px(9),
footnotes.multiline = TRUE
) |>
as_latex()
latex_table[[1]] <- sub(
"\\begin{table}",
"\\begin{table}\\centering",
latex_table[[1]],
fixed = TRUE
)
latex_table
```