最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

css - Created consistent caption formatting between figures and flextables in Rmd HTML output - Stack Overflow

matteradmin3PV0评论

I am having trouble getting my captions for figures and tables having consistent formatting when knitting a Rmd file to HTML output from the Bookdown package. I have tried applying different methods from documentation here and various forums posts on stack overflow but none seem to make any difference. A few examples of things I tried without success below. Bonus points for solving the same problem if switching to PDF output.

---
title: "Caption Testing"

output:
  bookdown::html_document2: 
    toc: false
    fig_caption: yes
    number_sections: false
    self_contained: true
    mode: selfcontained
---

<style type="text/css">
  body{
  font-size: 16pt;
  
}
</style>

<style type="text/css">
  .caption{
  font-size: 16pt;
  font-weight: bold
}
</style>

<style type="text/css">
  caption{
  font-size: 16pt;
  font-weight: bold
}
</style>



<style type="caption">
  caption{
  font-size: 16pt;
  font-weight: bold
}
</style>



```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, out.width='100%' )

library(tidyverse)
library(flextable)

set_flextable_defaults(font.size=16)
```
## Placeholder Heading

Want to produce html document that has consistent formatting between figure and table captions.  Multiple approaches applied none seem to have any effect.  I have been avoiding set_caption(as_paragraph(etc...)) as I would prefer to set as a more global option and not having to copy/paste options for each table and consistency with how figure labels are formatted where the second css style code(.caption) gives desired result.

```{r figure, fig.cap="Example ggplot" , fig.asp=1}
mtcars %>%
  
ggplot(aes(cyl, wt) )+geom_point()
```


More text

```{r table1, echo=FALSE}

head(mtcars,10) %>%
  flextable() %>%
    set_caption("No extra formatting")


```

More text



```{r table2, echo=FALSE, tab.cap="Set in chunk options using tab.cap.style=.caption" ,tab.cap.style=".caption"}

head(mtcars,10) %>%
  flextable() 


```
More text

```{r table3, echo=FALSE, tab.cap="Set in chunk options using tab.cap.style=caption" ,tab.cap.style="caption"}

head(mtcars,10) %>%
  flextable() 


```
More text

```{r table4, echo=FALSE }

head(mtcars,10) %>%
  flextable() %>%
    set_caption(caption = "Set in set_caption style=caption", style = "caption")


```

More text

```{r table5, echo=FALSE }

head(mtcars,10) %>%
  flextable() %>%
    set_caption(caption = "Set in set_caption style=.caption", style = ".caption")


```
More text


```{r table6, echo=FALSE }

head(mtcars,10) %>%
  flextable() %>%
    set_caption(caption = "Set in set_caption html_classes=.caption",html_classes = ".caption")


```

I am having trouble getting my captions for figures and tables having consistent formatting when knitting a Rmd file to HTML output from the Bookdown package. I have tried applying different methods from documentation here and various forums posts on stack overflow but none seem to make any difference. A few examples of things I tried without success below. Bonus points for solving the same problem if switching to PDF output.

---
title: "Caption Testing"

output:
  bookdown::html_document2: 
    toc: false
    fig_caption: yes
    number_sections: false
    self_contained: true
    mode: selfcontained
---

<style type="text/css">
  body{
  font-size: 16pt;
  
}
</style>

<style type="text/css">
  .caption{
  font-size: 16pt;
  font-weight: bold
}
</style>

<style type="text/css">
  caption{
  font-size: 16pt;
  font-weight: bold
}
</style>



<style type="caption">
  caption{
  font-size: 16pt;
  font-weight: bold
}
</style>



```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, out.width='100%' )

library(tidyverse)
library(flextable)

set_flextable_defaults(font.size=16)
```
## Placeholder Heading

Want to produce html document that has consistent formatting between figure and table captions.  Multiple approaches applied none seem to have any effect.  I have been avoiding set_caption(as_paragraph(etc...)) as I would prefer to set as a more global option and not having to copy/paste options for each table and consistency with how figure labels are formatted where the second css style code(.caption) gives desired result.

```{r figure, fig.cap="Example ggplot" , fig.asp=1}
mtcars %>%
  
ggplot(aes(cyl, wt) )+geom_point()
```


More text

```{r table1, echo=FALSE}

head(mtcars,10) %>%
  flextable() %>%
    set_caption("No extra formatting")


```

More text



```{r table2, echo=FALSE, tab.cap="Set in chunk options using tab.cap.style=.caption" ,tab.cap.style=".caption"}

head(mtcars,10) %>%
  flextable() 


```
More text

```{r table3, echo=FALSE, tab.cap="Set in chunk options using tab.cap.style=caption" ,tab.cap.style="caption"}

head(mtcars,10) %>%
  flextable() 


```
More text

```{r table4, echo=FALSE }

head(mtcars,10) %>%
  flextable() %>%
    set_caption(caption = "Set in set_caption style=caption", style = "caption")


```

More text

```{r table5, echo=FALSE }

head(mtcars,10) %>%
  flextable() %>%
    set_caption(caption = "Set in set_caption style=.caption", style = ".caption")


```
More text


```{r table6, echo=FALSE }

head(mtcars,10) %>%
  flextable() %>%
    set_caption(caption = "Set in set_caption html_classes=.caption",html_classes = ".caption")


```
Share Improve this question edited Apr 14 at 15:33 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked Nov 15, 2024 at 20:04 BEVANBEVAN 6353 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2 +50

Your custom CSS styles will not be applied to the flextable captions because flextables are rendered in the Shadow DOM, which uses its own separate CSS styles.

However, you can append custom CSS style to a flextable instance using

ft <- flextable::set_table_properties(
  x = ft, 
  opts_html = list(
    extra_css = 'caption {
      font-size: 16pt;
      font-weight: bold;
    }'
  )
)

Then this ft will be rendered with 16pt bold caption text in bookdown html_document.

To apply custom CSS styles to all flextables, use set_flextable_defaults(extra_css = ...):

set_flextable_defaults(extra_css = 'caption {
  font-size: 16pt;
  font-weight: bold;
}')
Post a comment

comment list (0)

  1. No comments so far