Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
    
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. –  m_goldberg Apr 16 at 2:32
    
@m_goldberg, edited accordingly –  alancalvitti Apr 16 at 2:38
1  
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. –  m_goldberg Apr 16 at 2:43
    
Where is the link to the chat please? –  alancalvitti Apr 16 at 3:04
    
Click on the stack exchange logo on the site toolbar. On the drop down menu, click on "chat" –  m_goldberg Apr 16 at 3:06

1 Answer 1

up vote 5 down vote accepted

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}]]
share|improve this answer
    
Thank you, that was simple after all –  alancalvitti Apr 16 at 4:18
2  
+1, but where is the code to generate this graphic? Is it from the documentation? –  Mr.Wizard Apr 16 at 5:33
    
@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. –  m_goldberg Apr 16 at 12:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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