Skip to main content
2 of 3
edited title
user64494
  • 29.1k
  • 4
  • 29
  • 56

How to rotate text in user coordinate system?

Applying Rotate to text rotates it relative to the Cartesian page coordinates. I want it rotated relative to the current user coordinate system. To see what I mean, consider the following manipulation. When the AspectRatio is 1, all is good; but otherwise not. (I've included a plot for an apect ratio of 1/2, to illustrate.) How can I keep the text on the line?

Manipulate[With[{f = x |-> (x + 0.4)^2, x0 = 1, x1 = 2},
  Module[{y0 = f[x0], y1 = f[x1]},
   Graphics[{
     Line[{{x0, y0}, {x1, y1}}],
     Text[
      Rotate["test", ArcTan[1, (y1 - y0)/(x1 - x0)]], {(x0 + x1)/2, (y0 + y1)/2}, {0, -2}]
     },
    PlotRange -> {{0, 6}, {0, 6}},
    Axes -> True, AspectRatio -> a,
    AxesOrigin -> {0, 0}]]],
 {{a, 1}, 1/2, 1}]

outcome for aspect ratio of 1/2

More generally, it seems that Mathematica has much to learn from TikZ about making annotation easy for users.

Alan
  • 13.8k
  • 21
  • 41