admin管理员组

文章数量:1123194

I'm trying to generate a PDF table using kableExtra in R Markdown where the output PDF dimensions exactly match the table dimensions (no extra white space, no extra text, no page number, nothing else). Currently, the PDF output is default A4 paper.

Here's a minimal reproducible example:

---
output: 
  pdf_document:
    latex_engine: xelatex
---

```{r include=FALSE}
library(kableExtra)
library(dplyr)
\```

```{r echo=FALSE}
# Create sample data
data <- data.frame(
  Category = c("A", "B", "C", "D"),
  Value1 = c(100, 200, 300, 400),
  Value2 = c(10.5, 20.5, 30.5, 40.5),
  Value3 = c("Yes", "No", "Yes", "No")
)

# Create table
data %>%
  kbl(booktabs = TRUE,
      col.names = c("Category", "Value 1", "Value 2", "Value 3"),
      align = "lccc") %>%
  kable_styling(latex_options = "HOLD_position") %>%
  add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 1)) %>%
  column_spec(1, width = "3cm") %>%
  footnote(
    symbol = c("This is a footnote", "Another note"),
    threeparttable = TRUE
  )
\```

Cropping the post-knit pdf is not a solution for many reasons that would take too much time to explain. The objective is really to have the table format directly during the knitting phase.

本文标签: rHow to crop pdf while knitting rmdStack Overflow