Search code examples
rlatexr-markdownknitrkableextra

Using kableExtra() breaking hyperlinks in my Latex tables


I'm outputting a table to PDF via Rmarkdown, and I'm trying to add a hyperlink into my table. It works using markdown syntax when I use kable(), but when I add any styling using kableExtra the hyperlink goes away. Below is an example:

This works:

---
output: pdf_document
---

```{r}
library(tidyverse)
data.frame(x = "[click here](https://google.com)") %>%
  knitr::kable() 
```

and gives me a clickable:

working_version

But when I change to:

---
output: pdf_document
---

```{r}
library(tidyverse)
data.frame(x = "[click here](https://google.com)") %>%
  knitr::kable() %>%
  kableExtra::kable_styling(font_size = 15)
```

I lose the hyperlink formatting:

broken_version

Anyone know how to maintain the hyperlink formatting while adding additional formatting, when going from RMarkdown to PDF? Thank you!!!


Solution

  • One way would be to use the "latex" option and make use \\href.

    ```{r}
    data.frame(x = "\\href{https://google.com}{Click here}") %>%
      knitr::kable("latex", escape = FALSE) %>%
      kableExtra::kable_styling(font_size = 15)
    

    enter image description here