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.

One of the requirements set for the graphing project I am working on is that plot and axis unit labels sometimes need to include superscripts. Here is an approximately minimal example (I have some plotting options set differently from the default, but these shouldn't matter here):

fakedata101 = 
  FoldList[0.92 #1 + #2 &, 0., 
   RandomVariate[NormalDistribution[0, 0.5], 100]];
ListLinePlot[fakedata101, Filling -> Axis, PlotStyle ->Red, 
 FillingStyle -> Red, 
 PlotLabel -> "Test Label in k(\!\(\*SuperscriptBox[\(m\), \(2\)]\))"]

enter image description here

The SingleLetterItalics option appears to be turned off in the Options Inspector, but that doesn't seem to work for String-format text labels. It does work if set explicitly, e.g. when adding Text in an Epilog with an explicit Style wrapping:

Epilog -> 
 Text[Style["\!\(\*SuperscriptBox[\(Text with Footnote marker\), \((a)\)]\)", 
   SingleLetterItalics -> False], {10, -1.5}]

Is there a way I can robustly turn off SingleLetterItalics in styled text such as this? I would prefer not to have to catch every String passed to a PlotLabel or AxisLabel and send it through a Style command if I could help it.

share|improve this question
2  
What I personally do to avoid this is the following: before entering a math-expression, I open a new subcell by pressing Ctrl-9. Then I type $m^2$. Then I select m and press Ctrl-I to make the italic into upright type. The input for of what I get is "k (\!\(\*Cell[TextData[Cell[BoxData[\nFormBox[\n\n \ SuperscriptBox[\nStyleBox[\"m\",\nFontSlant->\"Plain\"], \"2\"], \n \ TraditionalForm]],\nFormatType->\"TraditionalForm\"]]]\))" I know it looks awful here, just paste it back into a notebook and 'evaluate' it to see what it looks like. Move the cursor on m to see the subcell. –  Szabolcs May 13 '12 at 12:38
    
A related question is "Fixing quirky typesetting in plot labels". In my answer there, I describe the "interactive" method similar to what @Szabolcs mentions above, which works without problem for your plot labels. –  Jens May 13 '12 at 16:00
    
By the way, I still think this is an interesting question because it points to the rather byzantine methods one has to use to achieve a seemingly simple option setting, see the answer by @Mr.Wizard. –  Jens May 13 '12 at 16:04
    
One consideration I should have mentioned is that this is a graphing package that is to be used by many other people not familiar with Mathematica. So I'd like to ensure that users don't have to insert labels "the exact right" (and perhaps a bit convoluted) way. –  Verbeia May 13 '12 at 20:34
    
@Verbeia: your last comment is of course an important little detail, which will probably make my answer useless :-). How are these users supposed to provide the input, then? Possible solutions will depend on that, I think... –  Albert Retey May 13 '12 at 20:55
show 3 more comments

4 Answers

up vote 11 down vote accepted

I don't know if it qualifies as an answer to your question if I suggest to change the structure of the labeling in the first place. As you write it, the m is -- from the rendering point of view -- treated as a symbol, if you inclose it with quotation marks it will be treated as a string and no auto-italic is performed at all. E.g.:

PlotLabel -> "Test Label in k(\!\(\*SuperscriptBox[\(\"m\"\), \\(2\)]\))"

instead of

PlotLabel -> "Test Label in k(\!\(\*SuperscriptBox[\(m\), \(2\)]\))"

another thing you should be aware of is that you can use arbitrary expressions as labels, e.g.:

PlotLabel -> Row[{"Test Label in k(", Superscript["m", 2], ")"}]

which I find much easier to automatically create and manipulate as well as manually maintain than those cryptic strings...

If, as your comments suggest, you don't have control over how these labels are created, the following could be the solution you are after:

ListLinePlot[fakedata101, Filling -> Axis, PlotStyle -> Red, 
  FillingStyle -> Red, 
  PlotLabel -> "Test Label in k(\!\(\*SuperscriptBox[\(m\), \(2\)]\))",
  FormatType -> (Style[TraditionalForm[##],SingleLetterItalics -> False] &)
]
share|improve this answer
add comment

It's in the style sheet. Use a custom style sheet to override the default.

Format > Edit Stylesheet... then enter style name: TraditionalForm

Open Format > Option Inspector... and set SingleLetterItalics to False

share|improve this answer
    
This works perfectly, but setting the FormatType option is something I can do in the package, rather than making everyone use a particular stylesheet for their notebooks. –  Verbeia May 13 '12 at 22:15
1  
Note to open the Option Inspector from the Format menu, not the Preferences dialog. Also have the TraditionalForm cell highlighted beforehand. The cell can also be manually edited using Control-Shift-E. –  Chris Degnen Jun 2 '12 at 18:30
    
@Chris thanks for the note. I updated the answer to include at least part of that, and upvoted the comment for visibility. –  Mr.Wizard Jun 2 '12 at 21:30
add comment

Having set the notebook style definition as described in Mr.Wizard's answer, the value can be obtained and applied to new notebooks programmatically.

I.e. In a manually style-edited notebook obtain the style definition:

newstyle = StyleDefinitions /. Options[SelectedNotebook[]]

Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]], Cell[StyleData["TraditionalForm"], SingleLetterItalics -> False, MenuSortingValue -> 10000]}, Visible -> False, FrontEndVersion -> "8.0 for Microsoft Windows (32-bit) (February 23, 2011)", StyleDefinitions -> "PrivateStylesheetFormatting.nb"]

This can then be applied in a new notebook, (possibly as part of a called function):

SetOptions[SelectedNotebook[], StyleDefinitions -> newstyle]
share|improve this answer
add comment

A problem with the given solutions exists at least in version 7.0.1 on Win7, (but not in version 8), in that a space is added to a string when Inset is used.

Starting with the default format this text appears in italics, with an extra space:

ab = Graphics[{{Yellow, Rectangle[{0, 0}, {1, 0.5}]}, 
   Inset["A&B", {0.5, 0.25}]}, AspectRatio -> 0.5, 
  PlotRange -> {{0, 1}, {0, 0.5}}, ImageSize -> 50, 
  BaseStyle -> {FontSize -> 14}]

enter image description here

Editing the style sheet's TraditionalForm or applying this solution clears the italics but leaves the space:

Append[ab, 
 FormatType -> (Style[TraditionalForm[##], 
     SingleLetterItalics -> False] &)]

enter image description here

This is my current blunderbuss solution:

(ab[[Sequence @@ #]] = 
    Style[ab[[Sequence @@ #]], SingleLetterItalics -> False]) & /@ 
 Complement[Position[ab, _String], 
  Append[#, 1] & /@ Position[ab, _Style]]; ab

enter image description here

And a solution suggested by Heike, to eschew use of Inset:

Graphics[{{Yellow, Rectangle[{0, 0}, {1, 0.5}]}, 
  Text["A&B", {0.5, 0.25}]}, AspectRatio -> 0.5, 
 PlotRange -> {{0, 1}, {0, 0.5}}, ImageSize -> 50, 
 BaseStyle -> {FontSize -> 14}]

enter image description here

(However, the graphics I have to modify already have extensive use of Inset.)

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.