haozhu233 / kableExtra

Construct Complex Table with knitr::kable() + pipe.
https://haozhu233.github.io/kableExtra/
Other
688 stars 147 forks source link

markup text not rendered in latex #214

Open francoisbirgand opened 6 years ago

francoisbirgand commented 6 years ago

Hello, I thank you very much for your great package! As I was writing a tutorial on how to write scientific documents in bookdown, with html, pdf, and docx formats, I stumbled upon this, and I cannot seem to find a fix or witness that others have had the same issue: article reference or markup text are rendered just fine for an html output, but do not seem to be rendered for latex output...

in the example below with html output:

---
title: "Test"
documentclass: article
output:
  bookdown::pdf_document2:
    keep_tex: yes
  bookdown::html_document2:
    keep_tex: yes
    keep_md: yes
link-citations: true
references:
- type: article-journal
  id: WatsonCrick1953
  author:
  - family: Watson
    given: J. D.
  - family: Crick
    given: F. H. C.
  issued:
    1953
  title: 'Molecular structure of nucleic acids: a structure for deoxyribose nucleic acid'
  container-title: Nature
  volume: 171
  issue: 4356
  page: 737-738
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

@WatsonCrick1953

```{r "test", echo=FALSE, warning=FALSE, results='asis'}
df<-data.frame(Citation = c("@WatsonCrick1953"),
  formula = c("Some PO~4~^3-^"))

kableExtra::kable(df, align = "c", "html", booktabs=T)
```

The reference before and within the table are rendered correctly.

screen shot 2018-06-07 at 4 00 35 pm

But if I switch "html" to "latex" in the last code chuck, the text inside the table is not rendered. Am I am missing something here? Thanks for your help!

screen shot 2018-06-07 at 4 01 57 pm
haozhu233 commented 6 years ago

I'm not sure if this is a bug or just a behavior of knitr but markdown syntax in raw latex tables just won't get recognized. I tried to investigate through it but it seems to be quite complicated.

If what you need here is just a "simple table" without any fancy features, how about using kable(..., "markdown") or pander?

francoisbirgand commented 6 years ago

thank you for your answer. Yes, this is what I have been doing, I have been using pander instead, but I think it is an issue that needed to be reported, because kableExtra is so good and it is not uncommon for article citations or equations to have to appear in tables. If you find a solution or a reason for this, it will further improve your great package. Best.

haozhu233 commented 6 years ago

@francoisbirgand Thank you so much for your kind words! :) I will investigate in knitr a little bit more. If there is no easy solution, I can try to implement kableExtra's own citation interputator. Thanks for bringing it up.

rachaellappan commented 6 years ago

Hello, would just like to comment on this - I have also been trying to insert a citation in this way. I am using Bookdown and knitting to pdf. There are instances where I need to insert a citation within the cell of the table, in the caption, or in a footnote (which is why kableExtra is great!).

At the moment, my workaround is to just manually insert the text of the citation so that the table looks correct, and then insert the citation outside of the table - coloured white. For now this works for me, but it would be great if there is (or could be) a way to put citations inside these parts of a kable table so that they display correctly in pdf and can be recognised by the rest of the document (e.g. in a Vancouver-style reference list where the citations are numbered).

In the example above, the phosphate will render properly with latex commands: formula = c("Some PO\\textsubscript{4}\\textsuperscript{3-}"))

However, trying (Citation = c("\\cite{WatsonCrick1953}") renders a [?] instead of the citation.

rachaellappan commented 6 years ago

I have an update on inserting a citation within a table caption or table footnote generated by KableExtra.

What works for me within a Bookdown document is to specify the caption or footnote as a text reference: https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#text-references

e.g.

(ref:tablefootnote) A footnote for the table which contains some **markdown formatting** and a citation. [@WatsonCrick1953]

and to refer to this when specifying the caption or footnote in KableExtra, with a line like:

footnote(alphabet = '(ref:tablefootnote)')

Any markdown formatting in the text reference, including citations, then appears properly linked and numbered in the table caption or footnote.

stanstrup commented 5 years ago

Any news on this? I am also trying to insert citations in tables. We have a book project with several hundred references in tables and would like to find a way to make that work.

rachaellappan commented 5 years ago

I had success with the bookdown text references - I'm not sure if you need to be making a bookdown book or if it will work with the bookdown package loaded.

In my case, I defined the references first outside of a code block, e.g.

(ref:studies-table-ref1) Bogaert *et al.* (2011) [@BogaertVariabilityDiversityNasopharyngeal2011]

then added them to the table

studies.table$Reference <- c("(ref:studies-table-ref1)", "(ref:studies-table-ref2)", # etc

And kable interpreted them correctly.

tianyuan commented 4 years ago

I had success with the bookdown text references - I'm not sure if you need to be making a bookdown book or if it will work with the bookdown package loaded.

In my case, I defined the references first outside of a code block, e.g.

(ref:studies-table-ref1) Bogaert *et al.* (2011) [@BogaertVariabilityDiversityNasopharyngeal2011]

then added them to the table

studies.table$Reference <- c("(ref:studies-table-ref1)", "(ref:studies-table-ref2)", # etc

And kable interpreted them correctly.

This is working for me. I was searching a solution for this issue for a while. Thanks so much!

giabaio commented 9 months ago

Can I please follow up on this? I have something like the following

#| echo: false
#| message: false
#| warning: false
#| label: tbl-dataMTC
#| tbl-cap: The dataset containing information on the $S=24$ trials on smoking cessation. The data were originally reported in @Hasselblad1998.

# Reads the table in & fixes the LaTeX code for the parameters and then 
# formats using kableExtra
tibble("Study $s_i$"=c(1,2,3),"Intervention $t_i$"=c(1,2,1)) |>
   knitr::kable(escape=FALSE,booktabs=TRUE,longtable=TRUE,format="latex") |> 
   kable_styling(latex_options=c("striped","repeat_header","scaled_down")) |>
   row_spec(0,bold=TRUE)

It works almost OK --- the table caption doesn't render the markdown and so it's interpreted literally (the S=24 isn't formatted as LaTeX would and the citation isn't rendered).

Is there any other way I should do this? Thanks!