Skip to content

unexpected behavior in docx_set_settings() #712

@adcascone

Description

@adcascone

Hello,

Thanks for creating and maintaining this package! I was testing out some of the code included in the reference manual for docx_set_settings() and have a couple of questions. This is the code I ran, which is copy/pasted from pg. 56-57 of the manual:

library(officer)

txt_lorem <- rep("Purus lectus eros metus turpis mattis platea praesent sed. ", 50)
txt_lorem <- paste0(txt_lorem, collapse = "")

header_first <- block_list(fpar(ftext("text for first page header")))
header_even <- block_list(fpar(ftext("text for even page header")))
header_default <- block_list(fpar(ftext("text for default page header")))
footer_first <- block_list(fpar(ftext("text for first page footer")))
footer_even <- block_list(fpar(ftext("text for even page footer")))
footer_default <- block_list(fpar(ftext("text for default page footer")))

ps <- prop_section(
header_default = header_default,
footer_default = footer_default,
header_first = header_first,
footer_first = footer_first,
header_even = header_even,
footer_even = footer_even
)

x <- read_docx()
x <- docx_set_settings(
x = x,
zoom = 2,
list_separator = ",",
even_and_odd_headers = TRUE
)

for (i in 1:20) {
x <- body_add_par(x, value = txt_lorem)
}
x <- body_set_default_section(
x,
value = ps
)
print(x, target = tempfile(fileext = ".docx"))

From this, I would have expected a document that has a zoom factor of 2 (e.g., 200%), a list separator that is a comma, and different headers used for even and odd pages.

I can clearly see via the output that the headers for even/odd pages are different, but the zoom factor doesn't change from the Microsoft Word default, which is 100%. I also cannot verify that there has been any change to the list separator used. From this, two questions arose:

  1. Does this indicate that there may be a bug related to the zoom argument here?
  2. How can the list separator used in a document be verified (e.g., what settings could be checked in Microsoft Word)?

To further probe whether the function's arguments were indeed changing document settings as I expected, I tried to model your test code for this function: https://github.com/davidgohel/officer/blob/master/tests/testthat/test-docx_settings.R

Here's the code and the output:

doc <- read_docx()
doc <- docx_set_settings(
  x = doc,
  zoom = 5, 
  default_tab_stop = 2.5, 
  hyphenation_zone = 0.125, 
  decimal_symbol = "+",
  list_separator = "--",
  even_and_odd_headers = TRUE,
  auto_hyphenation = TRUE,
  compatibility_mode = "4",
  unit = "mm"
)
tmp = paste0(tempfile(), ".docx")
print(x = doc, target = tmp)

doc.tmp <- read_docx(path = tmp)

actual.settings <- data.frame(zoom = doc.tmp$settings$zoom,
                              default_tab_stop = doc.tmp$settings$default_tab_stop,
                              hyphenation_zone = doc.tmp$settings$hyphenation_zone,
                              decimal_symbol = doc.tmp$settings$decimal_symbol,
                              list_separator = doc.tmp$settings$list_separator,
                              even_and_odd_headers = doc.tmp$settings$even_and_odd_headers,
                              auto_hyphenation = doc.tmp$settings$auto_hyphenation,
                              compatibility_mode = doc.tmp$settings$compatibility_mode)

expected.settings <- data.frame(zoom =5, 
                                default_tab_stop = 2.5*0.0393701, #multiplied by this value since I specified "mm" as the unit
                                hyphenation_zone = 0.125*0.0393701, 
                                decimal_symbol = "+",
                                list_separator = "--",
                                even_and_odd_headers = TRUE,
                                auto_hyphenation = TRUE, 
                                compatibility_mode = "4")

testthat::expect_equal(actual.settings, expected.settings)                              

Image

From this, I realized:

  • The mm-to-inch conversion is off, since default_tab_stop and hyphenation_zone arguments did not match the inch-to-mm conversion I did in the expected.settings object. Is this a bug or is this the intended behavior?
  • The compatability_mode argument isn't being recognized. The expected value, based on what I set in the function call, should be "4", but it's still "15". Is this a bug or is this the intended behavior?
  • The zoom argument seems to be recognized here, even if the output .docx file doesn't reflect that. This led me to wonder: What is the best way to confirm that options being set programmatically via the docx_set_settings() function are actually being set in output Microsoft Word documents? Even if some of these parameters return the values that were set in the function call (e.g., zoom), that doesn't mean the output documents actually reflect the desired settings.

I appreciate your time and look forward to hearing your thoughts on the above.

Arianna C.

R version 4.5.2 (2025-10-31)
Platform: x86_64-pc-linux-gnu
Running under: Rocky Linux 9.7 (Blue Onyx)

Matrix products: default
BLAS:   /opt/R/4.5.2/lib64/R/lib/libRblas.so 
LAPACK: /opt/R/4.5.2/lib64/R/lib/libRlapack.so;  LAPACK version 3.12.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Etc/UTC
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] officer_0.7.1

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5       zip_2.3.3         cli_3.6.5         rlang_1.1.6      
 [5] purrr_1.2.0       pkgload_1.4.1     generics_0.1.4    textshaping_1.0.4
 [9] glue_1.8.0        openssl_2.3.4     askpass_1.2.1     rprojroot_2.1.1  
[13] ragg_1.5.0        brio_1.1.5        tibble_3.3.0      lifecycle_1.0.4  
[17] compiler_4.5.2    dplyr_1.1.4       testthat_3.3.1    pkgconfig_2.0.3  
[21] tidyr_1.3.1       rstudioapi_0.17.1 systemfonts_1.3.1 R6_2.6.1         
[25] tidyselect_1.2.1  pillar_1.11.1     magrittr_2.0.4    uuid_1.2-1       
[29] tools_4.5.2       desc_1.4.3        xml2_1.5.1   

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions