# [Rd] plotmath indices: suggested addition to help file

Claudia Beleites cbeleites at units.it
Fri Jan 21 19:25:48 CET 2011

```Dear all,

after realizing yesterday that my suggestion was not yet quite "ripe" as the
character version is very limited, I decided to wait a bit before summarizing
the outcome to a better proposition instead of creating too much fuss.

Martin, thanks for your support & systematic table :-)

My main point is that somehow I didn't at all expect plotmath to throw away
everything after the i of expression (x [i,j]). I think this "trap" deserves
either change (nice to have; I now believe that this may be quite easy, see
below) or at least an announcement.

I agree that one needs to learn dealing with plotmath. On the other hand, it is
nice if plotmath's output corresponds to the R meaning of the expression.

I guess the underlying problem is that we have three different possibilities
with indexing:

R ---> how I'd write down the R meaning
(a)   x [i, j]  ---> x_{i, j} or x_{i j} (not x_{ij})
(b)   x [i [j]] ---> x_i_j
(c)   x [i][j]  ---> (x_i)_j

(b) works as expected, though a tiny little bit more default space would be
excellent.
(a) produces plotmath output x_i which is _very_ unexpected, and
(c) produces output that looks very similar to my expectation for (a) and can
thus be used to cheat around unexpected behaviour of (a).
I'd say the behaviour of (c) is fine, and it is up to me to write (x [i])[j].
Note that writing (x [i]) [j] does not change the R meaning of the expression.

BTW: I prefer a comma for (a) because i couldn't distinguish the output of
expression (x [i][j])
from
expression (x [ij])
.
I also vote for a comma for the future behaviour of (a) - even with some space
it's easier to distinguish two indices if they are separated by a comma (and no
comma can still be obtained by x[i][j]). Plus, it may be easier to implement it
that way.

I tried:
CDDR instead CADDR in RenderSub (plotmath.c) yields all indices given in the
square bracket. Now I prepended install ("list") by LCONS, and it seems to work:
plot (1,1, type = "n"); text (1, 1, expression (x [i[a,b,c],j,k]*f(x)), cex = 3)
now looks as I expect.

Disclaimer: This is the first time ever I tried to do something with the R C
interface, and I didn't even properly read the maual and SoDa chapters. So I
have no idea whether I did something that will cause trouble.

Best regards & have a nice weekend

Claudia

cb at cb:~/r-devel\$ svn diff src/main/plotmath.c
Index: src/main/plotmath.c
===================================================================
--- src/main/plotmath.c	(revision 54062)
+++ src/main/plotmath.c	(working copy)
@@ -898,6 +898,7 @@
mathContext*, pGEcontext , pGEDevDesc);
static BBOX RenderExpression(SEXP, int, mathContext*, pGEcontext , pGEDevDesc);
static BBOX RenderSymbolChar(int, int, mathContext*, pGEcontext , pGEDevDesc);
+static BBOX RenderCommaList(SEXP, int, mathContext *, pGEcontext, pGEDevDesc);

/*  Code to Generate Bounding Boxes and Draw Formulae.	*/
@@ -1396,7 +1397,8 @@
{
BBOX bodyBBox, subBBox;
+    SEXP sub = CDDR(expr);
+    SEXP list = install ("list");
STYLE style = GetStyle(mc);
double savedX = mc->CurrentX;
double savedY = mc->CurrentY;
@@ -1407,6 +1409,7 @@
s5 = TeX(sigma5, gc, dd);
s16 = TeX(sigma16, gc, dd);
SetSubStyle(style, mc, gc);
+    sub = LCONS (list, sub);
subBBox = RenderElement(sub, 0, mc, gc, dd);
v = max(max(v, s16), bboxHeight(subBBox) - 0.8 * sigma5);
subBBox = RenderOffsetElement(sub, 0, -v, draw, mc, gc, dd);

--
Claudia Beleites
Dipartimento dei Materiali e delle Risorse Naturali
Università degli Studi di Trieste
Via Alfonso Valerio 6/a
I-34127 Trieste

phone: +39 0 40 5 58-37 68
email: cbeleites at units.it

```