4
$\begingroup$

Is there an option to align Text to automatically adjust for font descent?

In the attached graphic, strings that contain "g" are visibly shifted upward, even though all strings are uniformly rendered by:

Text[Style[name, FontFamily -> "Helvetica", 
  FontSize -> 12], {(b + 20), y}, {-1, -0.85}]

enter image description here

Is there a way to align strings vertically by the font's baseline? You can also see this issue in a simpler example:

  Graphics[
     {Line[{{0, 0}, {1, 0}}],
      Text["g", {1/4, 0.01}],
      Text["h", {3/4, 0.01}]},
     PlotRange -> {All, {-1, 1}}
     ]

which somehow doesn't replicate the issue (M9/OSX10.8)

enter image description here

$\endgroup$
5
  • $\begingroup$ How do you want it adjusted? And with respect to what? Also, I think you need to post more code. Context is important here. An example pruned down to two or three time lines will do. $\endgroup$
    – m_goldberg
    Commented Apr 16, 2014 at 2:32
  • $\begingroup$ @m_goldberg, edited accordingly $\endgroup$ Commented Apr 16, 2014 at 2:38
  • 1
    $\begingroup$ Your sample code doesn't reproduce your problem on my system. Both h and g are aligned to same baseline, with the descender of the g going below the line. Might be good to discuss this in chat. I'll open a chat room. $\endgroup$
    – m_goldberg
    Commented Apr 16, 2014 at 2:43
  • $\begingroup$ Where is the link to the chat please? $\endgroup$ Commented Apr 16, 2014 at 3:04
  • $\begingroup$ Click on the stack exchange logo on the site toolbar. On the drop down menu, click on "chat" $\endgroup$
    – m_goldberg
    Commented Apr 16, 2014 at 3:06

1 Answer 1

5
$\begingroup$

The problem stems from the y-shift introduced by the 3rd argument to Text, which is in a different coordinate system from the main plot. You need to get rid of the y-shift in the 3rd argument and do the shift in the 2nd argument.

Change

Text[Style[name, FontFamily -> "Helvetica", FontSize -> 12], {(b + 20), y}, {-1, -0.85}]

to

Text[Style[name, FontFamily -> "Helvetica", FontSize -> 12], {(b + 20), y + .35}, {-1, 0}]

and see if that works.

time-lines

You may have to tweak the 0.35 shift. I set that by trial-and-error.

Edit

OP's code as modified by me.

q = 
  {"roentgen" -> {1845, 1923}, "lorentz" -> {1853, 1928}, 
   "zeeman" -> {1865, 1943}, "van der waals" -> {1837, 1923}, 
   "starck" -> {1874, 1957}, "einstein" -> {1879, 1955}, 
   "bohr" -> {1885, 1962}, "ehrenfest" -> {1880, 1933}, 
   "born" -> {1882, 1970}, "schroedinger" -> {1887, 1961}, 
   "pauli" -> {1900, 1958}, "de broglie" -> {1892, 1987}, 
   "heisenberg" -> {1901, 1976}, "dirac" -> {1902, 1984}, 
   "jordan" -> {1902, 1980}, "planck" -> {1858, 1947}, 
   "sommerfeld" -> {1868, 1951}, "brillouin" -> {1889, 1969}};

Module[{color1 = Darker @ Darker @ Blue}, 
  Graphics[({#1, #2[[1]]} & ~MapIndexed~
    SortBy[q, #[[2, 1]]&]) /. {name_ -> {b_, d_}, y_} :> {color1, 
    Text[Style[name, FontFamily -> "Helvetica", FontSize -> 12], 
      {(b + 20), y + .35}, {-1, 0}], 
    Thickness[0.0005], Line[{{b, y}, {d, y}}], PointSize[0.005], 
    Point[{b, y}], Point[{d, y}]}, 
    ImageSize -> 1000, 
    AspectRatio -> 1/3, 
    FrameStyle -> Directive[{Gray, FontSize -> 18, FontFamily -> "Helvetica"}], 
    Frame -> {Automatic, None, None, None}]]
$\endgroup$
3
  • $\begingroup$ Thank you, that was simple after all $\endgroup$ Commented Apr 16, 2014 at 4:18
  • 2
    $\begingroup$ +1, but where is the code to generate this graphic? Is it from the documentation? $\endgroup$
    – Mr.Wizard
    Commented Apr 16, 2014 at 5:33
  • $\begingroup$ @Mr.Wizard. Edited answer to add code. It was posted in the chat room where we discussed this question. Sorry I didn't think to show it. $\endgroup$
    – m_goldberg
    Commented Apr 16, 2014 at 12:37

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.