10
$\begingroup$

The command CurrentValue (documentation) allows you to access various properties of fonts, such as x-height, line height, and various measures of width such as n-width and m-width. This has obvious applications in determining appropriate sizes of objects so that the text contained within them fits.

However it is not obvious how to pick up the characteristics of the font of styled output. For example, the following uses the width of the font in the input cell, not the selected style in the code (example adopted from this question).

 Framed @ Graphics[Text[
  Style["how quickly daft jumping zebras vex", 
   FontFamily -> "Verdana", FontSize -> 20]], 
 ImageSize -> CurrentValue["FontNWidth"]*35]

enter image description here

If I select the whole input cell (by its bracket, not just the text) and change the font, it works better, although of course "FontNWidth" is not a perfect indicator of actual width of characters:

enter image description here

How can I concisely get the CurrentValue dimensions from an arbitrarily chosen font?

$\endgroup$
0

1 Answer 1

7
$\begingroup$

You could do something like

{Framed@Graphics[
   Text[Style[DynamicWrapper["how quickly daft jumping zebras vex",
       p = CurrentValue["FontNWidth"]], FontFamily -> "Verdana", 
     FontSize -> 20]], ImageSize -> Dynamic[p]*35], Dynamic[p]}

Mathematica graphics

This code demonstrates that the font chosen for the styled output is definitely being picked up by CurrentValue.

Manipulate[{Framed@
   Graphics[
    Text[Style[
      DynamicWrapper["how quickly daft jumping zebras vex", 
       p = CurrentValue["FontNWidth"]], FontFamily -> fontfam, 
      FontSize -> i]], ImageSize -> Dynamic[p]*42], Dynamic[p]}, {i, 
  10, 40}, {fontfam, {"Verdana", "Arial", "TimesNewRoman", "Tahoma"}}]

enter image description here

$\endgroup$
11
  • $\begingroup$ Neat! Someday, I should start looking into Dynamic*... $\endgroup$
    – rm -rf
    Feb 13, 2012 at 10:11
  • $\begingroup$ I don;t see a need for a dynamic wrapper @Heike. As per my answer you can just make the current value Dynamic directly. $\endgroup$ Feb 13, 2012 at 21:10
  • $\begingroup$ @MikeHoneychurch That doesn't work on my system. If I run your edited code I always get a value of 7.02114 for CurrentValue["FontNWidth"] independent of FontSize and FontFamily. $\endgroup$
    – Heike
    Feb 13, 2012 at 21:24
  • $\begingroup$ @MikeHoneychurch but it seems to be the difference that makes it work. Consider Manipulate[{Framed@ Graphics[ Text[Style[ DynamicWrapper["how quickly daft jumping zebras vex", p = CurrentValue["FontNWidth"]], FontFamily -> "Verdana", FontSize -> i]], ImageSize -> Dynamic[p]*46], Dynamic[p]}, {i, 10, 40}] $\endgroup$
    – Verbeia
    Feb 13, 2012 at 21:26
  • $\begingroup$ @Heike what are you using? I wrote my answer with 8.0.4 on OS X 10.6.8 and it works fine -- as per screen grabs. $\endgroup$ Feb 13, 2012 at 21:36

Your Answer

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

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