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.

I'm trying to programmatically get the width of a character in Mathematica's default fixed-width font, Courier. I believe CurrentValue holds the answer but I'm having trouble understanding correct usage.

Four examples:

In[1]:= CurrentValue[$FrontEnd, FontFamily]
Out[1]= "Times"

Example 1: Expected Courier, since input is typed in Courier.

In[2]:= s = Style["Confusion", FontFamily -> "Times"]; CurrentValue[s, FontFamily]
Out[2]= "Courier"

Example 2: Expected Times, since I specifically chose that font family for s.

In[3]:= CurrentValue[s, "FontMWidth"] / CurrentValue[s, "FontNWidth"]
Out[3]= 2.

Example 3: Expected exactly 1, since since all letters should be equally wide in Courier.

In[4]:= CurrentValue[$FrontEnd, "FontMWidth"] / CurrentValue[$FrontEnd, "FontNWidth"]
Out[4]= 2.

Example 4: Expected about 1.2, based on a quick measurement of the width ratio of Times' M to N.

A prior question about fonts and CurrentValue has an example of using DynamicWrapper to get information out of dynamic objects, but I'm still curious how to use CurrentValue correctly in non-dynamic settings, and why the ratio of FontMWidth/FontNWidth is always 2.

I'm on Mac OS X 10.8.5 and Mathematica 9.0.1.0.

share|improve this question
    
discussion here may be relevant: mathematica.stackexchange.com/questions/17795/… –  Mike Honeychurch Nov 4 '13 at 3:54
    
@Mike Thanks, right on. –  duozmo Nov 6 '13 at 2:34
add comment

1 Answer

After additional research, I believe I can (mostly) explain all four examples.

Example 1: CurrentValue is returning the default font for the front end, not the font you're typing in. It's the same as looking at Options Inspector→Formatting→Font→FontFamily, or evaluating:

Options[$FrontEnd, FontFamily]
(*
  {FontFamily -> "Times"}
*)

Example 2: CurrentValue is returning the font of the input cell. You can change the cell font and get different results.

CurrentValue output from cell font change

CurrentValue is likely falling back on a default response for cases where it's given an object it doesn't have specific behavior for, although this is conjecture*. Broadly speaking, CurrentValue is for interacting with the front end, so it's little surprise that s, or any such symbol, is not among by CurrentValue's repertoire. The objects CurrentValue can report on are in the reference documentation and the M.SE question "Items known by CurrentValue."

* Tracing CurrentValue reveals a MathLink call to the front end, at which point my investigative skills diminish.

Examples 3 & 4: These appear to be related to a genuine bug with calculating character width on OS X.

share|improve this answer
add comment

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.