8
$\begingroup$

When I do

ListPlot[Table[RandomReal[NormalDistribution[], {20, 2}], {2}], 
 PlotStyle -> PointSize[0.02], 
 Epilog -> 
  Style[Text["b: 5%", Scaled[{0.05, 0.05}], {-1, -1}], 
   FontFamily -> "Helvetica"]]

enter image description here

the percent symbol is shown in the wrong typeface, possibly Times. How can I make it display in the correct face?

$\endgroup$
1
  • 1
    $\begingroup$ Unfortunately, the only way in which I've ever been able to achieve fully consistent typesetting in Mathematica is to use Times font for everything, or to use external $\LaTeX$ processing. Since the default notebook style has changed to Arial in version 9, many inconsistencies like the one you observe are revealed, and I haven't found a good answer how to fix that while using sans serif fonts. $\endgroup$
    – Jens
    Jan 10, 2013 at 18:38

3 Answers 3

12
$\begingroup$

Mathematica automatically replaces many operators found in normal text fonts with operators in a Mathematica font. You can change that behavior as follows:

SetOptions[EvaluationNotebook[], PrivateFontOptions -> {"OperatorSubstitution" -> False}]

Test:

ListPlot[Table[RandomReal[NormalDistribution[], {20, 2}], {2}], 
   PlotStyle -> PointSize[0.02], 
   Epilog -> Style[Text["b: 5%", Scaled[{0.05, 0.05}], {-1, -1}], 
                    FontFamily -> "GiddyupStd", 20]
]

Mathematica graphics

$\endgroup$
9
  • $\begingroup$ In the notebook, this works indeed (+1), but when you export it to PDF the problem re-appears. Also, it re-appears of course when you copy the graphic to another notebook. So the substitution doesn't appear to get turned off for good, unfortunately. $\endgroup$
    – Jens
    Jan 10, 2013 at 20:48
  • 1
    $\begingroup$ @Jens The OP says nothing about EPS. The question is: "How can I make it display in the correct face?". Other Notebooks can be served by replacing EvaluationNotebook[] with $FrontEnd. $\endgroup$ Jan 10, 2013 at 20:50
  • $\begingroup$ @Jens, you can set this globally using SetOptions[Graphics,BaseStyle -> {PrivateFontOptions -> {"OperatorSubstitution" -> False}} ] or SetOptions[$FrontEnd, PrivateFontOptions -> {"OperatorSubstitution" -> False}] $\endgroup$
    – Verbeia
    Jan 10, 2013 at 22:08
  • $\begingroup$ @verbeia Uhm... The latter part is already in my previous comment $\endgroup$ Jan 10, 2013 at 22:11
  • $\begingroup$ @Verbeia Using $FrontEnd as @Sjoerd said seems to fix the problem with Export too. It works for percent and a few other special cases, but not in general. For example, take the character \[HBar]. $\endgroup$
    – Jens
    Jan 10, 2013 at 22:18
7
$\begingroup$

An addition to Sjoerd's answer.

Style can be used for setting global FrontEnd or Notebook preferences locally. So it is better to move the PrivateFontOptions -> {"OperatorSubstitution" -> False} option directly in the Style expression rather than changing global preferences. It also solves the problems with Export and copying the graphics between notebooks:

ListPlot[Table[RandomReal[NormalDistribution[], {20, 2}], {2}], 
 PlotStyle -> PointSize[0.02], 
 Epilog -> 
  Style[Text["b: 5%", Scaled[{0.05, 0.05}], {-1, -1}], 
   FontFamily -> "Helvetica", 20, 
   PrivateFontOptions -> {"OperatorSubstitution" -> False}]]

Export["test.pdf", %]

screenshot

$\endgroup$
6
  • $\begingroup$ I think I knew this, but I had forgotten. Thanks for the reminder. +1 $\endgroup$
    – Mr.Wizard
    Jan 11, 2013 at 9:19
  • $\begingroup$ +1 I like this much better than the global $FrontEnd setting because the latter severely messes up the display of things like MatrixForm[...] where custom parentheses "operators" are needed. $\endgroup$
    – Jens
    Jan 11, 2013 at 19:57
  • $\begingroup$ Unfortunately, even with your method the formatting is wrong if your plot also includes a matrix with expanded parentheses. The ( and ) surrounding a matrix will simply become fixed height symbols because turning off the operator substitution prevents Mathematica from building them out of extensible parts. $\endgroup$
    – Jens
    Jan 11, 2013 at 20:25
  • $\begingroup$ @Jens You can always set local Style for single elements and combine them with Row or use low-level constructs like StyleBox and RowBox. For example, MatrixForm[{{Row[{1,Style["%",PrivateFontOptions->{"OperatorSubstitution"->False}]},BaseStyle->FontFamily->"Helvetica"],2},{3,4}}]. $\endgroup$ Jan 11, 2013 at 20:35
  • $\begingroup$ @AlexeyPopkov Sure - but then you're back to what I called "finger painting" in my answer... but at least you're getting nice looking output with your approach. $\endgroup$
    – Jens
    Jan 11, 2013 at 20:47
0
$\begingroup$

It will take some finger-painting to cobble together a combination of different fonts that look right together and have a percent sign that looks like sans-serif . Not a real solution, but maybe this will look more consistent to you:

Style["label text: 5 %", FontFamily -> "Monaco"]

enter image description here

Or you could do this:

Row[{Style["label text: 5", FontFamily -> "Helvetica"], 
  Style["%", FontFamily -> "Monaco"]}]

enter image description here

Edit

Looking at the exported PDF file containing Style[5 %, FontFamily -> "Helvetica"] in a text editor, there is in fact a font descriptor for the Mathematica1 font embedded where the percent sign should be rendered. One can manually delete the descriptors containing Mathematica1 to get a PDF that displays the % correctly in an external PDF viewer. However, I'm not sure if this can be done automatically in a sufficiently robust way. And the problem remains that such modified PDF files can't be directly re-imported into Mathematica unless you first outline the fonts as described here.

$\endgroup$

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.