3 % postprocesses a figure and write out an image and a text file that can be
4 % included in TeX documents.
6 % This method creates three files from a MATLAB figure specified by
7 %
'params.figure_handle':
8 % - A
'png' coded image file containing a snapshot of the figure box
9 % - A text file ending
'.tikz' that can be included in a LaTeX document
10 % adding axes and meta information around the
'png' file.
11 % - A text file ending
'_test.tex' that can be compiled with pdflatex and
12 % outputs a pdf file with the figure output.
14 % Why
this is better than including the
'png' file directly:
15 % When scaling images
for MATLAB figures, often the text
for the ticks,
16 % legends and axes description is scaled to an unreadable size. Furthermore,
17 % text sizes can differ between a LaTeX document and the MATLAB Monospace font
18 % type does not look nice in the PDF output. The solution here, circumvents
19 % all these problems, because all text used inside the figures will be
20 % generated during pdflatex compilation phase.
23 % params: Options controlling the output
25 % required fields of params:
26 % figure_handle: handle to the figure that shall be postprocessed
27 % filename: base of filename
for the three generated files
28 %
'filename.png',
'filename.tikz' and
'filename_test.tex'
29 % filepath: path where the output files shall be stored
30 % width: width of output picture in pixels.
32 % optional fields of params:
33 % height: height of output picture in pixels.
34 % If
this parameter does not exist or is set to zero, the
35 % height is calculated from the width and the pictures ratio.
36 % save_colorbar:
boolean specifying wether separate files
for the colorbar
38 % print_axes:
boolean specifying wether the tikz file shall include
39 % drawing commands
for axes around the picture.
41 % print_axes_label:
boolean specifying wether the tikz file shall include
42 % drawing commands
for labels at the axes. This field is
43 % ignored
if 'print_axes' is set to
false. (Default=
true)
44 % ticks: integer specifying how many ticks should be added to the
45 % axes drawn around the figure. This field is ignored
if
46 %
'print_axes' or
'print_axes_label' is set to
false.
51 if isfield(params,
'filepath')
57 if isfield(params,
'figure_handle')
58 fhandle = params.figure_handle;
63 if isfield(params, 'save_colorbar') && params.save_colorbar
69 if ~isfield(params, 'print_axes')
70 params.print_axes = 1;
73 if ~isfield(params, 'print_axes_label')
74 params.print_axes_label = 1;
77 if ~isfield(params, 'width')
81 if ~isfield(params, 'scaleaxis')
82 params.scaleaxis = 'scale only axis';
87 if isfield(params, 'ticks')
93 % change figure properties for transparencies
94 set(fhandle,'Color','none');
96 children = get(fhandle, 'Children');
97 for i=length(children):-1:1
98 if strcmp(get(children(i), 'Type'), 'axes') == 1
99 set(children(i),'Color','none');
103 %colorbar('peer', ahandle);
104 %colorbar(ahandle, 'hide');
105 set(fhandle,'InvertHardCopy','off');
108 % export figure to png file
109 im = export_fig(fullfile(fp, fn),'-png','-a3', fhandle);
112 % save colorbar as well
114 colorbarfn = fullfile(fp, [fn, 'colorbar']);
115 if ~exist([colorbarfn, '.png'], 'file')
116 colorh = colorbar('peer',ahandle);
117 yticks = get(colorh, 'YTick');
118 set(colorh, 'YTick',[]);
119 set(colorh, 'YTickLabel',[]);
121 cbim = export_fig(colorbarfn, '-png', '-a3', colorh);
122 % cbim = export_fig(colorh);
128 col_ranges=cell(2,1);
129 col_ranges{1} =
get(gca,
'XLim');
130 col_ranges{2} =
get(gca,
'YLim');
131 clim =
get(gca,
'CLim');
137 xyratio = imsize(1)/imsize(2);
140 fid = fopen(fullfile(fp, [fn,
'.tikz']),
'w+');
141 ftexid = fopen(fullfile(fp, [fn,
'_test.tex']),
'w+');
143 width = params.width;
144 if ~isfield(params,
'height') || params.height == 0
145 height = width * xyratio;
147 height = params.height;
155 sprintf(
'%.2fcm/%.1f', (width) * (i-1)/(ticks-1),...
156 (xr(2)-xr(1))*(i-1)/(ticks-1) + xr(1))];
158 sprintf(
'%.2fcm/%.1f', (height) * (i-1)/(ticks-1),...
159 (yr(2)-yr(1))*(i-1)/(ticks-1) + yr(1))];
161 xtstr = [xtstr,
', '];
162 ytstr = [ytstr,
', '];
167 colorbarinput = sprintf(
' \\\\begin{scope}[xshift=-2cm]\\\\input{%s}\\n\\\\end{scope}',[colorbarfn,
'.tikz']);
172 fprintf(ftexid,[
'\\documentclass{article}\n',...
173 '\\usepackage{tikz,pgfplots}\n',...
174 '\\usetikzlibrary{arrows,snakes,shapes,decorations.pathmorphing,backgrounds,fit}\n',...
175 '\\begin{document}\n',...
176 ' \\begin{tikzpicture}\n',...
179 ' \\end{tikzpicture}\n',...
180 '\\end{document}\n'],[fn,
'.tikz']);
184 if params.print_axes && params.print_axes_label
189 fileinput = sprintf([
'\\tikzset{background rectangle/.style={fill=white!0}}\n', ...
190 '\\everymath{\\scriptstyle};\n', ...
191 '\\begin{axis}[axis on top, enlargelimits=false,\n',...
192 'xtick=%s,ytick=%s,',params.scaleaxis,
', width=%s cm, height=%s cm]\n',...
193 ' \\addplot graphics[xmin=%s, xmax=%s, ymin=%s, ymax=%s] {%s};\n',...
195 ticks, ticks, num2str(width), num2str(height), ...
196 num2str(xr(1)), num2str(xr(2)), num2str(yr(1)), num2str(yr(2)), fn);
198 fwrite(fid, fileinput);
203 colorfid = fopen(fullfile(fp, [colorbarfn,
'.tikz']),
'w+');
204 fileinput = sprintf([
'\\tikzset{background rectangle/.style={fill=white!0}}\n', ...
205 '\\everymath{\\scriptstyle};\n', ...
206 '\\begin{axis}[xtick=\\empty,ytick=%s,enlargelimits=false,\n', ...
207 ' height=%s cm,width=50pt]\n', ...
208 ' \\addplot graphics[includegraphics={trim=1 1 1 1},xmin=0, xmax=1,\n', ...
209 ' ymin=%s,ymax=%s]\n', ...
211 '\\end{axis}\n'], ...
212 ticks, num2str(height), num2str(clim(1)), num2str(clim(2)), colorbarfn);
214 fwrite(colorfid, fileinput);
function clim = plot_as_tikzfile(model, params)
postprocesses a figure and write out an image and a text file that can be included in TeX documents...