Changing labels of cross-references on bookdown

I'm trying to provide custom labels to figures and tables on bookdown but I haven't managed to do so.

I tried to change the language according to the book (Internationalization section) on the default book when opening a new project on Rstudio and selecting a bookdown project.
I only added the label sections below to the _bookdown.yml file present

language:
  label:
    fig: function(i) paste("Figures ", i)
    tab: function(i) paste("Tables ", i)

Or

language:
  label:
    fig: !expr function(i) paste("Figures ", i)
    tab: !expr function(i) paste("Tables ", i)

Or simply

language:
  label:
    fig: "Figures "
    tab: "Tables "

However, the resulting cross reference text is " See Figure 3.1." and " Don’t miss Table 3.1." On the cross.html file rendered and not Figures 3.1 or Tables 3.1

I am misunderstanding something? Or this is a bug?

I found an issue related to this: [FR] Add prefixes to cross-references · Issue #1166 · rstudio/bookdown · GitHub (Which doesn't mention the internationalization help page)

Additionally, would the function work to solve this question html - How to create a R markdown/bookdown cross reference to a table that includes both text and the automatically generated table number? - Stack Overflow ?
I'm curious if the function works as to replace the text used for labeling or it is just appended.

Many thanks for such a useful package

PS: I'm using bookdown 0.24 on R 4.1.2 (Ubuntu 20.04)

1 Like

The cross reference syntax \@ref(label) will only be replaced by the number currently. So you see Figure or Table in front of the number because it should be in the text (like in the bookdown-demo).

The internationalization feature only works for Caption for now. You found the correct feature request for this.

Thanks, sorry but I find very difficult to find the documentation of the different options and combinations despite (or precisely due to) the high number of books and web pages with documentation.
So the function /expression is not used contrary to what it is documented? Is somewhere documented that this only works for captions? (I can send a PR to modify the internalization chapter of the "Authoring Books and Technical Documents with R Markdown")

The documentation for bookdown features are in the Bookdown book so this chapter

I don't understand what does not work ?

As documented, you can use a function to modify the Caption label that is otherwise hard-coded in Bookdown. For figure, it is by default Figure

In the reference part, only the number will be output when you render. So it is up to the author to add the prefix desired.

For example

```{r nice-fig, fig.cap='Here is a nice figure!', out.width='80%', fig.asp=.75, fig.align='center'}
par(mar = c(4, 4, .1, .1))
plot(pressure, type = 'b', pch = 19)
```

see \@ref(fig:nice-fig). 

will output

As you can see it is only the number. The author can write anything before.

The opened issue suggest to add a feature to use the label also in the reference directly.

Please, can you give an example of what is not working compared to what is documented ?

thank you.

Everything works, but I found the documentation unclear. I expected that the language option would add the expression also on cross-references. I didn't see any mention that the change only affects the caption labels.

I wish the documentation explained it:

If the language of your book is not English, you will need to translate certain English words and phrases into your language on the captions, such as the words “Figure” and “Table” when figures/tables are automatically numbered in the HTML output. Internationalization may not be an issue for LaTeX output, since some LaTeX packages can automatically translate these terms into the local language, such as the ctexcap package for Chinese.

Have a nice day.

1 Like

I think it is not something which gets precised because there is no such things by default as reference caption or reference prepended label. It is not a feature that exists yet, so we did not mention in the doc that internationalization that it affect only caption.

We welcome any improvement to the doc so please yes do send a PR with improved wording so that this is clearer to you. Thanks !

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.