10
$\begingroup$

Forgive me, but I cannot for the life of me figure out how to do this no matter how much I research. I really want to change the default font for all graphics, such as ArrayPlot, ContourPlot, DiscretePlot3D, etc. as well as the fonts used in any legends / axes / labels / (everything) to Times New Roman (I'm in Mathematica 10 so everything is sans serif by default). I know that I can set all of these manually usually by using something like Directive[FontFamily -> "Times New Roman"], but it's killing me to do this for every single graph. I've tried to use stylesheets to do this but I cannot figure out how to get them working. I'm sorry that this is so basic, but if you could just provide a simple, step-by-step solution to achieve this, I would be eternally grateful. Thank you very much.

$\endgroup$
5
$\begingroup$

You can control the fonts that are used through a stylesheet. First, note the style definition used by the DefaultBaseStyle GraphicsBox option:

Options[GraphicsBox, DefaultBaseStyle]

{DefaultBaseStyle -> "Graphics"}

This means that the following options are used unless overridden:

CurrentValue[{StyleDefinitions, "Graphics"}]

{CellMargins -> {{4, Inherited}, {Inherited, Inherited}}, CellGroupingRules -> "GraphicsGrouping", StripStyleOnPaste -> False, PageBreakWithin -> False, GeneratedCell -> True, CellAutoOverwrite -> True, ShowCellLabel -> False, DefaultFormatType -> DefaultOutputFormatType, ContextMenu -> {MenuItem["Cu&t Graphics Selection", "Cut"], MenuItem["&Copy Graphics Selection", "Copy"], MenuItem["&Paste into Graphic", FrontEnd`Paste[After]], Delimiter, MenuItem["&Drawing Tools", FrontEndExecute[{FrontEnd`NotebookOpen[ FrontEnd`FindFileOnPath["DrawingTools.nb", "PrivatePathsSystemResources"]]}]], MenuItem["&Get Coordinates", FrontEndExecute[FrontEnd`Select2DTool["GetCoordinates"]]], Delimiter, MenuItem["G&roup", "Group"], MenuItem["&Ungroup", "Ungroup"], Delimiter, MenuItem["Move to &Front", "MoveToFront"], MenuItem["Move to &Back", "MoveToBack"], MenuItem["Move For&ward", "MoveForward"], MenuItem["Move Bac&kward", "MoveBackward"]}, "AutoStyleOptionsHighlightMisspelledWords" -> False, LanguageCategory -> "NaturalLanguage", FormatType -> StandardForm, ShowStringCharacters -> False, NumberMarks -> False, CounterIncrements -> "Graphics", ImageMargins -> {{43, Inherited}, {Inherited, 0}}, Arrowheads -> 0.04, Dashing -> None, CapForm -> "Square", JoinForm -> {"Miter", 3.25}, LineColor -> GrayLevel[0], LineOpacity -> 1, EdgeColor -> GrayLevel[0], EdgeOpacity -> 1, EdgeThickness -> Medium, EdgeDashing -> None, EdgeCapForm -> "Square", EdgeJoinForm -> {"Miter", 3.25}, FrontFaceColor -> GrayLevel[0], BackFaceColor -> GrayLevel[0], FrontFaceOpacity -> 1, BackFaceOpacity -> 1, GraphicsColor -> GrayLevel[0], PointSize -> Absolute[3], Thickness -> Medium, Opacity -> 1, DrawEdges -> False, StyleMenuListing -> None, FontFamily -> "Arial", FontSize -> 10, FontWeight -> "Plain", FontColor -> GrayLevel[0], FontOpacity -> 1}

In particular note the option value for the font family:

CurrentValue[{StyleDefinitions, "Graphics", FontFamily}]

"Arial"

So, to have Mathematica use the font "Times New Roman" you can just change the stylesheet. For instance:

SetOptions[
    EvaluationNotebook[],
    StyleDefinitions -> Notebook[
        {
        Cell[StyleData[StyleDefinitions->"Default.nb"]],
        Cell[StyleData["Graphics"], FontFamily->"Times New Roman"]
        },
        StyleDefinitions->"PrivateStylesheetFormatting.nb"
    ]
]

Let's see this in action:

Plot[Sin[x], {x, 0, Pi}, PlotLabel->"FooBar"]

enter image description here

There may be other GraphicsBox options that may need adjusting, e.g., DefaultLabelStyle, which can be handled in the same way.

Addendum

A comment requested support for PDF export as well. PDF export uses the "Printout" style environment, so one needs to add:

Cell[StyleData["Graphics", "Printout"], FontFamily->"Times New Roman"]

to the style sheet as well. Another, perhaps better possibility, is to use All. So, the following should work:

SetOptions[
    EvaluationNotebook[],
    StyleDefinitions -> Notebook[
        {
        Cell[StyleData[StyleDefinitions->"Default.nb"]],
        Cell[StyleData["Graphics", All], FontFamily->"Times New Roman"]
        },
        StyleDefinitions->"PrivateStylesheetFormatting.nb"
    ]
]
$\endgroup$
  • 1
    $\begingroup$ Yes, I think the hidden point of all similar questions is 'how can I know which options/styles to adjust?'. "Graphics" is one of them, then Label/Axes-style options come in play, not to mention TraditionalForm etc. $\endgroup$ – Kuba Nov 23 '17 at 8:49
  • $\begingroup$ One can see richness of the setup by trying to set size and color aswell: Cell[StyleData["Graphics"], FontFamily -> "Times New Roman", FontSize -> 28, FontColor -> Black] and use this to compare: {Style["FooBar", "Graphics"], Plot[{Sin[x], Cos[x]}, {x, 0, Pi} , AxesLabel -> {"FooBar", "FooBar"} , PlotLegends -> "Expressions", ImageSize -> 400 , PlotLabel -> "FooBar"] // Labeled[#, "FooBar"] &} $\endgroup$ – Kuba Nov 23 '17 at 8:50
  • $\begingroup$ It seems that when I Export a figure like this to PDF, the new font style is not preserved. Is there an easy fix for this? $\endgroup$ – Szabolcs Nov 23 '17 at 16:19
  • $\begingroup$ I mean, I know that if I save the graphics using the context menu (right click -> Save Graphics As...) then the font will be preserved, but that method has other issues, such as imprecise output and lack of automatibility. I very much prefer to use Export. $\endgroup$ – Szabolcs Nov 23 '17 at 16:22
  • $\begingroup$ @Szabolcs PDF export uses the "Printout" environment, so you need to also specify Cell[StyleData["Graphics", "Printout"],FontFamily->"Times New Roman"]. I will add this to my answer. $\endgroup$ – Carl Woll Nov 23 '17 at 16:53
2
$\begingroup$

Try to see if this works for you. (add all the type of plots in the command below you want to set)

SetOptions[{Plot,ListPlot,ArrayPlot,ContourPlot,DiscretePlot3D},
    BaseStyle->{14,Directive[FontFamily->"Times New Roman"]}];

For BarLegend, it does not use BaseStyle so add this

SetOptions[{BarLegend},LabelStyle->{14,Directive[FontFamily->"Times New Roman"]}];

And now do

Plot[Sin[x],{x,-1,1},PlotLabel->"This is my plot", 
      AxesLabel->{"this is X","this is Y"}]

etc...

It will have the font family and size indicated in the SetOptions command for all plots.

Mathematica graphics

$\endgroup$
  • $\begingroup$ This is close, but it doesn't appear to change the text that appears in certain types of plot legends, such as "BarLegend." How would I account for that as well? $\endgroup$ – ereHsaWyhsipS Nov 23 '17 at 6:34
  • $\begingroup$ @JohnTravolski I have not tried it with BarLegend. This might be affected by the options of the plot used with the legened. It might have different baseStyle. It looks like BarLeged uses LabelStyle to set the font which does not have a BaseStyle option. $\endgroup$ – Nasser Nov 23 '17 at 6:36

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.