rbmatlab  1.13.10
 All Classes Namespaces Files Functions Variables Groups Pages
plot_as_tikzfile.m
Go to the documentation of this file.
1 function clim = plot_as_tikzfile(params)
2 % function plot_as_tikzfile(params)
3 % postprocesses a figure and write out an image and a text file that can be
4 % included in TeX documents.
5 %
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.
13 %
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.
21 %
22 % parameters:
23 % params: Options controlling the output
24 %
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 %
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
43 % is generated.
44 % print_axes: boolean specifying wether the tikz file shall include
45 % drawing commands for axes around the picture.
46 % (default=true)
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.
53 % (default=5)
54 %
55 fn = params.filename;
56 % dirname
57 
58 if ~isfield(params, 'export_fig_params')
59  params.export_fig_params = {};
60 end
61 if isfield(params, 'filepath')
62  fp = params.filepath;
63 else
64  fp = './';
65 end
66 if isfield(params, 'relpath')
67  relpath = params.relpath;
68 else
69  relpath = './';
70 end
71 % figure handle
72 if isfield(params, 'figure_handle')
73  fhandle = params.figure_handle;
74 else
75  fhandle = gcf;
76 end
77 % colorbar
78 if isfield(params, 'save_colorbar') && params.save_colorbar
79  save_colorbar = 1;
80 else
81  save_colorbar = 0;
82 end
83 
84 if ~isfield(params, 'print_axes')
85  params.print_axes = 1;
86 end
87 
88 if ~isfield(params, 'print_axes_label')
89  params.print_axes_label = 1;
90 end
91 
92 if ~isfield(params, 'width')
93  params.width = 8.0;
94 end
95 
96 if ~isfield(params, 'scaleaxis')
97  params.scaleaxis = 'scale only axis';
98 end
99 
100 if ~isfield(params, 'hide_axes')
101  params.hide_axes = true;
102 end
103 
104 if ~isfield(params, 'pic_scale')
105  picscale = 3;
106 else
107  picscale = params.pic_scale;
108 end
109 
110 
111 if isfield(params, 'ticks')
112  ticks = params.ticks;
113 else
114  ticks = 5;
115 end
116 
117 % change figure properties for transparencies
118 set(fhandle,'Color','none');
119 % get axes:
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');
124  ahandle=children(i);
125  end
126 end
127 colorbar('peer', ahandle);
128 colorbar('hide');
129 set(fhandle,'InvertHardCopy','off');
130 
131 if params.hide_axes
132  showaxes('hide');
133 end
134 
135 print(fhandle, '-dpng', '-noui', '-zbuffer', fullfile(fp, [fn, '_alt']));
136 
137 pos=get(fhandle, 'Position');
138 set(fhandle, 'Position', [pos(1:2), picscale*pos(3:4)]);
139 
140 % export figure to png file
141 im = export_fig(fullfile(fp, fn),'-png', params.export_fig_params{:}, '-a3', fhandle);
142 
143 imsize = size(im);
144 
145 % save colorbar as well
146 if save_colorbar
147 
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',[]);
154 
155  cbim = export_fig(colorbarfn, '-png', '-a3', colorh);
156 % cbim = export_fig(colorh);
157  size(cbim)
158  end
159 end
160 
161 % color ranges
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');
166 
167 xr = col_ranges{1};
168 yr = col_ranges{2};
169 
170 % x-y axes ratio
171 xyratio = imsize(1)/imsize(2);
172 
173 % tikz file
174 fid = fopen(fullfile(fp, [fn, '.tikz']), 'w+');
175 ftexid = fopen(fullfile(fp, [fn, '_test.tex']), 'w+');
176 
177 width = params.width;
178 if ~isfield(params, 'height') || params.height == 0
179  height = width * xyratio;
180 else
181  height = params.height;
182 end
183 
184 % xtick string
185 xtstr = '';
186 ytstr = '';
187 for i = 1:ticks
188  xtstr = [xtstr, ...
189  sprintf('%.2fcm/%.1f', (width) * (i-1)/(ticks-1),...
190  (xr(2)-xr(1))*(i-1)/(ticks-1) + xr(1))];
191  ytstr = [ytstr, ...
192  sprintf('%.2fcm/%.1f', (height) * (i-1)/(ticks-1),...
193  (yr(2)-yr(1))*(i-1)/(ticks-1) + yr(1))];
194  if i ~= ticks
195  xtstr = [xtstr, ', '];
196  ytstr = [ytstr, ', '];
197  end
198 end
199 
200 if save_colorbar
201  colorbarinput = sprintf(' \\\\begin{scope}[xshift=-2cm]\\\\input{%s}\\n\\\\end{scope}',[colorbarfn, '.tikz']);
202 else
203  colorbarinput = '';
204 end
205 
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',...
211  ' \\input{%s}\n',...
212  colorbarinput,...
213  ' \\end{tikzpicture}\n',...
214  '\\end{document}\n'],[fn, '.tikz']);
215 
216 fclose(ftexid);
217 
218 if params.print_axes && params.print_axes_label
219  ticks = 'data';
220 else
221  ticks = '\empty';
222 end
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',...
229 '\\end{axis}\n'],...
230  ticks, ticks, num2str(width), num2str(height), ...
231  num2str(xr(1)), num2str(xr(2)), num2str(yr(1)), num2str(yr(2)), relfn);
232 
233 fwrite(fid, fileinput);
234 
235 fclose(fid);
236 
237 if save_colorbar
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', ...
246  ' {%s};\n', ...
247  '\\end{axis}\n'], ...
248  ticks, num2str(height), num2str(clim(1)), num2str(clim(2)), relcfn);
249 
250  fwrite(colorfid, fileinput);
251 
252  fclose(fid);
253 end
254 
255 end