rstudio / gt

Easily generate information-rich, publication-quality tables from R
https://gt.rstudio.com
Other
1.98k stars 200 forks source link

Support Add reference by bibtex `@` in the function `tab_source_note` #112

Closed JiaxiangBU closed 3 months ago

JiaxiangBU commented 5 years ago

I am reading the vignette and find this table add a source note with reference McNeil, D. R. (1977).

Here is related R code I copy from this vignette.

# Display the `islands_tbl` data with a heading and
# two source notes
gt_tbl <- 
  gt_tbl %>%
  tab_source_note(
    source_note = "Source: The World Almanac and Book of Facts, 1975, page 406."
  ) %>%
  tab_source_note(
    source_note = md("Reference: McNeil, D. R. (1977) *Interactive Data Analysis*. Wiley.")
  )

# Show the gt Table
gt_tbl

Does the function tab_source_note to support BibTeX reference in the future? Maybe it is hard because I don't see other packages to support this feature.

rich-iannone commented 5 years ago

Thanks for filing this issue. It seems like a very good idea to integrate citations with BibTeX. I'll look into how this can be done easily.

rich-iannone commented 5 years ago

Looking into the possibility of adding a new method for LaTeX that could use additional arguments for different citation types.

JiaxiangBU commented 5 years ago

I find it maybe helps to add the arguement results='asis' in the R chunk.

image

image

However, the arguement results='asis' maybe disturbs the gt display.

csoeder commented 4 years ago

Closely related, it would be really useful for me if there was some sort of fmt_citation function, which would take a bibtex file and then format a column of @-tags ("@Li2019") into a column of short ("Li (2019)") or long ("'Title of a Good Paper'. Bob Li, 2019 Journal of Cool Stuff vol. 4 no. 20" ) citation. This would be good for literature reviews, meta-analyses, tables of software used/data sources, etc.

Thanks for the great package!

dmi3kno commented 4 years ago

My understand is that the reason @-tags don't work in gt is because it exports LaTex, but pandoc is skipping those tags when parsing the text with pandoc-citeproc. On the other hand knitr::kable(format="markdown") exports plain markdown tables and therefore they get properly parsed for @-tags. I don't know if anything can be done at all, since markdown formatting is very limited. Perhaps there's a solution with a different reference processor: natbib or biblatex, but I don't know enough to speak about that confidently.

dmi3kno commented 4 years ago

I think I have a solution. https://gist.github.com/dmi3kno/016b9ae2d2de1ef54f4df07ebcd24c4d

Key takeaways:

Generally speaking, fmt(cols, fns=tools::encoded_text_to_latex) seems to be a useful pattern. Would you consider adding it as a standard formatter, i.e. fmt_latex(), @rich-iannone ? Would you like a PR with that?

UPDATE Here's my [@ref] to \cite{ref} transforming function

citeproc_to_natbib <- function(x){
  p <- grepl("^\\[.+\\]$", x)
  # strip square brackets, if any
  if(p) x <- gsub("(^\\[)|(\\]$)", "", x) 
  txt_lst <- strsplit(x, ";\\s?")
  txt_lst <- lapply(txt_lst, function(i) gsub("^@", "",i))

  ltr <- ifelse(p, "p", "t")

  txt <- sapply(txt_lst, paste0, collapse = ", ")
  paste0("\\cite", ltr, "{", txt, "}")
}