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
31 % optional fields of params:
32 % width: width of output picture in cm. (default=8.0)
33 % relpath: a path to the image files relative to the main tex file
34 % from which the main tikz file is included. This might be
35 % necessary, because latex searches input files relative to
36 % the latex executation path only. This path string may also
37 % include tex commands for later specification of
38 % the image path in the tex file. (default='./')
39 % height: height of output picture in pixels.
40 % If this parameter does not exist or is set to zero, the
41 % height is calculated from the width and the pictures ratio.
42 % save_colorbar: boolean specifying wether separate files for the colorbar
44 % print_axes: boolean specifying wether the tikz file shall include
45 % drawing commands for axes around the picture.
47 % print_axes_label: boolean specifying wether the tikz file shall include
48 % drawing commands for labels at the axes. This field is
49 % ignored if 'print_axes' is set to false. (default=true)
50 % ticks: integer specifying how many ticks should be added to the
51 % axes drawn around the figure. This field is ignored if
52 % 'print_axes' or 'print_axes_label' is set to false.
58 if ~isfield(params,
'export_fig_params')
59 params.export_fig_params = {};
61 if isfield(params,
'filepath')
66 if isfield(params, 'relpath')
67 relpath = params.relpath;
72 if isfield(params, 'figure_handle')
73 fhandle = params.figure_handle;
78 if isfield(params, 'save_colorbar') && params.save_colorbar
84 if ~isfield(params, 'print_axes')
85 params.print_axes = 1;
88 if ~isfield(params, 'print_axes_label')
89 params.print_axes_label = 1;
92 if ~isfield(params, 'width')
96 if ~isfield(params, 'scaleaxis')
97 params.scaleaxis = 'scale only axis';
100 if ~isfield(params, 'hide_axes')
101 params.hide_axes = true;
104 if ~isfield(params, 'pic_scale')
107 picscale = params.pic_scale;
111 if isfield(params, 'ticks')
112 ticks = params.ticks;
117 % change figure properties for transparencies
118 set(fhandle,'Color','none');
120 children = get(fhandle, 'Children');
121 for i=length(children):-1:1
122 if strcmp(get(children(i), 'Type'), 'axes') == 1
123 set(children(i),'Color','none');
127 colorbar('peer', ahandle);
129 set(fhandle,'InvertHardCopy','off');
135 print(fhandle, '-dpng', '-noui', '-zbuffer', fullfile(fp, [fn, '_alt']));
137 pos=get(fhandle, 'Position');
138 set(fhandle, 'Position', [pos(1:2), picscale*pos(3:4)]);
140 % export figure to png file
141 im = export_fig(fullfile(fp, fn),'-png', params.export_fig_params{:},
'-a3', fhandle);
145 % save colorbar as well
148 colorbarfn = fullfile(fp, [fn,
'colorbar']);
149 if ~exist([colorbarfn,
'.png'],
'file')
150 colorh = colorbar('peer',ahandle);
151 yticks = get(colorh, 'YTick');
152 set(colorh, 'YTick',[]);
153 set(colorh, 'YTickLabel',[]);
155 cbim = export_fig(colorbarfn, '-png', '-a3', colorh);
156 % cbim = export_fig(colorh);
162 col_ranges=cell(2,1);
163 col_ranges{1} =
get(gca,
'XLim');
164 col_ranges{2} =
get(gca,
'YLim');
165 clim =
get(gca,
'CLim');
171 xyratio = imsize(1)/imsize(2);
174 fid = fopen(fullfile(fp, [fn,
'.tikz']),
'w+');
175 ftexid = fopen(fullfile(fp, [fn,
'_test.tex']),
'w+');
177 width = params.width;
178 if ~isfield(params,
'height') || params.height == 0
179 height = width * xyratio;
181 height = params.height;
189 sprintf('%.2fcm/%.1f', (width) * (i-1)/(ticks-1),...
190 (xr(2)-xr(1))*(i-1)/(ticks-1) + xr(1))];
192 sprintf('%.2fcm/%.1f', (height) * (i-1)/(ticks-1),...
193 (yr(2)-yr(1))*(i-1)/(ticks-1) + yr(1))];
195 xtstr = [xtstr, ', '];
196 ytstr = [ytstr, ', '];
201 colorbarinput = sprintf(
' \\\\begin{scope}[xshift=-2cm]\\\\input{%s}\\n\\\\end{scope}',[colorbarfn,
'.tikz']);
206 fprintf(ftexid,[
'\\documentclass{article}\n',...
207 '\\usepackage{tikz,pgfplots}\n',...
208 '\\usetikzlibrary{arrows,snakes,shapes,decorations.pathmorphing,backgrounds,fit}\n',...
209 '\\begin{document}\n',...
210 ' \\begin{tikzpicture}\n',...
213 ' \\end{tikzpicture}\n',...
214 '\\end{document}\n'],[fn,
'.tikz']);
218 if params.print_axes && params.print_axes_label
223 relfn = fullfile(relpath, fn);
224 fileinput = sprintf([
'\\tikzset{background rectangle/.style={fill=white!0}}\n', ...
225 '\\everymath{\\scriptstyle};\n', ...
226 '\\begin{axis}[axis on top, enlargelimits=false,\n',...
227 'xtick=%s,ytick=%s,',params.scaleaxis,
', width=%s cm, height=%s cm]\n',...
228 ' \\addplot graphics[xmin=%s, xmax=%s, ymin=%s, ymax=%s] {%s};\n',...
230 ticks, ticks, num2str(width), num2str(height), ...
231 num2str(xr(1)), num2str(xr(2)), num2str(yr(1)), num2str(yr(2)), relfn);
233 fwrite(fid, fileinput);
238 colorfid = fopen([colorbarfn,
'.tikz'],
'w+');
239 relcfn = fullfile(relpath, [fn,
'colorbar']);
240 fileinput = sprintf([
'\\tikzset{background rectangle/.style={fill=white!0}}\n', ...
241 '\\everymath{\\scriptstyle};\n', ...
242 '\\begin{axis}[xtick=\\empty,ytick=%s,enlargelimits=false,\n', ...
243 ' height=%s cm,width=50pt]\n', ...
244 ' \\addplot graphics[includegraphics={trim=1 1 1 1},xmin=0, xmax=1,\n', ...
245 ' ymin=%s,ymax=%s]\n', ...
247 '\\end{axis}\n'], ...
248 ticks, num2str(height), num2str(clim(1)), num2str(clim(2)), relcfn);
250 fwrite(colorfid, fileinput);