Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quarto - Word cross reference #494

Open
jtchittenden opened this issue Jan 4, 2023 · 5 comments
Open

Quarto - Word cross reference #494

jtchittenden opened this issue Jan 4, 2023 · 5 comments
Labels
caption quarto caption Word related to Word output

Comments

@jtchittenden
Copy link

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:

  • We want the SEQ set to Table
  • We want bookmark all set so references always resolve as "Table #"

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
Copy link
Owner

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?

@davidgohel davidgohel changed the title caption_default_docx_openxml ignores existing autonum Quarto - Word cross reference Jan 12, 2023
@cscheid
Copy link

cscheid commented Jan 12, 2023

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
Copy link
Owner

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
Copy link

BMC1986 commented Feb 16, 2023

@davidgohel any progress on this one?

@cscheid
Copy link

cscheid commented Feb 16, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
caption quarto caption Word related to Word output
Projects
None yet
Development

No branches or pull requests

4 participants