Last active
October 24, 2022 04:57
-
-
Save dmi3kno/016b9ae2d2de1ef54f4df07ebcd24c4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: 'Tables with references' | |
author: dmi3kno | |
date: '2018-01-25' | |
abstract: | | |
My goal was to prepare a minimum reproducible example. | |
output: | |
pdf_document: | |
citation_package: natbib | |
keep_md: yes | |
html_document: default | |
bibliography: some.bib | |
link_citations: true | |
--- | |
```{r, include=FALSE} | |
knitr::opts_chunk$set(echo = TRUE) | |
``` | |
# Section | |
Let's enter some text. \cite{R-base} gave us amazing tool. You can do a lot with R, especially if you use `tidyverse` [@tidyverse2019]. | |
```{r, define_tibble, message=FALSE, error=FALSE, warning=FALSE} | |
library(tidyverse) | |
library(gt) | |
knitr::write_bib(c(.packages(), "knitr"), "some.bib") | |
ref_tbl <- tibble::tribble(~pkg, ~ref, | |
"R", "@R-base", | |
"dplyr", "@R-dplyr", | |
"stringr", "@R-stringr", | |
"knitr", "@knitr2014", | |
"gt", "@R-gt") | |
``` | |
```{r knitr_kable, results='asis'} | |
knitr::kable(ref_tbl, format = "markdown") | |
``` | |
This does not work, because `gt` is rendered in LaTeX and pandoc-citeproc does not look into LaTeX tags. | |
```{r gt} | |
gt(ref_tbl) | |
``` | |
This however works. | |
```{r gt_latex} | |
ref_tbl %>% | |
mutate(ref=paste0("\\cite{",gsub("^@", "", ref), "}")) %>% | |
gt() %>% | |
fmt(vars(ref), fns=tools::encoded_text_to_latex) | |
``` | |
# Summary | |
Solution is therefore three-fold: | |
- 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()` | |
# References |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment