Skip to content

Commit f90f092

Browse files
committed
add function get_tikz_code() that only returns the code in a string
1 parent 321341d commit f90f092

File tree

2 files changed

+44
-35
lines changed

2 files changed

+44
-35
lines changed

matplotlib2tikz/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
__status__
1515
)
1616

17-
from matplotlib2tikz.save import save
17+
from matplotlib2tikz.save import get_tikz_code, save
1818

1919
import pipdated
2020
if pipdated.needs_checking('matplotlib2tikz'):

matplotlib2tikz/save.py

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,27 @@
99
from . import patch
1010
from . import text as mytext
1111

12+
from .__about__ import __version__
13+
14+
import codecs
1215
import os
1316
import matplotlib as mpl
14-
from .__about__ import __version__
1517

1618

17-
def save(filepath,
18-
figure='gcf',
19-
encoding=None,
20-
figurewidth=None,
21-
figureheight=None,
22-
textsize=10.0,
23-
tex_relative_path_to_data=None,
24-
strict=False,
25-
wrap=True,
26-
extra=None,
27-
dpi=None,
28-
show_info=True
29-
):
19+
def get_tikz_code(
20+
filepath,
21+
figure='gcf',
22+
encoding=None,
23+
figurewidth=None,
24+
figureheight=None,
25+
textsize=10.0,
26+
tex_relative_path_to_data=None,
27+
strict=False,
28+
wrap=True,
29+
extra=None,
30+
dpi=None,
31+
show_info=True
32+
):
3033
'''Main function. Here, the recursion into the image starts and the
3134
contents are picked up. The actual file gets written in this routine.
3235
@@ -132,13 +135,6 @@ def save(filepath,
132135
else:
133136
data['dpi'] = dpi
134137

135-
# open file
136-
import codecs
137-
file_handle = codecs.open(filepath, 'w', encoding)
138-
139-
if show_info:
140-
print('file encoding: {0}'.format(file_handle.encoding))
141-
142138
# gather the file content
143139
data, content = _recurse(data, figure)
144140

@@ -150,32 +146,45 @@ def save(filepath,
150146
disclaimer = 'This file was created by matplotlib2tikz v%s.' % __version__
151147

152148
# write disclaimer to the file header
153-
file_handle.write(_tex_comment(disclaimer))
149+
code = ''''''
150+
code += _tex_comment(disclaimer)
154151

155152
# write the contents
156153
if wrap:
157-
file_handle.write('\\begin{tikzpicture}\n\n')
154+
code += '\\begin{tikzpicture}\n\n'
158155

159156
coldefs = _get_color_definitions(data)
160157
if coldefs:
161-
file_handle.write('\n'.join(coldefs))
162-
file_handle.write('\n\n')
158+
code += '\n'.join(coldefs)
159+
code += '\n\n'
163160

164-
try:
165-
file_handle.write(''.join(content))
166-
except UnicodeEncodeError:
167-
# We're probably using Python 2, so use proper unicode treatment
168-
file_handle.write(unicode(''.join(content)).encode('utf-8'))
161+
code += ''.join(content)
169162

170163
if wrap:
171-
file_handle.write('\\end{tikzpicture}')
172-
173-
# close file
174-
file_handle.close()
164+
code += '\\end{tikzpicture}'
175165

176166
# print message about necessary pgfplot libs to command line
177167
if show_info:
178168
_print_pgfplot_libs_message(data)
169+
return code
170+
171+
172+
def save(*args, **kwargs):
173+
'''Same as `get_tikz_code()`, but actually saves the code to a file.
174+
'''
175+
code = get_tikz_code(*args, **kwargs)
176+
177+
file_handle = codecs.open(
178+
args[0],
179+
'w',
180+
kwargs['encoding'] if 'encoding' in kwargs else None
181+
)
182+
try:
183+
file_handle.write(code)
184+
except UnicodeEncodeError:
185+
# We're probably using Python 2, so use proper unicode treatment
186+
file_handle.write(unicode(code).encode('utf-8'))
187+
file_handle.close()
179188
return
180189

181190

0 commit comments

Comments
 (0)