Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table caption style in yaml not used #112

Open
jessekps opened this issue May 9, 2023 · 4 comments
Open

table caption style in yaml not used #112

jessekps opened this issue May 9, 2023 · 4 comments

Comments

@jessekps
Copy link

jessekps commented May 9, 2023

I have an rmarkdown document:

---
output: 
  officedown::rdocx_document:
    reference_docx: "C:/Rlibs/officedown/examples/bookdown/template.docx"
    tables:
      caption:
        style: caption
        pre: 'Tabel'
        tnd: 1
        tns: '.'
    plots:
      topcaption: true
      caption:
        style: caption
        pre: 'Figuur '
        tnd: 1
        tns: '.'
---

# a chapter

```{r,tab.cap='This table caption does not look correct', echo=FALSE}

library(officer)
library(flextable)

docx_file <- system.file(
  package = "officedown", "examples",
  "bookdown", "template.docx"
)
doc <- read_docx(docx_file)

fit_to_page = function(flx,landscape=FALSE, pgwidth=ifelse(landscape,24.7,15.98)/2.54)
{
  width(flx, width = dim(flx)$widths*pgwidth /(flextable_dim(flx)$widths))
}

flextable(styles_info(doc)[25:30, 1:6]) |>
  fit_to_page()
```

some text

```{r,fig.cap='This plot caption does look correct',echo=FALSE}
plot(1:10,rnorm(10))
```

It is using the default style reference_docs in your package and specifies to use the style 'caption' for tables and plots. However the captions tyles seem different on output and I think only the plot caption has the correct formatting.

image

It could be that I'm mistaken on how to specify the style or what the style actually does, if so: please enlighten me! And many thanks for making this package.

session info:

## R version 4.2.2 (2022-10-31 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19045)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_Europe.utf8  LC_CTYPE=English_Europe.utf8   
## [3] LC_MONETARY=English_Europe.utf8 LC_NUMERIC=C                   
## [5] LC_TIME=English_Europe.utf8    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] flextable_0.8.4 officer_0.5.2  
## 
## loaded via a namespace (and not attached):
##  [1] zip_2.2.2         Rcpp_1.0.10       compiler_4.2.2    later_1.3.0      
##  [5] base64enc_0.1-3   gfonts_0.2.0      tools_4.2.2       digest_0.6.31    
##  [9] uuid_1.1-0        lifecycle_1.0.3   jsonlite_1.8.4    evaluate_0.20    
## [13] memoise_2.0.1     rlang_1.0.6       shiny_1.7.4       cli_3.6.0        
## [17] rstudioapi_0.14   crul_1.3          curl_5.0.0        yaml_2.3.7       
## [21] officedown_0.3.0  xfun_0.36         fastmap_1.1.0     xml2_1.3.3       
## [25] knitr_1.42        systemfonts_1.0.4 gdtools_0.3.0     askpass_1.1      
## [29] grid_4.2.2        data.table_1.14.6 glue_1.6.2        httpcode_0.3.0   
## [33] R6_2.5.1          rvg_0.3.2         rmarkdown_2.20    magrittr_2.0.3   
## [37] promises_1.2.0.1  htmltools_0.5.4   ellipsis_0.3.2    mime_0.12        
## [41] xtable_1.8-4      httpuv_1.6.8      openssl_2.0.5     cachem_1.0.6     
## [45] crayon_1.5.2

@jessekps
Copy link
Author

jessekps commented May 10, 2023

addition: it is not specifically a problem in the yaml as using

knitr::opts_chunk$set(tab.cap.style='caption')

gives the same result

@alapo
Copy link

alapo commented May 29, 2023

Hello,

This isnt a complete answer to your query but I modified your code a bit for it to be reproducible for others who may want to troubleshoot.

library(officer)
library(flextable)
library(tidyverse)

docx_file <- "styles/template.docx"
doc <- read_docx(docx_file)

fit_to_page = function(flx,landscape=FALSE, pgwidth=ifelse(landscape,24.7,15.98)/2.54)
{
  width(flx, width = dim(flx)$widths*pgwidth /(flextable_dim(flx)$widths))
}

style_idx <- c("Table", "ImageCaption", "TableCaption", "Normal", "FirstParagraph", "TableauNormal") #index the styles we want to see in the table

flextable(
  styles_info(doc) %>% 
    filter(style_id %in% style_idx))  %>% 
  fit_to_page()

Secondly, I have uploaded a template that I have been working on here. Based on the little I know you need to call the "style_name" in the YAML where the "style_type" is "table". In your example you have the following in your YAML

tables:
      caption:
        style: caption
        pre: 'Tabel'
        tnd: 1
        tns: '.'

but if you look at the styles info caption is of style_type paragraph see below

img

If you look at the template.docx I uploaded here you could use the following YAML which will apply a style to the table, but you'll need to select the entire table and click on MDPI_4.2_table_body to apply a paragraph style to the table...as far as I know this can't be accomplished with YAML but would be nice to see if anyone else can add to the solution

output: 
  officedown::rdocx_document:
    #reference_docx: styles/mdpi-brain-science.docx
    reference_docx: styles/template.docx
    #mapstyles:
      #'MDPI_3.1_text': ['Normal']
      #Normal: ['MDPI_3.1_text']
    tables:
      style: MDPI_4.1_three_line_table
        #style: M_deck_5_table_body_three_lines
      layout: autofit
      width: 1.0
[template.docx](https://github.com/davidgohel/officedown/files/11592776/template.docx)

      caption:
        style: MDPI_4.1_table_caption
        pre: 'Table '
        sep: '. '

@woyzeck1978
Copy link

Hi Jesse, I'm encountering the same issue. Have you found a solution?

@jessekps
Copy link
Author

No, I never did. I use a simple workaround to style the table that I don't really mind using, so I stopped trying to get it to work.

is_word = \() coalesce(knitr::pandoc_to(),'') == 'docx'

word_things = function(flx)
{
  if(is_word())
  {
    flx |>
      fontsize(size = 10, part = "all") |>
      hrule(part='all',rule='atleast') |>
      height(part='body',height=0.3) |>
      font(part='all',fontname='Calibri')
  } else 
  {
    flx
  }
}

flextable(...) |> ... |> word_things()

Table caption styles sort of work out of the box but seem always to get centered, I don't know why, maybe I'm overlooking something, but I use the following ugly hack for that:

knitr::knit_hooks$set(document = function(x){
  gsub('w:val="Tabeltitel"/><w:jc w:val="center"/>','w:val="Tabeltitel"/><w:jc w:val="left"/>',x,fixed=TRUE)
})

where "Tabeltitel" is a style defined in my word file.

I feel I must add that overall I think the package is fantastic and I know of no other way for getting acceptable word output, so I gladly live with or work around these small flaws.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants