yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.36k stars 873 forks source link

Chunks can make figure captions bold, but not table cations when knitting #2298

Closed GabriellaS-K closed 8 months ago

GabriellaS-K commented 9 months ago

Hello!

I am knitting a document to word, and want the figures and table captions to be structured: as follows: the number of the figure/table. bold summary a bit more about it.

e.g. Figure 1. Graph showing car sale trends. Data produced from 2020 only or this is fine too: Figure 1. Graph showing car sale trends. Data produced from 2020 only

Currently, adding ** before and after the text in the chunk options works for figures, not tables. It works for tables in html but not if you knit tables to word. See the reproducible example below:

---
output:
 bookdown::html_document2:
  number_sections: false
  toc: true
 bookdown::word_document2:
   fig_caption: yes
   tab_caption: yes
---

{r setup, include=FALSE}
knitr::opts_chunk$set(
  echo = FALSE, #chunk code not shown
  warning=FALSE,
  message = FALSE
)

{r cartable, eval=TRUE, tab.id='cartable',tab.cap="**Wow car facts** with abbreviations"}
library(flextable)
flextable(head(mtcars))

{r plot, fig.cap="**Gear type** bars in green show"}
gearType<-table(mtcars$gear)
xVal<-c("Type 3", "Type 4","Type 5")
# Plot the bar chart 
barplot(gearType,names.arg=xVal,xlab="Gear Types",ylab="Total # of each type",col="green",
        main="Distribution Of Gear Types",border="black")

See plot \@ref(fig:plot)

some cool analysis

look at table \@ref(tab:cartable)

By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

cderv commented 9 months ago

Using this in chunk option

tab.id='cartable',tab.cap="**Wow car facts** with abbreviations"

is a feature of flextable directly (https://ardata-fr.github.io/flextable-book/captions-and-cross-references.html#knitr-chunk-options-for-flextable-captions)

You should look for info there like

Also in

 bookdown::word_document2:
   fig_caption: yes
   tab_caption: yes

I don't where tab_caption is coming from. I don't think it has some effect.

Also if you don't know about it you can have a look at Quarto which has better table and cross ref support (https://quarto.org/docs/authoring/tables.html). It is an evolution of R Markdown for broader usage outside of R world. (https://quarto.org/docs/faq/rmarkdown.html)

GabriellaS-K commented 8 months ago

Hi

The tab_caption: option is one I added just to see if it worked, as I had fig_caption: Actually though, using tab.cap and then the table in flextable with the caption there allows me to sort the bold as I like, but still cross referencing in markdown, so it's a good solution!

github-actions[bot] commented 2 months ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.