Open jessekps opened 1 year ago
addition: it is not specifically a problem in the yaml as using
knitr::opts_chunk$set(tab.cap.style='caption')
gives the same result
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
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: '. '
Hi Jesse, I'm encountering the same issue. Have you found a solution?
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.
I have an rmarkdown document:
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.
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: