1

I need to write a text, in bold face, in an R plot, using latex content but also containing a value coming from a variable.

Is there a way to write in bf a latex text containing also a value from a variable?

t1 <- TeX(paste("$\\textbf{\\left[\\frac{M}{L}\\right]_d =}$", sprintf(fmt="%.3f",XML), sep=""))

text(PosX1,PosY, t1, adj=c(0,0), cex=1.0, font=2)

In that case, the variable is XML, but putting font=2 does not change the font of the value of XML...

Here I wrote an example with a fake plot. The M/L written with TeX is in bf and I would like to have the value 0.001 value from the variable also in bf.

library(graphics)
library(latex2exp)

pdf(file="Test2.pdf", 15, 10)
par(mar=c(5.0, 5.0, 6.0, 5.0))
XML = 0.001
x = seq(0,40, 0.2)
y = 1*exp(-((x - 20)/5)^2)
plot(x,y, pch=19, cex=1.0, xlab = "X", ylab = "Y")
#
PosX = 5
PosY = 0.8
t1 <- TeX(paste("$\\textbf{\\left[\\frac{M}{L}\\right] =}$",    sprintf(fmt="%.3f",XML), sep=""))
text(PosX,PosY, t1, adj=c(0,0), cex=1.0, font=2)

dev.off()

1 Answers1

0

I suggest two changes:

  • use \mathbf instead of `\textbf¸ since you are in math mode
  • extend the range of \mathbf and math mode to include the number

Combined:

library(graphics)
library(latex2exp)

pdf(file="Test2.pdf", 15, 10)
par(mar=c(5.0, 5.0, 6.0, 5.0))
XML = 0.001
x = seq(0,40, 0.2)
y = 1*exp(-((x - 20)/5)^2)
plot(x,y, pch=19, cex=1.0, xlab = "X", ylab = "Y")
#
PosX = 5
PosY = 0.8
t1 <- TeX(paste("$\\mathbf{\\left[\\frac{M}{L}\\right] =",
                 sprintf(fmt="%.3f",XML),
                 "}$",
                 sep=""))
text(PosX,PosY, t1, adj=c(0,0), cex=1.0, font=2)

dev.off()

Result:

enter image description here

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75