Skip to main content
Tweeted twitter.com/StackMma/status/934553429783056385
edited tags
Link
Alexey Popkov
  • 62.5k
  • 7
  • 163
  • 405
Source Link
Szabolcs
  • 238.9k
  • 32
  • 653
  • 1.3k

Change the default font in all plots and legends, even when exporting to PDF

This is a followup to:

Carl Woll gave a great answer there: change the "Graphics" style of the current notebook's stylesheet. This is simple to do and indeed affects the style in all GraphicsBoxes.

Unfortunately, the style change does not stick when we export a legended figure to PDF.

Question: How can we work around this problem and preserve the new style even when exporting to PDF?


Demonstration:

We set the "Graphics" style for all style environments:

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

Additionally, we change the printing style environment to "Working". Otherwise Export would use the "Printout" environment for labelled plots, which would downscale everything.

SetOptions[$FrontEndSession, PrintingStyleEnvironment -> "Working"] 

Let's try exporting these two plots:

plot1 = DensityPlot[x, {x, -1, 1}, {y, -1, 1}, PlotLabel -> "Plot 1"] plot2 = DensityPlot[x, {x, -1, 1}, {y, -1, 1}, PlotLabel -> "Plot 2", PlotLegends -> Automatic] 

Both use the Times font when displayed in the notebook, but plot2 will revert to the default font when exported to PDF:

enter image description here

enter image description here


Note: To be more precise, the problem appears when we export something that is not represented as a GraphicsBox. This is the case with Legended expressions when the legend is placed outside of the plot frame. Row[{plot1}] also doesn't export correctly.