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

Quarto PDF输出中gt表格位置错误的技术求助

问题:Quarto渲染PDF时gt表格位置错误前置

使用Quarto渲染PDF制作动态与静态内容输出演示文档时,遇到gt表格位置错位问题:表格代码块明明放在### Population Table章节标题及说明文本之后,但PDF渲染完成后,表格却错误显示在该章节标题之前,且该问题在多个Quarto项目中均出现。

可复现代码

---
title: "gt table testing"
format: 
  pdf:
    geometry: # 设置PDF页面边距
      - top=2.0cm
      - left=1.27cm
      - right=1.27cm
      - bottom=2.5cm
execute:
  echo: false
  message: false
  warning: false
  include: true
editor_options: 
  chunk_output_type: console
---
#| label: Setup

pacman::p_load(
  gt, # 表格工具包
  sf, # 地理空间数据处理工具
  spData, # 包含nz制图数据集
  tidyverse, # 整合型数据分析工具包
  tmap # 制图工具包
)

# 取消科学计数法显示
options(scipen = 999)

静态地图 - PDF

静态地图可展示有效信息,但不具备动态地图的交互能力,示例如下:

#| label: Static map
#| fig-height: 6

# 设置地图为静态绘图模式
tmap_mode("plot")

# 绘制地图
tm_shape(nz) +
  tm_polygons(
    col = 'Population', # 按人口数量着色
    alpha = 0.8, # 调整颜色透明度
    breaks = seq(0, 2000000, by = 250000) # 设置人口数量分段断点
    ) +
  tm_layout(
    main.title = "新西兰人口静态地图",
    main.title.size = 1
  ) +
  tm_text("Name", size = 0.6, col = "black")

人口统计表格

动态地图可设置点击弹窗展示详情,静态地图无法实现该功能,因此补充信息表格能提升静态地图的可读性。

#| label: gt table of population

nz %>% 
  st_drop_geometry() %>% 
  select(Name, Island, Population) %>% 
  arrange(desc(Population)) %>% 
  gt(
    groupname_col = "Island",
    rowname_col = "Name"
    ) %>% 
  tab_header(
    title = "新西兰各地区人口统计",
    subtitle = "按南北岛分组"
  ) %>% 
   tab_style(
     style = list(
       cell_fill("royalblue2"),
       cell_text("white")
       ),
     locations = cells_row_groups()
     ) %>% 
  fmt_number(columns = "Population", decimals = 0)

新章节主题

表格之后的内容是否会影响排版?

环境与版本信息

R版本详情

platform       x86_64-w64-mingw32
arch           x86_64
os             mingw32
crt            ucrt
system         x86_64, mingw32
status
major          4
minor          4.2
year           2024
month          10
day            31
svn rev        87279
language       R
version.string R version 4.4.2 (2024-10-31 ucrt)
nickname       Pile of Leaves

Quarto版本

1.6.39


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 05:43:11