You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Quarto生成PDF时如何避免gt表格跨页显示?

解决Quarto中gt表格跨页拆分的问题

针对你遇到的gt表格被跨页拆分的情况,可通过以下几种方式设置,确保表格在当前页面空间不足时自动跳转到新页面完整显示:

1. 单个表格的代码块级设置

在你的R代码块选项中添加LaTeX表格环境参数,强制表格完整显示:

{r}
#| tbl-cap: Average load factor and visitor arrivals by year
#| label: tbl-monthly
#| echo: false
#| warning: false
#| tbl-latex-env: "table[ht!]"  # 让LaTeX优先在当前位置放置表格,空间不足则跳页
#| tbl-latex-options: "H"       # 强制表格固定在逻辑位置,无足够空间时直接跳至下一页

summary_table_lf <- load_factor %>%
   group_by(year(Date)) %>% 
   summarise_all(mean) %>% 
   rename(`Avg Load (000)`= load_factor_nominal,
         `Avg Load (%)`= load_fact_percent,
         `Avg Visitors`= visitors,
         `Avg Locals`= locals,
         `Google Search`=google_search) %>% 
   select(-Date) %>% 
   gt() %>% 
      fmt_number(columns = 2:6, decimals = 0) %>% 
      fmt_percent(columns = 3, decimals = 1) %>% 
      cols_align(align = 'center', columns=everything()) %>% 
        tab_header(
           title = md("**Country Average Load Factor and Visitor Data**"),
           subtitle = md("*monthly*"  )) %>% 
             tab_source_note(source_note = 'Source: Enter name here') %>% 
             tab_footnote(footnote = 'Not a full year end August 2023',
                   locations = cells_body(column=1, rows = 10)) %>% 
             tab_style(style = list(cell_text(weight = "bold")),
                   locations=cells_column_labels(everything())) %>% 
             opt_table_lines(extent = 'default') %>% 
             tab_options(
                   column_labels.border.bottom.width = px(3),
                   column_labels.border.top.width = px(3),
                   column_labels.border.bottom.color = 'black',
                   column_labels.border.top.color='black',
                   footnotes.border.bottom.color = 'black',
                   row_group.border.bottom.color  = 'black'
          )

summary_table_lf 
  • tbl-latex-env: "table[ht!]":LaTeX浮动选项,h表示当前位置,t表示页面顶部,!表示优先遵循该规则
  • tbl-latex-options: "H":依赖LaTeX的float包(Quarto默认已包含),强制表格放置在代码对应的位置,若当前页面空间不足,直接跳转到新页面完整显示

2. 全局YAML配置(所有表格生效)

如果希望所有表格都遵循该规则,可在文档YAML头部添加全局设置:

title: "My report"
author: "Author name here"
date: '26 September 2023'
format:
  pdf:
    toc: true
    number-sections: true
    colorlinks: true
    lof: true
    lot: true
    papersize: 'a4'
    table:
      latex_env: "table[ht!]"
      latex_options: "H"
editor: visual

3. 通过gt包+KableExtra控制输出

若上述方法无效,可将gt表格转换为LaTeX格式后,用kableExtra强制保持表格完整性:

{r}
#| tbl-cap: Average load factor and visitor arrivals by year
#| label: tbl-monthly
#| echo: false
#| warning: false
#| message: false

library(kableExtra)

summary_table_lf <- load_factor %>%
   group_by(year(Date)) %>% 
   summarise_all(mean) %>% 
   rename(`Avg Load (000)`= load_factor_nominal,
         `Avg Load (%)`= load_fact_percent,
         `Avg Visitors`= visitors,
         `Avg Locals`= locals,
         `Google Search`=google_search) %>% 
   select(-Date) %>% 
   gt() %>% 
      fmt_number(columns = 2:6, decimals = 0) %>% 
      fmt_percent(columns = 3, decimals = 1) %>% 
      cols_align(align = 'center', columns=everything()) %>% 
        tab_header(
           title = md("**Country Average Load Factor and Visitor Data**"),
           subtitle = md("*monthly*"  )) %>% 
             tab_source_note(source_note = 'Source: Enter name here') %>% 
             tab_footnote(footnote = 'Not a full year end August 2023',
                   locations = cells_body(column=1, rows = 10)) %>% 
             tab_style(style = list(cell_text(weight = "bold")),
                   locations=cells_column_labels(everything())) %>% 
             opt_table_lines(extent = 'default') %>% 
             tab_options(
                   column_labels.border.bottom.width = px(3),
                   column_labels.border.top.width = px(3),
                   column_labels.border.bottom.color = 'black',
                   column_labels.border.top.color='black',
                   footnotes.border.bottom.color = 'black',
                   row_group.border.bottom.color  = 'black'
          ) %>%
   as_latex() %>%
   kable_styling(latex_options = c("hold_position", "scale_down"))  # scale_down自动缩小表格适配页面宽度

summary_table_lf 

补充说明

你之前尝试的out.width='70%'仅控制表格显示宽度,无法解决跨页拆分问题——gt默认在PDF输出时使用longtable环境(支持表格跨页),上述方法的核心是将表格切换为普通table环境,并通过LaTeX参数强制表格完整显示在一页。

内容的提问来源于stack exchange,提问作者ianaj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 16:34:56