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

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

Closed
JiaxiangBU opened this issue Dec 15, 2018 · 6 comments
Closed

Comments

@JiaxiangBU
Copy link

JiaxiangBU commented Dec 15, 2018

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.

@JiaxiangBU JiaxiangBU changed the title Support bib te x Support Add reference by bibtex @ in the function tab_source_note Dec 15, 2018
@rich-iannone
Copy link
Member

rich-iannone commented Dec 23, 2018

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
Copy link
Member

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

@JiaxiangBU
Copy link
Author

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
Copy link

csoeder commented Apr 24, 2020

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
Copy link

dmi3kno commented May 22, 2020

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
Copy link

dmi3kno commented May 23, 2020

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

Key takeaways:

  • use citation_package: natbib for processing references
  • make a dataframe of citations in LaTeX style (double-escaped).
  • pass to gt and use post-formatting with tools::encoded_text_to_latex()

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, "}")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment