stefano-meschiari / latex2exp

Use LaTeX in R graphics.
Other
185 stars 10 forks source link

Using vector values #31

Closed jorgesinval closed 3 years ago

jorgesinval commented 3 years ago

Is it possible to vector values (using R objects) within the TeX expression?

Example (It did not work; I had to include the values manually in the TeX code):

p_1 <- .2
ntrials <- 10
success <- 0:ntrials
exp_v_1 <- ntrials*p_1
var_v_1 <- ntrials*p_1*(1-p_1)

plot(success, dbinom(success, size=10, prob=p_1),type='h',
     main = TeX(r'($p = `r p_1`; E(X)=`r exp_v_1`;Var(X)=`r var_v_1`$)', italic = T),
     ylim = c(0,.3),
     xlab=substitute(italic(X)), ylab=substitute(italic(f(X))), frame.plot = F, col="blue")
text(x = 0, y= .3, "a)")

Desired behavior resulting in:

image

stefano-meschiari commented 3 years ago

Not at this point in time, although it would be trivial to use sprintf to insert those values in the string (bonus: you get to control how the values you interpolate into the string are formatted!):

TeX(sprintf(r'($p = %.1f; E(X)=%f; Var(X)=%.1f$)', p_1, exp_v_1, var_v_1), 
        italic = T)
jorgesinval commented 3 years ago

Thank you! I will keep an eye on updates.