3
$\begingroup$

There is an absolutely wonderful answer by Heike at Generate a Unit Circle Trigonometry. It is an answer that will lead me to a lot of learning.

Here is my playing with her code:

Graphics[{
  Circle[],
  {LightGray, Line[{{0, 0}, pt[#]}] & /@ alist},
  PointSize[Medium], Blue, Point[pt /@ alist],
  Text[pt[5 Pi/6], pt[5 Pi/6], -2 pt[5 Pi/6], Background -> Yellow],
  Text[pt[Pi/6], pt[Pi/6], -2 pt[Pi/6], Background -> Yellow],
  Red, PointSize[Large],
  Point[-2 pt[Pi/6]],
  Point[-2 pt[5 Pi/6]],
  }]

Which gives this image:

enter image description here

Now I worked through the examples in the documentation for the Text command and found that:

Graphics[{
  Text[Framed["ABCDEF"], {0, 0}, {-1, -1}],
  Circle[]
  }, Axes -> True, Ticks -> None]

Puts the lower left corner of the textbook at the origin:

enter image description here

But then I am not sure why this moves the textbook as it does.

Graphics[{
  Text[Framed["ABCDEF"], {0, 0}, {-2, -1}],
  Circle[]
  }, Axes -> True, Ticks -> None]

enter image description here

Then, of course, I'd like to get back to the brilliant code by Heike, and understand how his labels work so wonderfully in my first block of code above.

I'd like to be able to understand this so well that I can explain it to my students and to faculty in one of my Mathematica sessions.

$\endgroup$
1
  • 2
    $\begingroup$ For a long time now (version 6?) Text has just been a proxy for Inset so if you want to really understand it look at that documentation instead. $\endgroup$
    – Mr.Wizard
    Commented Jun 4, 2016 at 22:49

2 Answers 2

3
$\begingroup$

I find it easier to use the 2nd argument of Text to do all the positioning and to use its 3rd argument for aligning. That is I treat Text as having the formal arguments

Text[expression, position, alignment]

I define the following constants for convenience.

align[Right] = {1, 0};
align[Center] = {0, 0} ;(* default *)
align[Left] = {-1, 0};

Here's how it works in practice. I will draw a disk and put two lines of text below it.

Without a 3rd argument, the text is centered, the default alignment.

Module[{center, r, dy},
  center = {0.5, 0.7};
  r = .2;
  dy = -.1;
  Graphics[
    {Red, Disk[Scaled[center], Scaled[r]],
     Black,
       Text["A short line", Scaled[center + {0, -r + dy}]], 
       Text["A much longer line of text", Scaled[center + {0, -r + 2 dy}]]},
    Frame -> True,
    BaseStyle -> {FontSize -> 14}]]

example1

But it would look better if it were left justified, so I use the 3rd argument.

Module[{center, r, dy},
  center = {0.5, 0.7};
  r = .2;
  dy = -.1;
  Graphics[
   {Red, Disk[Scaled[center], Scaled[r]],
    Black,
     Text["A short line", Scaled[center + {0, -r + dy}], align[Left]], 
     Text["A much longer line of text", Scaled[center + {0, -r + 2 dy}], align[Left]]},
   Frame -> True,
   BaseStyle -> {FontSize -> 14}]]

example2

The text is now properly justified but it needs to be shifted to left by the radius of the disk. So I adjust the position using the 2nd argument.

Module[{center, r, dy},
  center = {0.5, 0.7};
  r = .2;
  dy = -.1;
  Graphics[
   {Red, Disk[Scaled[center], Scaled[r]],
    Black,
     Text["A short line", Scaled[center + {-r, -r + dy}], align[Left]], 
     Text["A much longer line of text", Scaled[center + {-r, -r + 2 dy}], align[Left]]},
   Frame -> True,
   BaseStyle -> {FontSize -> 14}]]

example3

Now the text looks as I wish it to. I often find this way of working with Text useful and convenient.

I do not claim this method fits all situations, but I thought I would bring it to the attention of others who might also find it useful and convenient.

$\endgroup$
1
  • $\begingroup$ Very helpful, thank you. $\endgroup$ Commented Jun 5, 2016 at 1:53
2
$\begingroup$

I understand, you want to know, how the 3rd argument of the Text works. To me it seems it is answered in the documentation of the Text function.

Here is the relevant part:

Text[expr,coords,offset] specifies an offset for the block of text relative to the coordinates given. Giving an offset {sdx,sdy} specifies that the point {x,y} should lie at relative coordinates {sdx,sdy} within the bounding rectangle that encloses the text. Each relative coordinate runs from -1 to 1 across the bounding rectangle.

If you use the offset to -2 you set the point given by coords outside of your element.

Small additional vertical offset is caused by Framed and can be removed by adding option BaselinePostition->{Bottom->Bottom}.

In the code by Heike he makes use of the fact, that pt[5 Pi/6] is vector of coordinates and for the offset he uses this vector for a direction for an additional shift. -2 pt[5 Pi/6] is in the relative (to frame size) coordinates of the label pushing the label away from the coordinates of pt[5 Pi/6].

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.