flextable
flextable copied to clipboard
Feature request: add option to left-align caption
Per default, flextable centers the table caption.
library(flextable)
head(mtcars) |>
flextable() |>
set_caption("Title")
Created on 2022-07-20 by the reprex package (v2.0.1)
It would be nice to be able to left-align the title (also possibly italicize), as per APA style recommendations. However, there does not seem to be an argument within set_caption to specify alignment (left, center, right). Even using "all" in align does not seem to do the trick.
library(flextable)
head(mtcars) |>
flextable() |>
set_caption("Title") |>
align(align = "left", part = "all")
Created on 2022-07-20 by the reprex package (v2.0.1)
Is there another way to do this?
Hello,
Can you read the documentation here (https://ardata-fr.github.io/flextable-book/captions-and-cross-references.html#caption-appearance) and let me know if you still have question? Also, I am not sure if you are working in HTML, or Word or PDF. It would help to let me provide an answer with code.
I am hearing APA everywhere, would an APA theme be interesting or even possible in your opinion? I quickly read https://apastyle.apa.org/style-grammar-guidelines/tables-figures/tables. Is that all I should know about? Sorry for the unclear question, I am just trying to guess if I can do something as APA is a recurrent theme with flextable, and I am not really aware of what it is.
Thank you. I read through the help page for set_caption as well as the book section you provided. Unfortunately, I am not sure it can help for this specific issue. The help recommends reading officer::styles_info() to see available styles, which are type = c("paragraph", "character", "table", "numbering"). However, I am not sure those would allow left-aligning the title.
APA style is the style recommended by the American Psychological Association. It is used by everyone in the psychological sciences, but also in many other social science fields. That's a lot of people. Competing styles include MLA and Chicago styles (or AMA for medicine). It is mostly used for scientific publishing, so most people (including myself, though not all) will use Word (although, in my case, I additionally use HTML for tutorials and rmarkdown reports).
I agree with you that it would be very nice indeed to have at least a basic APA theme. I think people would appreciate. In fact, this is what I've attempted to do with my own convenience nice_table function in my rempsyc package (https://remi-theriault.com/blog_table). In addition to base formatting, my function deals with statistical values, symbols, and decimals.
However I do think that a basic flextable APA theme could at least deal with the basic formatting (and I would use it in my own function to simplify code). That would essentially involve using Times New Roman size 12, only some horizontal lines, global centering, and double-spacing (though to be fair instead of using double-space in my function I use height = 0.55 as it looks better while creating the same spacing). So yes, the APA link you provided covers most of it.
The screenshots I have provided were the HTML preview in RStudio. However, now that you mention it, I realize that the proper title formatting isn't preserved once exported to Word. This is how it looks (and should look) inside the RStudio previewer in flextable version 0.7.2. Note that in version 0.7.2, the HTML preview shows a large space between the table and the title (which is undesirable), which wasn't there minutes ago before I updated from 0.7.0 to 0.7.2.
library(flextable)
#> Warning: package 'flextable' was built under R version 4.2.1
head(mtcars) |>
flextable() |>
set_caption("Title") |>
font(part = "all", fontname = "Times New Roman") -> test
test

save_as_docx(test, path = "test.docx")
Created on 2022-07-20 by the reprex package (v2.0.1)
Whereas when opened in Word, we can see the Title isn't Times New Roman anymore (it is Cambria), and is bold:

In the meanwhile, here is a rather hacky workaround by my friend @Buedenbender:
library(flextable)
#> Warning: package 'flextable' was built under R version 4.2.1
table_caption <- c("Table 1", "This is a pretty title")
head(mtcars) |>
flextable() |>
add_header_lines(values = rev(table_caption)) |>
bold(part = "header", i = 1) |>
italic(part = "header", i = c(2:length(table_caption))) |>
align(part = "header", i = c(1:length(table_caption)), align = "left") |>
border(part = "head", i = c(1:length(table_caption)),
border = list("width" = 0, color = "black", style = "solid"))
Created on 2022-07-21 by the reprex package (v2.0.1)
And the neat thing is that it also preserves its formatting properties when exported to Word.
I am not sure it can help for this specific issue
You need to understand what are styles in Word. If you use a style that is left aligned, the paragraph is left aligned, that's how Word is working, associate paragraphs, text, tables, and lists with a given style.
When working with officer, it is important to understand what template you are using and eventually use a specific one. Styles are located in templates. See https://ardata-fr.github.io/officeverse/office-documents-generation.html#templates
Later in this section, you should find a part explaining styles https://ardata-fr.github.io/officeverse/office-documents-generation.html#word-document-styles
rempsyc [...]
OK, cool! Thanks for sharing. It looks good (the data types are preserved, thus it let users do conditional formatting, it's great!). One comment, I would not use height=.5 if you sometimes use set_table_properties(layout="autofit"). I will read a little about APA and try to suggest something if possible! I am ok to do a theme, sure. I will open a discussion about that and @ you.
space between table and caption
This is new yes, but I don't think it is coming from flextable. Probably about an R Markdown change or knitr. I will open an issue about that, thanks for showing it (I answered about that on SO few weeks ago but did not realize how annoying it was).
@rempsyc @Buedenbender, it's not hacky but it's not a real caption. In Word, HTML and PDF it can't be cross-referenced. But if you don't need cross-reference, it's totally ok. You can add as many lines/rows in the footer or the header part (also in the body since latest version)
I am not sure it can help for this specific issue
You need to understand what are styles in Word. If you use a style that is left aligned, the paragraph is left aligned, that's how Word is working, associate paragraphs, text, tables, and lists with a given style.
When working with officer, it is important to understand what template you are using and eventually use a specific one. Styles are located in templates. See https://ardata-fr.github.io/officeverse/office-documents-generation.html#templates
Later in this section, you should find a part explaining styles https://ardata-fr.github.io/officeverse/office-documents-generation.html#word-document-styles
rempsyc [...]
OK, cool! Thanks for sharing. It looks good (the data types are preserved, thus it let users do conditional formatting, it's great!). One comment, I would not use height=.5 if you sometimes use set_table_properties(layout="autofit"). I will read a little about APA and try to suggest something if possible! I am ok to do a theme, sure. I will open a discussion about that and @ you.
space between table and caption
This is new yes, but I don't think it is coming from flextable. Probably about an R Markdown change or knitr. I will open an issue about that, thanks for showing it (I answered about that on SO few weeks ago but did not realize how annoying it was).
I am more than happy to contribute as well just @ me to the discussion (also if desired, I can provide excerpts from the Publication Manual 7th edition) despite I think that the website you referenced should contain the most important details.
Thank you for the links and additional details. I see what you mean now; when I open the Word document, the reason it is Cambria, bold, and italicized, is that it has the "Table Caption" Style selected. This is why I was not seeing any difference when changing the style option within set_caption. I had to change it from Word directly. So it is behaving correctly.
Although in theory, Word-savvy people who use styles properly could customize their default Table Caption style, I think there is an accessibility barrier. I know few, if any, people who use Word styles, let alone Table Caption style... Still, from an endpoint user, it would be feasible and reasonable to learn this.
However, because I integrate flextable in my own package and advertise it as a "convenience" function, this is one of the things I would like to take care of for the users so they don't have to mess with the styles if they don't use that feature. In my case, I think it would be reasonable to resort to using the workaround described above, as I think a proper default output is more important than working cross-references.
Because from what I understand, it would not be possible to have BOTH custom formatting AND cross-references if it is using the Table Caption style because that would need to overwrite the user's default Table Caption style (which would be undesirable). If it would though, one possibility would be to add formatting arguments to set_caption. But I wonder if it would not make more sense to add "caption" as an option in other formatting options (italic, bold, align, etc.), so we would have:
part: partname of the table (one of 'all', 'body', 'header', 'footer', 'caption')
Finally, I am happy to take part in the discussion about the APA theme. Would it be useful for me to provide a basic working example of the desired output format? I can do this in the discussion once you tag me.
(Also, thanks for the compliments and comment about height=.5! I will investigate the matter! And also very neat that you can add rows in the body of the table in the newest version now! I was looking for this feature some time ago!)
Hello @rempsyc
Thanks for the explanations about why we need to have a better system to define paragraph styles. It makes sense!
This is now implemented (not for PDF, maybe later if it is possible...).
Here is a quarto example. It shows new options for captions:
- captions are now supporting simple text or a call to
as_paragraph(), you can create complex captions (with color, bold, etc.) - a new argument
fp_pis available, it expects anofficer::fp_par()objects where you can define borders, paddings and alignment. - by default alignment is the same as the table alignment, but it can be changed by using
align_with_table=FALSE, in that case, the alignment will not be overwritten by the table alignment.
---
title: "Untitled"
format:
docx:
keep-md: false
---
```{r include=FALSE}
library(flextable)
library(officer)
knitr::opts_chunk$set(echo = FALSE)
fpp <- fp_par(text.align = "left", padding = 3)
```
blah blah blah `r run_reference(id = "tbl-flextable-1")`.
```{r}
#| label: tbl-flextable-1
#| tbl-cap: dummy
#| ft.align: right
ft <- flextable(mtcars[1:4, 1:4])
ft |>
set_caption(
caption = as_paragraph(
"Caption ",
as_chunk("strong", props = fp_text_default(bold = TRUE))
),
align_with_table = FALSE,
word_stylename = "Table Caption",
fp_p = fpp
)
```
```{r}
#| label: tbl-flextable-2
#| tbl-cap: dummy
ft |>
set_caption(
caption = as_paragraph(
"Caption ",
as_chunk("strong", props = fp_text_default(bold = TRUE))
),
align_with_table = TRUE, # default
word_stylename = "Table Caption",
fp_p = fpp
)
```
In HTML the rendering is slightly different as the caption is part of the table (in Word usual captions are simple paragraphs located before or after the table).
To try it, you will need to update package 'officer' (github version only for now). The required version of officer should be >= 0.4.4.006.
remotes::install_github("davidgohel/officer")
remotes::install_github("davidgohel/flextable")
Note also fp_p can't be supported with 'bookdown' (because of cross-references!)
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.