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

Caption formatting issues when using knitr::kable() and when using symbols in chunk name #46

Open
MarcioFCMartins opened this issue Oct 10, 2018 · 11 comments

Comments

@MarcioFCMartins
Copy link
Contributor

I have been adding captions to my documents in radix and noticed some issues with them:

Table captions inserted using knitr::kable are formated as standard text. Would it be possible to make it so that captions inserted this way are formated like captions from fig.cap chunk option?

Table/figure numbering and naming does not work if your chunk name includes a space, underscore or point.

See attachment for simple examples :

Test_captions.txt

@jjallaire
Copy link
Member

Okay, I've modified the CSS for table captions to appear as follows:

screen shot 2018-10-10 at 9 06 19 am

Note that for kable the caption always appears above the table by design: yihui/knitr#1189

In terms of the other cross references, this is a known limitation of bookdown cross-references: https://bookdown.org/yihui/bookdown/figures.html

I'll add a note about that to the documentation.

@MarcioFCMartins
Copy link
Contributor Author

kable captions are now working, but adding extra formatting via kableExtra::kable_styling still breaks it. Actually, simply loading the package breaks it unless the user setsoptions(kableExtra.auto_format = FALSE). This makes me believe that the issue is when the format is set to 'html' via this function.

I have no idea how pratical it is to ensure this formatting works for all package combinations (formattable comes to mind, since it works with kableExtra - https://haozhu233.github.io/kableExtra/use_kableExtra_with_formattable.html), but since kableExtra is recommended in the documentation (https://bookdown.org/yihui/rmarkdown/r-code.html#tables) I think it's worth ensuring it works.

The caption apearing above the table is proper scientific writing behaviour (atleast in most fields and from my experience) so that makes sense and it's exactly as I expect it.

I never realized that bookdown had that issue as well - thank you for the heads up and for the super quick fix - I will simply avoid extra table formatting until everything can be worked out.

@jjallaire
Copy link
Member

Yeah, it's difficult to run down every table formatting package and imbue all of their output with correct behavior. So far we've focused on pagedtable and plain kable. We'd welcome contributions to help support others so long as they don't make our core codebase too complicated and/or fragile.

@MarcioFCMartins
Copy link
Contributor Author

MarcioFCMartins commented Oct 11, 2018

EDIT: I just submitted my proposed change as a pull request.

I noticed that you can actually break the caption formatting with the base kable function if you set format to html rather than pandoc.

This is because in distill.html you are setting the caption style for the pandoc table class individually. This means you need to ensure all tables have pandoc-table class to correctly format their caption - and this has to be done for every package.

Wouldn't it be more efficient to set the style for the table>caption element? This element is used to create the captions for all tables, regardless of their class (and the same is true for images, but you might want the ability to control the styles separatly).

I tried changing line 835 - currently $('.pandoc-table>caption').addClass("figcaption");. Changing it to $('caption').parent('table').addClass("figcaption") changes all table captions (regardless of being tagged as pandoc tables) to the same style as figcaption.

You could also just set style for all captions, regardless of where they are used by removing the parent. If you have no need for differently formatted captions this might be the best.

@haozhu233
Copy link

https://haozhu233.github.io/kableExtra/using_kableExtra_in_radix.html

I just added a quick example of how to use kableExtra in radix. @MarcioFCMartins I hope it helps you feel better. :)

I really love the design of radix!

@jrgant
Copy link

jrgant commented Nov 29, 2018

I'm still having this issue with table captions using the following:

knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
df <- data.frame(x = rep(1:2, each = 5),
                 y = rnorm(10))

df %>%
  knitr::kable(., caption = "Good caption (no chunk label)")
df %>%
  knitr::kable(., caption = "Bad caption (chunk label with period)")
df %>%
  knitr::kable(., caption = "Bad caption (chunk label with underscore)")

The bad captions:

rendered_captions.jpg

radix_bug.txt

@a-bro-0227
Copy link

o man! @jrgant thank you. I've been stuck on this issue and could not figure out why my captions were rendering (#tab:caption). Only populates correctly with a - for spaces. No periods or underscores.

@meghandownes
Copy link

Thank you for this post and the discussion. Changing my chunk label allowed the bookdown kable_extra rendered as PDF to correctly add table headings. You have helped my day so much. Thanks!

@mapostolides
Copy link

How do I change things like the caption colour and size, etc? There seems to be no documentation on this

@cderv
Copy link
Collaborator

cderv commented Jun 2, 2022

How do I change things like the caption colour and size, etc? There seems to be no documentation on this

You could probably use CSS for this - the exact CSS could depend on which format of table you are using. CSS and distill is documented in https://rstudio.github.io/distill/basics.html#theming and https://rstudio.github.io/distill/website.html#custom-style and https://rstudio.github.io/distill/website.html#theming

We don't have built in way to change this using variable, you need to use custom CSS for this.

Hope it helps

@cashcab1221
Copy link

I have also found that a caption can not contain some other symbols. I was trying with % in my caption for a while and then found this issue.

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

No branches or pull requests

9 participants