Open N0rbert opened 4 years ago
EPUB (bookdown::epub_book: default
) is affected too:
And PDF (bookdown::pdf_book: default
) book too (but looks different - have two \\ref{..}
instead of \@(...)
):
The issue you report is kind of specific because you did not have any figure or table to reference.
You should see a warning about that. Something like
Warning messages:
1: The label(s) fig:myfig not found
2: The label(s) tab:mytab not found
3: The label(s) fig:myfig not found
4: The label(s) tab:mytab not found
5: The label(s) fig:myfig not found
6: The label(s) tab:mytab not found
7: The label(s) fig:myfig not found
8: The label(s) tab:mytab not found
9: The label(s) fig:myfig not found
10: The label(s) tab:mytab not found
However, maybe the output should be different in case of no reference found... 🤔
Can you clarify something: Why do you think the output for html is correct ? There is no tables and figures to reference either in your HTML examples ?
Can you make an example using a table and a figure to reference too ? This should work as expected by replacing the reference by the correct number.
I see these warnings, but this should not happen.
I'm writing a text with example code inside it - so I expect that `see Fig. \@ref(fig:myfig)`
or `see Table \@ref(tab:mytab)`
should not be interpreted and should be shown as-is (in-line or fenced code block depending on syntax used). From this PoV the HTML output is absolutely correct and expected. But DOCX and ODT are wrong!
So I do not refer to existing table and/or figure, I'm providing example of the reference in code form.
Like in Y. Xie book - with theorems and `\@ref(prefix:label)`
.
With the context, it is way clearer. thank you very much for the precisions. I am sorry if I misunderstood you. To be sure I am correct, do you expect that when put \@ref
anywhere inside inline code block, bookdown should not parse them as reference to be replaced ?
Currently, bookdown is parsing the whole file and finding Fig. \@ref(fig:name)
and Table. \@ref(tab:name)
to be replaced, then replace it. But if you write
Fig. `\@ref(fig:myfig)`
or
Table `\@ref(tab:mytab)`
then it is not replaced - That is what is done in the bookdown book and why it works.
Bookdown currently seems to only skip \@ref
when put directly after a backtick.
In your example that would mean
This line contains code example with reference to Fig. `\@ref(fig:myfig)`.
This line contains code example with reference to Table `\@ref(tab:mytab)`.
If we change the detection method, the question I see here:
Should we assume that if the Fig. \@ref
or Table. \@ref
code are put in some code blocks, then it all case we don't want to replace them by their reference ?
I think so, but not sure if it could break some use case...
Anyway, not a trivial case to deal with, 🤔
Playing with regex and making exception is not a simple task.
Bookdown is currently made to parse and get correct references for any \@ref
occurrence.
Thanks for your analysis!
I have already using workaround for inline code using Fig. `\@ref(fig:myfig)`
and `Table `\@ref(tab:mytab)`
.
But I do not know how to apply the fix for fenced code (or nested) blocks.
And I wonder why it works normally on HTML format, but does not work in other formats. Could you please elaborate or dig into the problem deeper?
For HTML, the processing is different as support for references is done by processing the html output directly. For other format like word and odt, references support in bookdown is added by processing the md input so that conversion by pandoc keeps the references and it works. For pdf, it is the tex file that is processed before conversion to pdf.
The syntax is different for each format, so the logic too, hence the difference in treatment currently. What you want is not supported I guess in current version, some adaptations are required to ignore reference label inside code blocks.
I will look into it when I find time. You can also dig yourself and open a PR if you find a way to deal with this. Do not hesitate, it is always welcome !
Some notes on this topic:
Maybe using a Lua filter to implement this referencing feature would allow to avoid such differences between format because we could do the change directly in the Pandoc AST.
Currently, to improve this for formats powered by bookdown:::process_markdown()
, this would require changing the logic here:
https://github.com/rstudio/bookdown/blob/3695a759770507a3a045fb7968f8d5b288e9e8dc/R/ebook.R#L85-L96
Currently, figure labels are found by creating a temp HTML files from the md files, and then making the replacement in the markdown input directly, which causes the above issue.
FYI: I am interested in contributing, and already have a prototype filter but not well tested.
---
output:
word_document:
number_sections: true
pandoc_args:
- "--lua-filter"
- "crossref.lua"
---
```{r, include=FALSE}
download.file("https://raw.githubusercontent.com/atusy/lua-filters/master/lua/crossref.lua", "crossref.lua")
Fig. \@ref(fig:myfig)
.Table \@ref(tab:mytab)
.
@atusy @cderv I don't know how much work it would be to replace my dirty hacks in R with a Lua filter, but I just want to say that I'm all for doing that!
That said, eventually we want to switch the syntax from \@ref(fig:label)
+ (\#fig:label)
to @fig:label
+ {#fig:label}
(https://github.com/quarto-dev/quarto-cli/tree/main/src/resources/filters/crossref), so I'm not sure if it'll be worth the effort now to develop the Lua filter for the former. I'll let you decide.
I'll look into it. @atusy seems to have a working prototype so maybe it just lack thorough testing. We'll see about that.
The code below knits correctly to the HTML:
as shown on the image below:
but DOCX and ODT outputs are wrong (they contain HTML code with ??):
DOCX
ODT
My version info:
and I get this problem with bundled and latest pandoc 2.7.3-1 and even with
remotes::install_github('yihui/knitr')
.Could please elaborate why this happening? Should we move this bug to Knitr or Pandoc issue trackers?
By filing an issue to this repo, I promise that
xfun::session_info('bookdown')
. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('rstudio/bookdown')
.I understand that my issue may be closed if I don't fulfill my promises.