How to proper handle equations in package generated tables for PDF and HTML output?

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\)', '\\', '\\\\'  )

```

If you put your latex equations in a separate file, you could read them in as strings using readLines(). That would save you having to double all those backslashes. Example:

In equations.txt:


$\alpha^2$
$\hat{\beta}^2$

In R:

equations <- readLines("equations.txt")
hux(equations) %>% set_escape_contents(FALSE) %>% quick_pdf()

Thank you for the suggestion.

I have too much equations to handle via external file. Could put them all in a kind of db file. Should work but I find that not very elegant and easy to scale up and to maintain on the long run.

jm

Have you considered just using a standard markdown table?

I have some complicated table to format for both HTML and PDF.
By complicated I mean:

  • Sometimes visually not rectangular
  • Generated content
  • Few conditionally colored cells

Basic markdown table are ok for very basic content. I would say 6-8 cells, more than that I am lost. It is a personal opinion, there. Other people mileage will vary.

OK. I agree you need a programmatic solution. There's no way you can write raw TeX strings in R without doubling the backslashes. So yes, you are going to need to store them externally.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.