为什么 gt table 在 Quarto PDF 输出中将表格放在错误的位置?

问题描述 投票:0回答:1

我正在编写一个简短的演示文稿,介绍使用四开渲染为 PDF 的动态与静态内容的不同输出选项。使用

gt
表包时,我在部分标题文本和描述之后有表的代码块。但是,在渲染为 PDF 时,表格会在章节标题文本之前错误地打印。在源代码中,节标题位于表代码之前。我不明白为什么
gt
表继续打印在错误的位置。

下面是重现该问题的简短示例。在示例中,部分

### Population Table
应该打印在
gt
表之前,但事实并非如此。我在其他几个四开本项目中也遇到过这个问题。

---
title: "gt table testing"
format: 
  pdf:
    geometry: # sets page margins for 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
---


```{r}
#| label: Setup

pacman::p_load(
  gt, # table package
  sf, # for working with geospatial data
  spData, # contains the nz dataset for making maps
  tidyverse, # data analysis metapackage containing other packages
  tmap # mapping package
)

# Eliminate scientific notation
options(scipen = 999)
```

## Static Map - PDF  

Static maps present useful information but lack interactivity like a dynamic map. Check it out!  
```{r}
#| label: Static map
#| fig-height: 6

# Set map to static plot mode
tmap_mode("plot")

# View map
tm_shape(nz) +
  tm_polygons(
    col = 'Population', # color by population count
    alpha = 0.8, # change transparency of colors
    breaks = seq(0, 2000000, by = 250000) # set breakpoints for population count
    ) +
  tm_layout(
    main.title = "Static Map of New Zealand's Population",
    main.title.size = 1
  ) +
  tm_text("Name", size = 0.6, col = "black")
```

### Population Table  

Dynamic maps can contain popups that users can click to see more information. With a static map that cannot have dynamic popups, adding a table with more information can help make the static map more understandable.   

```{r}
#| 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 = "Population of New Zealand by Region",
    subtitle = "Grouped by Island (North/South)"
  ) %>% 
   tab_style(
     style = list(
       cell_fill("royalblue2"),
       cell_text("white")
       ),
     locations = cells_row_groups()
     ) %>% 
  fmt_number(columns = "Population", decimals = 0)
```

## New Section Topic  

Anything after the table change things?  

会话和版本信息: 平台 x86_64-w64-mingw32
拱门x86_64
操作系统 mingw32
crt ucrt
系统 x86_64,mingw32
状态
主要4
小4.2
2024 年
10 个月
第 31 天
svn 修订版 87279
语言 R
version.string R 版本 4.4.2 (2024-10-31 ucrt) 昵称 一堆树叶

四开版: 1.6.39

latex quarto gt
1个回答
0
投票

这是因为表格获得了默认的浮动选项,该选项将其置于页面顶部。文档建议了一种解决方案(来自

?gt::tab_options
):

latex.tbl.pos 指定乳胶浮动位置

浮动环境的乳胶位置指示器(例如“!t”, “H”)。指定时应不带方括号。四开本用户 应该设置代码块内的浮动位置 参数 tbl-pos。输出表只会浮动,如果 Latex.use_longtable = FALSE。

但是,正如最近在 quarto-dev/quarto-cli#4849 中讨论的那样,

tbl-pos
还需要使用
layout
。因此,您可以使用以下内容将代码块包装在
div
中,并且表格将放置在“此处”:

::: {#tbl-t1 tbl-pos="h" layout-ncol="1"}

```{r}
#| label: gt table of population
nz %>% 
  st_drop_geometry() %>% 
  ...
```

:::

enter image description here

---
title: "gt table testing"
format: 
  pdf:
    geometry: # sets page margins for 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
keep-tex: true
---


```{r}
#| label: Setup

pacman::p_load(
  gt, # table package
  sf, # for working with geospatial data
  spData, # contains the nz dataset for making maps
  tidyverse, # data analysis metapackage containing other packages
  tmap # mapping package
)

# Eliminate scientific notation
options(scipen = 999)
```

## Static Map - PDF  

Static maps present useful information but lack interactivity like a dynamic map. Check it out!  
```{r}
#| label: Static map
#| fig-height: 6

# Set map to static plot mode
tmap_mode("plot")

# View map
tm_shape(nz) +
  tm_polygons(
    col = 'Population', # color by population count
    alpha = 0.8, # change transparency of colors
    breaks = seq(0, 2000000, by = 250000) # set breakpoints for population count
    ) +
  tm_layout(
    main.title = "Static Map of New Zealand's Population",
    main.title.size = 1
  ) +
  tm_text("Name", size = 0.6, col = "black")
```

### Population Table  

Dynamic maps can contain popups that users can click to see more information. With a static map that cannot have dynamic popups, adding a table with more information can help make the static map more understandable.   

::: {#tbl-t1 tbl-pos="h" layout-ncol="1"}

```{r}
#| 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 = "Population of New Zealand by Region",
    subtitle = "Grouped by Island (North/South)"
  ) %>% 
   tab_style(
     style = list(
       cell_fill("royalblue2"),
       cell_text("white")
       ),
     locations = cells_row_groups()
     ) %>% 
  fmt_number(columns = "Population", decimals = 0)
```

:::

## New Section Topic  

Anything after the table change things?  
© www.soinside.com 2019 - 2024. All rights reserved.