davidgohel / flextable

table farming
https://ardata-fr.github.io/flextable-book/
563 stars 82 forks source link

Quarto - Word cross reference #494

Open jtchittenden opened 1 year ago

jtchittenden commented 1 year ago

I'm trying to get around the quarto features for referencing Tables and Figures for docx outputs. When using flextables we have a custom function to apply formatting. In there we use set_caption and run_autonum to configure the desired bookmark. We're primarily targeting two main outcomes:

There's a Lua filter that runs before the quarto filter to intercept intext references and convert them to inline openxml references, which works fine.

The problem we noticed was that the flexible object was creating a bookmark on just the field code, not the pre-label, even though running to_wml.autonum showed otherwise. The issue was traced to caption_default_docx_openxml where this code ignores any existing autonum in the caption:

autonum <- ""
    if (allow_autonum) {
      run_autonum <- get_word_autonum(x, tab_props = tab_props)
      if (!is.null(run_autonum)) {
        autonum <- to_wml(run_autonum)
      }
    } 

We were able to fix the function in the namespace (kluge for now) by checking for existing autonum:

autonum=""
  if(is.null(x$caption$autonum)){
    autonum <- ""
    if (allow_autonum) {
      run_autonum <- flextable:::get_word_autonum(x, tab_props = tab_props)
      if (!is.null(run_autonum)) {
        autonum <- to_wml(run_autonum)
      }
    } 
  } else{
    autonum = to_wml(x$caption$autonum)
  }

We also noted that get_word_autonum always sets bkm_all to FALSE.

The above fix works for us, but a more user/quarto friendly option could be to allow tab.lp and tab.bmk_all to pass through to get_word_autonum.

PS: Thanks for this fantastic package!

davidgohel commented 1 year ago

I did not notice Quarto is not producing the same reference than R Markdown and bookdown.

Cross-references are in the format "Table XX" with Quarto; with R Markdown they are in the format "XX".

I can generate "Table 1" as you are asking using bkm_all = TRUE, but quarto will still add "Table " before. 'flextable' is not generating the prefix "Table"; it is generated by Quarto.

Capture d’écran 2023-01-12 à 11 20 29

Hello @cscheid. Is it something that can be changed/defined/set for Quarto? I understood that there would be soon evolutions on this subject (captions+cross-references in the context of raw xml injection) but I don't know which line to follow. Do you have any advice or indications?
For example, Quarto inject/replace tables' captions when format are HTML and PDF, do you plan to do the same for Word format?

cscheid commented 1 year ago

We are actively working on a way for libraries to communicate metadata (such as rich captions) directly to quarto, so that we can resolve crossrefs, etc, as well as make sure that the output is formatted properly. The plan is to make it so that (eg) flextable can detect it's running "in a quarto environment", in which case it doesn't need to know about the crossref style (which can be configured by quarto itself). Then, flextable wouldn't need produce a "decorated" caption with Table $ref: <caption>. That decoration can be determined by quarto, and all that flextable would need is to communicate is the content of the caption (ideally directly in Markdown), and quarto would resolve that into the right place.

We hope to have a concrete implementation and preliminary documentation to share in about 3-4 weeks. Send me an email at carlos.scheidegger@posit.co? I'd love to meet with you directly to explain and make myself available to help you get a solid implementation going. We are optimistic about the simplicity of the resulting implementation based on our early experiments.

davidgohel commented 1 year ago

It really seems to fit what we need. Thank you very much for your answer, I will email you tomorrow, I'd love to chat and do a solid implementation!

BMC1986 commented 1 year ago

@davidgohel any progress on this one?

cscheid commented 1 year ago

@BMC1986 When there's progress, we will update the issue.