2828
2929
3030class TikzPlot (PathPyPlot ):
31- """Base class for plotting d3js objects."""
31+ """Base class for plotting tikz objects."""
3232
3333 def __init__ (self , ** kwargs : Any ) -> None :
3434 """Initialize plot class."""
@@ -52,20 +52,30 @@ def save(self, filename: str, **kwargs: Any) -> None:
5252 shutil .copy (temp_file , filename )
5353 # remove the temporal directory
5454 shutil .rmtree (temp_dir )
55-
55+ elif filename .endswith ("svg" ):
56+ # compile temporary svg
57+ temp_file , temp_dir = self .compile_svg ()
58+ # Copy a file with new name
59+ shutil .copy (temp_file , filename )
60+ # remove the temporal directory
61+ shutil .rmtree (temp_dir )
5662 else :
5763 raise NotImplementedError
5864
5965 def show (self , ** kwargs : Any ) -> None :
6066 """Show the plot on the device."""
6167 # compile temporary pdf
62- temp_file , temp_dir = self .compile_pdf ()
68+ temp_file , temp_dir = self .compile_svg ()
6369
6470 if config ["environment" ]["interactive" ]:
65- from IPython .display import IFrame , display
66-
67- # open the file in the notebook
68- display (IFrame (temp_file , width = 600 , height = 300 ))
71+ from IPython .display import SVG , display
72+
73+ # open the file, read the content and display it
74+ # workaround because it is not possible to embed files in vs code
75+ # https://github.com/microsoft/vscode-jupyter/discussions/13769
76+ with open (temp_file , "r" ) as svg_file :
77+ svg = SVG (svg_file .read ())
78+ display (svg )
6979 else :
7080 # open the file in the webbrowser
7181 webbrowser .open (r"file:///" + temp_file )
@@ -76,33 +86,44 @@ def show(self, **kwargs: Any) -> None:
7686 # remove the temporal directory
7787 shutil .rmtree (temp_dir )
7888
79- def compile_pdf (self ) -> tuple :
80- """Compile pdf from tex."""
81- # basename
82- basename = "default"
83- # get current directory
84- current_dir = os .getcwd ()
85-
86- # template directory
87- tikz_dir = str (
88- os .path .join (
89- os .path .dirname (os .path .dirname (__file__ )),
90- os .path .normpath ("templates" ),
91- "tikz-network.sty" ,
92- )
93- )
94-
95- # get temporal directory
96- temp_dir = tempfile .mkdtemp ()
89+ def compile_svg (self ) -> tuple :
90+ """Compile svg from tex."""
91+ temp_dir , current_dir , basename = self .prepare_compile ()
9792
98- # copy tikz-network to temporal directory
99- shutil .copy (tikz_dir , temp_dir )
93+ # latex compiler
94+ command = [
95+ "latexmk" ,
96+ "--interaction=nonstopmode" ,
97+ basename + ".tex" ,
98+ ]
99+ try :
100+ subprocess .check_output (command , stderr = subprocess .STDOUT )
101+ except subprocess .CalledProcessError as e :
102+ logger .error ("latexmk compiler failed with output:\n %s" , e .output .decode ())
103+ raise AttributeError from e
104+
105+ # dvisvgm command
106+ command = [
107+ "dvisvgm" ,
108+ basename + ".dvi" ,
109+ "-o" ,
110+ basename + ".svg" ,
111+ ]
112+ try :
113+ subprocess .check_output (command , stderr = subprocess .STDOUT )
114+ except subprocess .CalledProcessError as e :
115+ logger .error ("dvisvgm command failed with output:\n %s" , e .output .decode ())
116+ raise AttributeError from e
117+ finally :
118+ # change back to the current directory
119+ os .chdir (current_dir )
100120
101- # change to output dir
102- os .chdir (temp_dir )
121+ # return the name of the folder and temp svg file
122+ return os .path . join (temp_dir , basename + ".svg" ), temp_dir
103123
104- # save the tex file
105- self .save (basename + ".tex" )
124+ def compile_pdf (self ) -> tuple :
125+ """Compile pdf from tex."""
126+ temp_dir , current_dir , basename = self .prepare_compile ()
106127
107128 # latex compiler
108129 command = [
@@ -115,16 +136,32 @@ def compile_pdf(self) -> tuple:
115136
116137 try :
117138 subprocess .check_output (command , stderr = subprocess .STDOUT )
118- except Exception :
119- # If compiler does not exist, try next in the list
120- logger .error ("No latexmk compiler found" )
121- raise AttributeError
139+ except subprocess .CalledProcessError as e :
140+ logger .error ("latexmk compiler failed with output:\n %s" , e .output .decode ())
141+ raise AttributeError from e
122142 finally :
123143 # change back to the current directory
124144 os .chdir (current_dir )
125145
126146 # return the name of the folder and temp pdf file
127- return (os .path .join (temp_dir , basename + ".pdf" ), temp_dir )
147+ return os .path .join (temp_dir , basename + ".pdf" ), temp_dir
148+
149+ def prepare_compile (self ) -> tuple [str , str , str ]:
150+ """Prepare compilation of tex to pdf or svg by saving the tex file."""
151+ # basename
152+ basename = "default"
153+ # get current directory
154+ current_dir = os .getcwd ()
155+
156+ # get temporal directory
157+ temp_dir = tempfile .mkdtemp ()
158+
159+ # change to output dir
160+ os .chdir (temp_dir )
161+
162+ # save the tex file
163+ self .save (basename + ".tex" )
164+ return temp_dir , current_dir , basename
128165
129166 def to_tex (self ) -> str :
130167 """Convert data to tex."""
@@ -144,8 +181,8 @@ def to_tex(self) -> str:
144181 # fill template with data
145182 tex = Template (tex_template ).substitute (
146183 classoptions = self .config .get ("latex_class_options" , "" ),
147- width = self .config .get ("width" , "6cm " ),
148- height = self .config .get ("height" , "6cm " ),
184+ width = self .config .get ("width" , "12cm " ),
185+ height = self .config .get ("height" , "12cm " ),
149186 tikz = data ,
150187 )
151188
0 commit comments