Skip to content

Commit bd74cd8

Browse files
committed
Python: pass usetex to plot_to_fig
1 parent c8a7664 commit bd74cd8

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

Utilities/Python/fdsplotlib.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -708,44 +708,39 @@ def get_data(E, spec, start_idx):
708708
return y, col_names
709709

710710

711-
def configure_fds_fonts():
711+
def configure_fds_fonts(**kwargs):
712712
import matplotlib.pyplot as plt
713713
import platform
714714

715715
system = platform.system()
716+
use_tex = kwargs.get('usetex', False)
716717

717718
if system == "Linux":
718-
# Linux: use Nimbus Roman as primary serif, with Times ahead of Times New Roman in the fallback chain
719719
primary_serif = "Nimbus Roman"
720720
serif_list = [
721-
"Nimbus Roman", # primary on Linux
721+
"Nimbus Roman",
722722
"Times",
723723
"Times New Roman",
724724
"serif",
725725
]
726726
else:
727-
# macOS ("Darwin") and Windows: prefer Times, then Times New Roman
728727
primary_serif = "Times"
729728
serif_list = [
730-
"Times", # first choice
729+
"Times",
731730
"Times New Roman",
732731
"Nimbus Roman",
733732
"serif",
734733
]
735734

736735
plt.rcParams.update({
737-
# Core-14 fonts for small PDFs (Times-Roman in output)
738736
"pdf.use14corefonts": True,
739-
"text.usetex": False,
737+
"text.usetex": use_tex,
740738

741-
# Make serif the default everywhere
742739
"font.family": "serif",
743740
"font.serif": serif_list,
744-
745-
# If something explicitly requests 'sans-serif', try to keep it Times-like too
746741
"font.sans-serif": serif_list,
747742

748-
# Math text: follow the same primary serif
743+
# Mathtext still matters when text.usetex=False
749744
"mathtext.fontset": "custom",
750745
"mathtext.rm": primary_serif,
751746
"mathtext.it": f"{primary_serif}:italic",
@@ -758,6 +753,13 @@ def configure_fds_fonts():
758753
"pdf.compression": 9,
759754
})
760755

756+
# Only for usetex=True, insert packages that Times-ify math
757+
if use_tex:
758+
plt.rcParams["text.latex.preamble"] = r"""
759+
\usepackage{newtxtext}
760+
\usepackage{newtxmath}
761+
"""
762+
761763

762764
def plot_to_fig(x_data,y_data,**kwargs):
763765
"""
@@ -774,8 +776,6 @@ def plot_to_fig(x_data,y_data,**kwargs):
774776

775777
plot_style = get_plot_style("fds")
776778

777-
configure_fds_fonts()
778-
779779
# plt.rcParams.update({
780780
# "pdf.use14corefonts": True,
781781
# "text.usetex": False,
@@ -828,17 +828,33 @@ def plot_to_fig(x_data,y_data,**kwargs):
828828
# if figure handle is passed, append to current figure, else generate a new figure
829829
if kwargs.get('figure_handle'):
830830
fig = kwargs.get('figure_handle')
831+
831832
if fig.axes:
832833
ax = fig.axes[0]
833834
else:
834-
# Ensure we have at least one axes
835835
ax = fig.add_subplot(111)
836+
836837
plt.figure(fig.number)
837838
using_existing_figure = True
839+
840+
# ---- restore original usetex for this figure ----
841+
use_tex = getattr(fig, "_fds_usetex", False)
842+
plt.rcParams["text.usetex"] = use_tex
843+
838844
else:
839845
fig = plt.figure(figsize=figure_size)
840846
using_existing_figure = False
841-
# Convert to fractions of the figure size:
847+
848+
# Take usetex from kwargs, default to False
849+
use_tex = kwargs.get('usetex', False)
850+
851+
# ---- apply font settings before any text is drawn ----
852+
configure_fds_fonts(usetex=use_tex)
853+
854+
# ---- record the choice on the figure for later calls ----
855+
fig._fds_usetex = use_tex
856+
857+
# Create axes
842858
ax_w = plot_size[0] / figure_size[0]
843859
ax_h = plot_size[1] / figure_size[1]
844860
left = plot_origin[0] / figure_size[0]

0 commit comments

Comments
 (0)