5

I've mostly been able to coerce expression() into giving me the mathematical output I've needed for graphs, but I'm flummoxed by this one.

text(-2,.21,expression(P(a <= X)), cex=1.2)

gives me the expected result, but

text(-2,.21,expression(P(a <= X <= b)), cex=1.2)

fails with an arrow pointing to the second \le. Pasting 2 pieces together doesn't work, either, as <= requires both left and right tokens; i.e. this also fails:

text(-2,.21,expression(<= X), cex=1.2)

Any ideas? It's kind of frustrating that there doesn't seem be any documentation on how the expression token parser works short of looking at the source code. The only documentation seems to be a couple of different color versions of this:

http://stat.ethz.ch/R-manual/R-patched/library/grDevices/html/plotmath.html

plannapus
  • 18,529
  • 4
  • 72
  • 94
pgoetz
  • 848
  • 1
  • 8
  • 18

1 Answers1

8

You need to group the operators in plotmath - in otherwords you have to be explicit about the precedence. To do this invisibly, wrap the sub-statements in { } as in:

R> plot(1:10, type = "n")
R> text(5, 5, expression(P(a <= {X <= b})), cex = 1.2)

Which gives

enter image description here

This is documented (if you know what it means) towards the end of the list of markup plotmath understands.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453