Search code examples
rshinymathjax

Proper display of degree Celsius, Kelvin, or Fahrenheit in mathjax


I'm trying to get the degree symbol to show up properly when using mathjaxr. That is, I want it to be adjacent to the F, C, or K for degrees (temperature). According to the mathjax supported commands, the \\degree command should make the correct symbol. The \\circ command does not do what I want, unless there is a way to force it to be adjacent to the temperature scale (F,C, or K). Is there a way to get this to work so the degree symbol is adjacent to the temperature scale?

library(shiny)
library(mathjaxr)

ui <- fluidPage(
        title = 'MathJax Examples',
        uiOutput('ex3'))

server <- function(input, output, session){
                   output$ex3 <- renderUI({
                   withMathJax(
                               helpText(        
                                        "$$3^{\\circ}F$$"      
    
                                        # "$$3\\degree F$$"
  ))    
})}

shinyApp(ui = ui, server = server)

Solution

  • Looking at some posts from TeX Stack Exchange (here, here) suggests using one or more negative spaces.

    I replaced the literal string in your code with helpText(txt) so that I could experiment a little more easily

    txt <- "$$3 {}^\\circ \\! F$$"
    shinyApp(ui = ui, server = server)
    

    gives

    enter image description here

    Using two double-negative spaces (\\!\\!) gives

    enter image description here

    which looks a little too crowded.

    Adding a positive thin space after the number and before the degree symbol (txt <- "$$3 \\, {}^\\circ \\!F$$") isn't perfect either:

    enter image description here

    Getting really obsessive, txt <- "$$3 {}^\\circ \\kern{-5mu} F$$" looks pretty good to me:

    enter image description here

    Being even pickier, it looks like you're not supposed to set units in math italic (see here) — and Kelvin degrees don't take a degree symbol ... so you might want something like txt <- "$$3 {}^\\circ \\kern{-2mu} \\mathrm{F}$$"