Are bookdown cross-references in a table in Word possible?

Hello,

I am writing a report in R Markdown. This report will contain a table with three columns, the third of which contains cross-references to sections elsewhere in the document. I have to output the document to .docx. Is there a way to have R Markdown/bookdown cross-references being output correctly in a Word table (preferably using the {flextable} package).

A reprex (stolen - I would call it recycled - from this post) is included below.

---
title: "Can we do this using bookdown::word_document2?"
output: bookdown::pdf_document2
---

# Section {#ref1}

## Sub section {#ref1-1}

# A different section {#ref2}

```{r echo = FALSE}
suppressMessages(library(tidyverse))
suppressMessages(library(flextable))


cross_references <- tibble(
  Section_Name = c("Section",              "Sub section",            "A different section"),
  Section      = c("Section \\@ref(ref1)", "Section \\@ref(ref1-1)", "Section \\@ref(ref2)")
) 

ft_cross_reference <- flextable(cross_references)

ft_cross_reference

The cross-references display correctly when the output is PDF (i.e. when output: bookdown::pdf_document2 but only "Section \@ref(ref1)" when the output is Word.

Is it even possible to have cross-references work correctly in a table in Word?

All help is most welcome!

Rikard

1 Like

Have you read about single document for bookdown ?

You did not share what YAML format you are using for word so I don't know if you are aware

Hi Christophe,

Thanks again for replying to my post! I included a minimal YAML header in my reprex; for this example I am using bookdown::pdf_document2, but the wish is to have the cross-references also work with bookdown::word_document2 (which I guess is the only way to have cross-references function) (and, as said, preferably with {flextable}).

Note that what I am looking for is for the cross-references to work inside a table (and not, as is customary, in the text itself).

Note that what I am looking for is for the cross-references to work inside a table (and not, as is customary, in the text itself).

Then this is something to look for inside flextable...

this is working if you are using

knitr::kable(cross_references)

So it seems that what flextable is doing is not supported by bookdown reference mechanism. I don't know what or where it happens. This is an issue to report and/or look into in one of those two packages.

@atusy, hope you don't mind me pinging you, is this something you encountered or solve through ftExtra package ?

The cross-references do indeed seem to be working by using {kable} - and that is more than enough for my present purposes. The problem, then, does seem to lie with {flextable}.

Thanks again, Christophe!

Unfortunately, even with bookdown-style crossref is not supported by flexdown even with ftExtra.
I do not even come up with ideas to implement it (for now).

I guess you can workaround by using custom cross-reference by officedown.

@cderv Thanks for pining.

Thanks both of you for your replies.

I tried replicating Christophe's solution (with knitr::kable()) in my document, but could not get the cross-references in the table to work when output to Word (although this worked for the reprex I posted, as indicated by Christophe). Seems I have some further digging to do.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.