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

Quarto生成Word文档:确保图表与标题始终同页的方法

解决Quarto生成Word文档时图表/表格与标题分页的问题

问题说明

使用Quarto创建包含多组带标题的表格、图表的Word文档时,出现图表/表格与对应标题分属不同页面的情况,同时需要确保表格能自动编号、显示标题并支持@tbl-tab1这类引用。

解决方案

1. 开启Quarto内置的"标题与内容同页"选项

在docx格式配置或代码块选项中添加对应参数,强制Quarto将标题和对应的图表/表格绑定:

  • 全局配置:在YAML的format.docx下添加keep-with-next: true,对所有图表/表格生效
  • 单个代码块:给图表chunk添加fig-keep-with-title: true,给表格chunk添加tbl-keep-with-title: true,仅对当前内容生效

2. 针对flextable表格的额外处理

由于flextable生成的表格是独立Word对象,需手动设置表格的keep_with_next属性,确保它和上方标题不分离:
在flextable代码末尾添加set_table_properties(ft, keep_with_next = TRUE)

3. 优化内容尺寸避免分页

  • 调整图表的fig-width和fig-height,确保不超出Word页面可用宽度(默认Word页面宽度约为6.5英寸,避免设置过大的宽度)
  • 调整表格列宽,避免表格过宽被拆分到下一页

修正后的完整代码示例

---
title: "Country Focus on this and that"
format:
  docx:
    toc: false
    number-sections: true
    highlight-style: github
    # 全局开启标题与内容同页
    keep-with-next: true
    
execute:
  echo: false
  warning: false
  message: false
---
{r, scoreboard, echo=FALSE, eval=TRUE}
library(tidyverse, warn.conflicts = FALSE)
library(viridis)
library(flextable)

stat_cases <- structure(list(procedure_name = c("Agriculture Block Exemption Regulation", 
"Fisheries Block Exemption Regulation", "General Block Exemption Regulation", 
"Notified Aid", "Total"), n = c(38L, 3L, 251L, 47L, 339L), percent = c("11.2%", 
"0.9%", "74.0%", "13.9%", "100.0%")), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"), core = structure(list(procedure_name = c("Agriculture Block Exemption Regulation", 
"Fisheries Block Exemption Regulation", "General Block Exemption Regulation", 
"Notified Aid"), n = c(38L, 3L, 251L, 47L), percent = c(11.2, 
0.9, 74, 13.9)), row.names = c(NA, -4L), class = "data.frame"), tabyl_type = "two_way", totals = "row")

stat_cases2 <- structure(list(year = c(2012, 2012, 2012, 2013, 2013, 2013, 2014, 
2014, 2014, 2015, 2015, 2015, 2016, 2016, 2016, 2017, 2017, 2017, 
2018, 2018, 2018, 2019, 2019, 2019, 2020, 2020, 2020, 2021, 2021, 
2021, 2022, 2022, 2022), procedure_name = c("Agriculture Block Exemption Regulation", 
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation", 
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation", 
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation", 
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation", 
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation", 
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation", 
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation", 
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation", 
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation", 
"General Block Exemption Regulation", "Notified Aid", "Agriculture Block Exemption Regulation", 
"General Block Exemption Regulation", "Notified Aid"), expenditure = c(0.031341, 
0.772735, 1.0506364, 0.031725, 0.672124, 1.1587967, 0.027114, 
0.395669, 1.0655565, 0.023264, 0.910199, 0.9733338, 0.132403, 
0.9334881, 0.9164271, 0.1159009, 0.9422837, 0.8899091, 0.1342904, 
1.0357514, 0.7333295, 0.1724899, 1.1234719, 0.7128631, 0.1892858, 
1.265574, 6.1494719, 0.1930364, 1.2223709, 8.573606, 0.207672, 
1.2500287, 5.0596376), expenditure_deflated = c(0.040544631307, 
0.999657179817, 1.359167399796, 0.040209125481, 0.851868187541, 
1.468690367567, 0.033474074075, 0.488480246886, 1.315501851829, 
0.027961538464, 1.09398918266, 1.169872355741, 0.155768235294, 
1.098221294133, 1.078149529462, 0.134611962827, 1.094406155676, 
1.033576190453, 0.152602727269, 1.176990227258, 0.83332897725, 
0.192511049105, 1.253874888393, 0.795606138384, 0.205076706392, 
1.371152762738, 6.662483098824, 0.204487711868, 1.29488442799, 
9.082209745624, 0.207672, 1.2500287, 5.0596376)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -33L))
{r}
#| label: tbl-tab1
#| tbl-cap: 不同程序类型的国家援助措施数量及占比
#| echo: false
#| eval: true
#| tbl-cap-location: top
#| 强制表格与标题同页
#| tbl-keep-with-title: true
year_focus <- 2022

ft <- stat_cases |>
    flextable()  |>
    set_header_labels(procedure_name="程序类型",
                      n="有效措施数量",
                      percent="占总数量比例"
                      )  |>
    theme_zebra()  |>
    fontsize(part = "all", size = 8)  |>
    font(part="all", fontname = "Verdana")  |>
    colformat_double(big.mark = " ")  |>
    width(width = c(2,2,2)) 
ft <- add_header_row(
  x = ft, values = paste(year_focus, "年国家援助措施"),
  colwidths = c(3))
ft <- theme_box(ft)  |>
    align(align="center", part="header") |> 
    align(align = "right",j=c(2,3), part="body") |>
    # 给flextable添加同页属性
    set_table_properties(keep_with_next = TRUE)
ft
{r}
#| label: fig-spending
#| fig-cap: 不同程序类型的国家援助实际支出(固定价格)
#| fig-width: 6.5  # 调整为Word页面可用宽度
#| fig-height: 4
#| echo: false
#| fig-cap-location: top
#| 强制图表与标题同页
#| fig-keep-with-title: true
my_pal <- viridis(4)

ggplot(data = stat_cases2, aes(x  = year, y=expenditure_deflated,
                                      fill=procedure_name)) +
    geom_bar(position=position_dodge2(preserve="single"), stat="identity", alpha=1, color="black")+
    scale_fill_manual(NULL,   values=my_pal)+
    labs(title="国家援助支出(十亿欧元)")+
    xlab(NULL)+
    ylab("国家援助支出(十亿欧元)")+
    guides(fill=guide_legend(position="top"))

现在你可以通过@tbl-tab1引用表格,@fig-spending引用图表,且标题和内容会保持在同一页面。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 12:04:51