Search code examples
rshinymathjax

R shiny with mathjax: How to avoid parentheses being automatically placed in math mode?


I have a shiny app which uses MathJax. In my normal text, MathJax converts text in parentheses to math mode automatically. Which setting allows me to escape math mode for something simple as a parenthesis?

In the MWE, the first Hello! should have been printed as (Hello!) instead of math mode. How to do this?

MWE:

library(shiny)

server <- shinyServer(function(input, output) {
 })

ui <- shinyUI(fluidPage( 
  withMathJax(),
  tags$div(HTML("<script type='text/x-mathjax-config'>
                MathJax.Hub.Config({
                tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
                });
                </script>
                ")),

  titlePanel("Minimal application"),
    sidebarLayout(  
    sidebarPanel( 
      fluidRow(h4("(Hello!)"))),
    mainPanel(
      fluidRow(h4("Hello!")))
    )
   ))

shinyApp(ui=ui, server=server)

Solution

  • solution 1

    The simplest workaround is to delete ['\\(','\\)'] in the script. This tells the math engine that between "(" and ")" will be treated as math mode. If you want to use math mode, you can still use "$xxx$".

    library(shiny)
    
    server <- shinyServer(function(input, output) {
    })
    
    ui <- shinyUI(fluidPage( 
        withMathJax(),
        tags$div(HTML("<script type='text/x-mathjax-config'>
                    MathJax.Hub.Config({
                    tex2jax: {inlineMath: [['$','$']]}
                    });
                    </script>
                    ")),
    
        titlePanel("Minimal application"),
        sidebarLayout(  
            sidebarPanel( 
                fluidRow(h4("(Hello!)"))),
            mainPanel(
                fluidRow(h4("Hello!")))
        )
    ))
    
    shinyApp(ui=ui, server=server)
    
    

    solution 2

    Directly escape "()" in the HTML by using <span class='tex2jax_ignore'>. Replace your h4 with this:

    fluidRow(HTML("<h4><span class='tex2jax_ignore'>(Hello!)</span></h4>"))),
    

    This will allow you to even escape "$".