@@ -31,6 +31,13 @@ def to_gridspec_kw(self) -> dict[str, float]:
3131 return {"wspace" : self .wspace , "hspace" : self .hspace }
3232
3333
34+ def _parse_bool_env_var (name : str , default : bool = False ) -> bool :
35+ value = os .getenv (name )
36+ if value is None :
37+ return default
38+ return value .strip ().lower () in {"1" , "true" , "yes" , "on" }
39+
40+
3441def plot_matplotlib (tikzfigure : TikzFigure , ax , layers = None ):
3542 """
3643 Plot all nodes and paths on the provided axis using Matplotlib.
@@ -179,6 +186,7 @@ def __init__(
179186 dpi : int = 300 ,
180187 width : str = "5cm" ,
181188 ratio : str = "golden" , # TODO Add literal
189+ usetex : bool | None = None ,
182190 subplot_spacing : SubplotSpacing | None = None ,
183191 gridspec_kw : Mapping [str , float ] | None = None ,
184192 ):
@@ -196,6 +204,8 @@ def __init__(
196204 dpi (int): DPI for the figure. Default is 300.
197205 width (str): Width of the figure. Default is "17cm".
198206 ratio (str): Aspect ratio. Default is "golden".
207+ usetex (bool | None): Default text.usetex behavior for this canvas.
208+ If None, read from MAXPLOTLIB_USETEX environment variable.
199209 subplot_spacing (SubplotSpacing): Typed subplot spacing.
200210 Default is SubplotSpacing(wspace=0.08, hspace=0.1).
201211 gridspec_kw (Mapping[str, float]): Optional matplotlib gridspec kwargs.
@@ -212,6 +222,11 @@ def __init__(
212222 self ._dpi = dpi
213223 self ._width = width
214224 self ._ratio = ratio
225+ self ._usetex = (
226+ _parse_bool_env_var ("MAXPLOTLIB_USETEX" , default = False )
227+ if usetex is None
228+ else usetex
229+ )
215230 if subplot_spacing is not None and gridspec_kw is not None :
216231 raise ValueError ("Pass either subplot_spacing or gridspec_kw, not both." )
217232 if subplot_spacing is None and gridspec_kw is None :
@@ -764,34 +779,43 @@ def plot(
764779 backend : Backends = "matplotlib" ,
765780 savefig = False ,
766781 layers = None ,
782+ usetex : bool | None = None ,
767783 verbose : bool = False ,
768784 ):
785+ resolved_usetex = self ._usetex if usetex is None else usetex
786+
769787 if verbose :
770788 print (f"Plotting figure using backend: { backend } " )
771789
772790 if backend == "matplotlib" :
773791 return self .plot_matplotlib (
774792 savefig = savefig ,
775793 layers = layers ,
794+ usetex = resolved_usetex ,
776795 verbose = verbose ,
777796 )
778797 elif backend == "plotly" :
779- return self .plot_plotly (savefig = savefig )
798+ return self .plot_plotly (
799+ savefig = savefig ,
800+ usetex = resolved_usetex ,
801+ verbose = verbose ,
802+ )
780803 elif backend == "plotext" :
781804 return self .plot_plotext (
782805 savefig = savefig ,
783806 layers = layers ,
784807 verbose = verbose ,
785808 )
786809 elif backend == "tikzfigure" :
787- return self .plot_tikzfigure (savefig = savefig )
810+ return self .plot_tikzfigure (savefig = savefig , verbose = verbose )
788811 else :
789812 raise ValueError (f"Invalid backend: { backend } " )
790813
791814 def show (
792815 self ,
793816 backend : Backends = "matplotlib" ,
794817 layers : list | None = None ,
818+ usetex : bool | None = None ,
795819 verbose : bool = False ,
796820 ):
797821 if verbose :
@@ -802,11 +826,13 @@ def show(
802826 backend = "matplotlib" ,
803827 savefig = False ,
804828 layers = layers ,
829+ usetex = usetex ,
805830 verbose = verbose ,
806831 )
807832 # self._matplotlib_fig.show()
808833 elif backend == "plotly" :
809- self .plot_plotly (savefig = False )
834+ resolved_usetex = self ._usetex if usetex is None else usetex
835+ self .plot_plotly (savefig = False , usetex = resolved_usetex )
810836 elif backend == "plotext" :
811837 figure = self .plot_plotext (
812838 savefig = False ,
@@ -827,7 +853,7 @@ def plot_matplotlib(
827853 self ,
828854 savefig : bool = False ,
829855 layers : list | None = None ,
830- usetex : bool = False ,
856+ usetex : bool | None = None ,
831857 verbose : bool = False ,
832858 ):
833859 """
@@ -839,7 +865,8 @@ def plot_matplotlib(
839865 if verbose :
840866 print ("Generating Matplotlib figure..." )
841867
842- tex_fonts = setup_tex_fonts (fontsize = self .fontsize , usetex = usetex )
868+ resolved_usetex = self ._usetex if usetex is None else usetex
869+ tex_fonts = setup_tex_fonts (fontsize = self .fontsize , usetex = resolved_usetex )
843870
844871 setup_plotstyle (
845872 tex_fonts = tex_fonts ,
@@ -1003,18 +1030,28 @@ def plot_plotext(
10031030 self ._plotext_figure = wrapped
10041031 return wrapped
10051032
1006- def plot_plotly (self , show = True , savefig = None , usetex = False ):
1033+ def plot_plotly (
1034+ self ,
1035+ show = True ,
1036+ savefig = None ,
1037+ usetex : bool | None = None ,
1038+ verbose : bool = False ,
1039+ ):
10071040 """
10081041 Generate and optionally display the subplots using Plotly.
10091042
10101043 Parameters:
10111044 show (bool): Whether to display the plot.
10121045 savefig (str, optional): Filename to save the figure if provided.
1046+ verbose (bool): Whether to print verbose output.
1047+
10131048 """
10141049
1050+ resolved_usetex = self ._usetex if usetex is None else usetex
1051+
10151052 setup_tex_fonts (
10161053 fontsize = self .fontsize ,
1017- usetex = usetex ,
1054+ usetex = resolved_usetex ,
10181055 ) # adjust or redefine for Plotly if needed
10191056
10201057 # Set default width and height if not specified
@@ -1106,6 +1143,10 @@ def label(self):
11061143 def figsize (self ):
11071144 return self ._figsize
11081145
1146+ @property
1147+ def usetex (self ):
1148+ return self ._usetex
1149+
11091150 @property
11101151 def subplot_matrix (self ):
11111152 return self ._subplot_matrix
0 commit comments