I am creating a complex word document with quarto. It contains several tables and figures with captions. In some cases, I end up with a figure/table on one page while the corresponding caption is on another page? How can I fix that? Below I give a simple script which does not exhibit the problem, but showcases the main features of my word document.Any suggestion is appreciated!
---title: "Country Focus on this and that"format: docx: toc: false number-sections: true highlight-style: githubexecute: 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: Number and share of State aid measures by type of procedure.#| echo: false#| eval: true#| tbl-cap-location: topyear_focus <- 2022ft <- stat_cases |> flextable() |> set_header_labels(procedure_name="Type of Procedure", n="Number of Active Measures", percent="Share of Total" ) |> theme_zebra() |> fontsize(part = "all", size = 8) |> font(part="all", fontname = "Verdana") |> colformat_double(big.mark = " ") |> ## autofit() ## %>% width(width = c(2,2,2)) ft <- add_header_row( x = ft, values = paste("State Aid Measures in ",year_focus, sep=""), colwidths = c(3))ft <- theme_box(ft) |> align(align="center", part="header") |> align(align = "right",j=c(2,3), part="body") ## |>ft```I would like the table above to be numbered, to show its caption and Iwould like to be able to reference it as @tbl-tab1.```{r}#| label: fig-spending#| fig-cap: State aid expenditure at constant prices by type of procedure.#| fig-width: 12#| fig-height: 6#| echo: false#| fig-cap-location: topmy_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=paste("State aid spending in billion EUR "))+ xlab(NULL)+ ylab("State aid expenditure (EUR bn)")+ guides(fill=guide_legend(## nrow=1,byrow=TRUE, position="top"))```I reference it as @fig-spending.