0

I often want to display both Latex and the underlying value of a variable in a ggplot title, but my existing methods seem to only work for one or the other.

library(tidyverse)
library(glue)
library(latex2exp)
MY_VARIABLE <- 1000
ggplot(mtcars, aes(x=wt, y=mpg)) +
    geom_point() + 
    labs(title = TeX(r'(Can I use $\LaTeX$ with MY_VARIABLE)'))) 

enter image description here

MY_VARIABLE <- 1000
ggplot(mtcars, aes(x=wt, y=mpg)) +
    geom_point() + 
    labs(title = glue('Can I use \\LaTeX with {MY_VARIABLE}')) 

enter image description here

2 Answers 2

2

Another option using paste0 with TeX:

library(tidyverse)
library(latex2exp)
MY_VARIABLE <- 1000
ggplot(mtcars, aes(x=wt, y=mpg)) +
  geom_point() + 
  labs(title = TeX(paste0('Can I use \\LaTeX with', MY_VARIABLE)))

Created on 2022-08-16 by the reprex package (v2.0.1)

Sign up to request clarification or add additional context in comments.

Comments

2

Just pass the output of glue to TeX:

library(tidyverse)
library(glue)
library(latex2exp)

MY_VARIABLE <- 1000
ggplot(mtcars, aes(x=wt, y=mpg)) +
    geom_point() + 
    labs(title = TeX(glue('Can I use \\LaTeX with {MY_VARIABLE}')))

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.