I like to use Mathematica text cells for creating draft technical documents. When entering equations in a text cell, the ^ (hat accent) does not dynamically adjust its size to fit. It looks good if you enter x^, but if you wanted abc^, the ^ covers only the b. Anyone know how to make the ^ (and other accents) adjust to the size of the text?

  • $\begingroup$ Don't know how to do it automatically. You can change the font size of the hat when you use the upper script (ctrl-7). Still much work when you have to type the hat alot. $\endgroup$ – Lou Mar 10 '12 at 20:21
  • 3
    $\begingroup$ Good question: how to create a custom extensible character $\endgroup$ – Rojo Mar 10 '12 at 20:40
  • 1
    $\begingroup$ Okay, someone had to do it. $\endgroup$ – Mr.Wizard Mar 11 '12 at 14:29

This answer is just a quick hack. I think that to make true extensible character might not be something that an end-user can do...

Anyway, redefine the formatting for OverHat using

OverHat /: MakeBoxes[OverHat[a_], form_] := 
 With[{s = First[Rasterize[a, "RasterSize"]], 
   ab = MakeBoxes[a]}, With[{sl = N[2 Log[2 s]]},
     TagBox[GraphicsBox[LineBox[{{-s, 0}, {0, sl}, {s, 0}}], 
       ImageSize -> {s, Automatic}], "LongOverHat", 
      Selectable -> False]], OverHat[a]]]]

Then, just type in the expression as per normal, e.g., abc 1, and then select the OverHat expression and reformat the boxes using the menu command Cell > Convert To > Standard From (which can also be done using the shortcut CtrlShiftN) to get abc 2. The FullForm of this expression is OverHat[abc].

Here's an example showing how the modified OverHat scales for longer expressions

Powers of ten

The MakeBoxes command also works with TraditionalForm (CtrlShiftT), but you might want to modify it to include an AdjustmentBox[..., BoxBaselineShift -> b] in order to get the vertical spacing looking right.

  • 2
    $\begingroup$ Rasterize[..., "BoundingBox"] and Rasterize[..., "RasterSize"] may be of interest. $\endgroup$ – Szabolcs Mar 11 '12 at 9:55
  • $\begingroup$ Nice work Simon. +1 $\endgroup$ – Mr.Wizard Mar 11 '12 at 10:00
  • $\begingroup$ Thanks @Szabolcs - it is of interest! And thanks Mr.Wizard. $\endgroup$ – Simon Mar 11 '12 at 14:24
  • $\begingroup$ this is great - thank you for the quick response $\endgroup$ – gamma Mar 13 '12 at 7:05
  • $\begingroup$ Not a problem @gamma. If you're happy with the answer, press the checkmark button at the top left of the answer. Also, maybe submit a request to Wolfram Research for a proper extensible circumflex glyph... $\endgroup$ – Simon Mar 13 '12 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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