如何用officedown实现左栏动态项目符号+右栏地图的分栏布局?
批量生成Word报告的分栏布局问题与解决方案
需求背景
正在搭建批量生成两页报告并合并为单个Word文档的流程,曾用gitbook实现类似功能,现改用officedown,目标实现:
- 左栏:编程生成的动态项目符号列表
- 右栏:带标题的地图
当前代码
index.Rmd
--- title: ' ' site: bookdown::bookdown_site documentclass: book biblio-style: apalike link-citations: true --- ```{r include=FALSE} # load libraries and setup environment #### library(dplyr) library(here) library(knitr) library(magick) library(officedown) library(officer) library(rmarkdown) reference_font <- officer::fp_text(font.size = 10, font.family = "Arial", vertical.align = 'superscript')
### 01-multicolumn-lists.Rmd ```r --- title: "MultiColumn Layout" site: bookdown::bookdown_site documentclass: book biblio-style: apalike link-citations: true --- ```{r include=FALSE} # Summary Information # current_lha <- "REPLACE_ME1" current_lha <- "My Region of Interest" img.file <- file.path( R.home("doc"), "html", "logo.jpg" ) # health_master_subset <- health_master %>% # filter(chsa_name == current_lha) # # lha_title <- health_master_subset %>% pull(lha_title) important_num <- sprintf("%.1f%%", 18.756) population_statement <- paste0("Usually this is ", "a lot more complicated and will reference numbers such as ", important_num, ".") pop_projection_statement <- paste("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla eu pulvinar arcu, quis aliquam dui. In at cursus ante. Vestibulum non sagittis lacus. Duis vitae iaculis dui.") fig_caption <- paste0("Figure 1: Location of ", current_lha, " which looks very nice this time of year.")
Community Profile: LHA
r current_lha 1 Population Profile
Demographics
fpar(population_statement, ftext("2", prop = reference_font), pop_projection_statement, ftext("2", prop = reference_font), run_columnbreak(), ftext(fig_caption), external_img(src = img.file, width = 3.5, height = 4.5) )
r run_pagebreak()
2 Population Profile
Demographics
r fpar(population_statement, ftext("2", prop = reference_font))r fpar(pop_projection_statement, ftext("2", prop = reference_font))r run_columnbreak()
fpar( ftext(fig_caption), external_img(src = img.file, width = 3.5, height = 4.5), fp_p = fp_par(line_spacing = 1, word_style = "No Spacing") )
## 遇到的问题 1. 左栏无法显示项目符号,触发bookdown默认ol/ul不存在的错误,移除yml相关配置后仍未解决 2. 两种尝试布局均异常:第一种右栏内容下移一行;第二种用RMarkdown写项目符号的方式布局混乱 ## 解决方案 ### 1. 修复左栏项目符号显示问题 officedown分栏中不能直接混用RMarkdown原生列表语法,需用`officer`构建带项目符号的段落: #### 步骤1:定义项目符号段落格式 ```r # 定义项目符号的段落属性 bullet_par <- officer::fp_par( text.align = "left", line_spacing = 1, list_style_type = "bullet", # 指定项目符号类型 level = 1, margin = officer::fp_margin(0, 0, 0, 0) # 消除默认边距 )
步骤2:构建带项目符号的列表项
# 生成第一个列表项 item1 <- fpar( ftext(population_statement), ftext("2", prop = reference_font), fp_p = bullet_par ) # 生成第二个列表项 item2 <- fpar( ftext(pop_projection_statement), ftext("2", prop = reference_font), fp_p = bullet_par )
2. 修复分栏布局混乱与内容偏移
严格用officedown分栏标签包裹officer生成的内容,避免混用RMarkdown原生内容,同时调整段落格式消除偏移:
修正后的分栏代码
<!---BLOCK_MULTICOL_START---> ```{r echo=FALSE} # 左栏:输出带项目符号的列表 fpar(item1, item2, run_columnbreak()) # 右栏:输出带标题的图片,消除默认边距 fpar( ftext(fig_caption, prop = officer::fp_text(font.size = 10, font.family = "Arial")), external_img(src = img.file, width = 3.5, height = 4.5), fp_p = officer::fp_par( line_spacing = 1, margin = officer::fp_margin(0, 0, 0, 0) ) )
### 3. 配置文件优化 移除所有Rmd文件中的`documentclass: book`配置,在`_output.yml`中设置officedown专用输出格式: ```yaml bookdown::word_document2: base_format: officedown::rdocx_document toc: false number_sections: false
内容的提问来源于stack exchange,提问作者PhDavey
相关产品推荐
相关产品推荐

