Dear community,
I am using the huxtable package to create a table. I want to have some latex equation displayed in it for html and pdf output. I have to double backslash my basic equation and disable escape_content to have the proper output.
So my questions to you are:
- How would you handle such requirements?
- If I have to keep using the double backslash version, how can I produce it from my basic tex code?
Thanks in advance for your answers and suggestions,
jm
---
title: "chap5"
output:
pdf_document: default
html_document: default
---
```{r setup_chap5, echo=FALSE, warning=FALSE, error=FALSE}
library(knitr)
library(dplyr)
library(huxtable)
```
## Example 1
We consider the following observations of X and Y (sample):
```{r sampleXY, echo=FALSE}
x = c(3,4,5,12)
y = c(30,70,130,210)
df = data.frame(
xi = x,
yi = y
)
colnames(df) <- c('x', 'y')
hx = huxtable(df, add_colnames = TRUE) %>%
set_all_borders(row = every(), col = every(), value = 1)
# Used to test variations html vs latex & with and without escape
if (knitr::is_latex_output()) {
hx = set_escape_contents(hx, row = c(1), col = every(), value = FALSE)
hx = set_contents(hx, row = c(1), col = c(1), value = "latex")
hx = set_contents(hx, row = c(1), col = c(2), value = "\\(\\alpha + \\hat{y}_i\\)")
}
if (knitr::is_html_output()) {
hx = set_escape_contents(hx, row = c(1), col = every(), value = FALSE)
hx = set_contents(hx, row = c(1), col = c(1), value = "html")
hx = set_contents(hx, row = c(1), col = c(2), value = "\\(\\alpha + \\hat{y}_i\\)")
}
hx
```
My equation is \(\alpha + \hat{y}_i\)
```{r stringmanip, echo=TRUE, eval=FALSE}
# Does not work
str_replace_all( '\(\alpha + \hat{y}_i\)', '\', '\\' )
# Should double in the pattern part
str_replace_all( '\(\alpha + \hat{y}_i\)', '\\', '\\\\' )
```