officer icon indicating copy to clipboard operation
officer copied to clipboard

Alternative text for tables with chunk option

Open camitz opened this issue 3 years ago • 4 comments

As in davidgohel/officedown#53, but for tables. Same motivation, accessibility.

I haven't looked into much and as in pr davidgohel/officedown#53, I expect I'll make an attempt, although I find myself stressed for time at this moment....

table.alt?

camitz avatar May 21 '21 14:05 camitz

I didn't know it was possible. OK!

davidgohel avatar May 25 '21 08:05 davidgohel

Hey @davidgohel !

Friday afternoons are great for inspiration, don't you think? :)

I've made a hook that does the trick. Do you have any comments on names or anything else before I proceed with a pull req?

```{r setup, include=FALSE}
library(knitr)
default_source_hook <- knit_hooks$get('chunk')

knit_hooks$set(
  chunk = function(x, options) {
    if(is.character(options$tab.alt) || is.character(options$tab.altHeader)) {
      if(!is.character(options$tab.alt)) options$tab.alt <- ""
      if(!is.character(options$tab.altHeader)) options$tab.altHeader <- ""

      ooxml <- paste0("<w:tblCaption w:val=\"%s\" />",
                "<w:tblDescription w:val=\"%s\" />")

      ooxml <- sprintf(ooxml, options$tab.altHeader, options$tab.alt)
      x <- gsub("</w:tblPr>",paste0(ooxml,"</w:tblPr>"),x)
    }
    
    default_source_hook(x, options)
  }
)
```
```{r tab.cap="caption 1", tab.id="mtcars", tab.altHeader="Table with mtcars data", tab.alt="This is the famous mtcars data."}
head(mtcars)```

A note on tab.altHeader. tab.alt (tblDescription) is the one that matters for accessibility, or so I understand. In some versions of word, there is support for this also for figures, I've noticed. What do you think? In or out? If in, I can update the code for fig also.

camitz avatar May 28 '21 14:05 camitz

It needs to be done in officer. Sorry, I can't work on the subject for now. Please wait, I'll do the job in officer and let you know when officedown will be able to use it.

davidgohel avatar May 28 '21 15:05 davidgohel

ok, @davidgohel!

camitz avatar May 28 '21 17:05 camitz

This is now complete. Chunk options tab.alt.title and tab.alt.description can be used as alternative text for tables in Word (with officer and flextable since few versions and with officedown now).

davidgohel avatar Dec 10 '22 23:12 davidgohel

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.

github-actions[bot] avatar Jun 15 '23 05:06 github-actions[bot]