Search code examples
quarto

Math formulas in a table are not processed in PDF (but caption math is).?


I'm using Quarto. I need to put LaTeX math formulas in the cells of a table, and in the table caption. Using kbl(), the math is displayed as math in the caption, but not in the cells. I'm especially interested in PDF output. How can I get the math to display properly in the cells of the table??? Thanks in advance!

Here's an example of the problem, showing the math correctly rendered in the caption but not in the table itself:

enter image description here

I have seen the suggestion to use results: asis, kt <- kbl(), and cat(kt), but this does not change the PDF rendering (and loses the caption and table reference in HTML).

Here is a complete, minimal example. It is a Quarto .qmd file.

    ```{r}
    library(kableExtra)
    ```
    
    Specify the table with LaTeX math formulas. (R requires using double backslash.)
    ```{r}
    tbltext = matrix( c( "$(r1,c1)$ " , "$\\vdots$" ,
                         "$\\mbox{C}$" , "no math" ) ,
                      nrow=2 , byrow=TRUE )
    dimnames(tbltext) = list( "Row" = c("Row $A$","Row $B$") , 
                              "Column" = c("Col.$x$","Col.$y$") )
    tbltext
    ```
    
    
    **Display the table  with `kbl()`:**
    ```{r}
    #| label: tbl-math
    #| tbl-cap: "This table has math in it and in the caption: $p(x|\\ldots)$."
    
    kbl( tbltext , align="cc" , booktabs=TRUE ) |>
      add_header_above(
        header = setNames( c(1,2) ,
                           c(" ",names(dimnames(tbltext))[2]) ) ,
        bold=TRUE ) |>
      pack_rows( names(dimnames(tbltext))[1] , 1, 2)
    ```
    See @tbl-math. Rendered in PDF or in HTML, the math in the caption *is* displayed as math, but the math in the table is displayed as text, not processed as math. 

    

Solution

  • Add the argument with the value escape = FALSE to kableExtra::kbl and you will obtain the desire table in the case of pdf format:

    #| label: tbl-math
    #| tbl-cap: "This table has math in it and in the caption: $p(x|\\ldots)$."
        
    kbl(tbltext, align = "cc", booktabs = TRUE, escape = FALSE) |>
        add_header_above(
        header = setNames(c(1,2),
                          c(" ",names(dimnames(tbltext))[2]) ) ,
        bold = TRUE) |>
      pack_rows( names(dimnames(tbltext))[1] , 1, 2)
    

    Here is a screenshot of the result applying this option:

    enter image description here

    I know you don't mention the case about html format but if you need it please take into account the following for inline-math in the case of MathJax: https://docs.mathjax.org/en/latest/web/start.html?highlight=inline#configuring-mathjax

    Therefore if you want that your minimal example also works for html you need to add to the header the following script:

    <script>
      MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']]
        }
      };
      </script>
      <script id="MathJax-script" async
        src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js">
      </script>
    

    In my case I calleded in-line_math_mode.html and a minimal yaml will be:

    ---
    title: "Untitled"
    format: 
      pdf: default
      html:
        include-in-header: in-line_math_mode.html
    ---
    

    Here is a screenshot of the result applying this option for the html format:

    enter image description here