figure2latex.m

来自「figures to latex code generate latex co」· M 代码 · 共 349 行 · 第 1/2 页

M
349
字号
function figure2latex(fig_handle, st)
% David Krause
% Queen's University
% June 30, 2006
% From a figure, create the latex file
%
% fig_handle - the figure handle, typically 1 for figure 1, 2 for figure 2
% The figure can have one or two axes (for left and right axes figures)
% st - a structure with the output settings
% st.filename - the output filename
% st.comments - a string that has useful comments about the figure
% st.figure_box - 4 element vector specifying [x, y, x_end, y_end] for plot
% box
% st.x_tick_weight - the weight of the x ticks, 0.25
% st.x_tick_length - the length of the x ticks, 2
% st.x_label_y_offset - for the xlabel, the distance from the box, 8
% st.x_ticklabel_y_offset - for the x tick labels, the distance from the
% box, 3
% st.y_tick_weight = 0.25;
% st.y_tick_length = 2;
% st.y_label_x_offset = 18;
% st.y_ticklabel_x_offset = 2;
% st.y_ticklabel_pow10 - the offset for the power of 10 from the top corner, 6;
% 
% st.plot_line_thickness = 0.35;
% Create the file
[fid, message] = fopen(st.filename, 'w');

% Write the comments
fprintf(fid, '%%Written by figure2latex\n');
fprintf(fid, '%%Author: David Krause\n');
fprintf(fid, '%%Email: david.krause@ece.queensu.ca\n');
fprintf(fid, ['%%Clock output: ', num2str(clock), '\n']);
fprintf(fid, ['%%Comments: ', st.comments, '\n']);

% Write the starting material
fprintf(fid, '\\psset{xunit=1mm,yunit=1mm,runit=1mm}\n');
fprintf(fid, ['\\begin{pspicture}(0,0)(', ...
              num2str(st.figure_box(3) + st.figure_box(1)), ...
              ',', ...
              num2str(st.figure_box(4) + st.figure_box(2)), ')\n']);
          
% Create the main figure box
fprintf(fid, ['%% Main figure box\n']);
fprintf(fid, ['\\psframe[linewidth=0.35,linecolor=black](', ...
              num2str(st.figure_box(1)), ',', ...
              num2str(st.figure_box(2)), ')(', ...
              num2str(st.figure_box(3)), ',', ...
              num2str(st.figure_box(4)), ')\n']);

% Get the figure children
fig_children = get(fig_handle, 'children');

% For each child, if it is an axes, generate the labels, ticks, and then
% plot the lines
for count = 1 : length(fig_children)
    axes_tag = get(fig_children(count), 'Tag');
    axes_type = get(fig_children(count), 'Type');
    if isempty(axes_tag) & strcmp(axes_type, 'axes')
        % Yes, an axes, now get on with it
        fprintf(fid, ['%% Axes handle = ', num2str(fig_children(count)), '\n']);
        
        % X-axis label, ticks, and tick labels
        % First, determine the positions for the x-labels
        if strcmp(get(fig_children(count), 'XAxisLocation'), 'bottom')
            x_label_ypos = st.figure_box(2) - st.x_label_y_offset;
            x_ticklabel_ypos = st.figure_box(2) - st.x_ticklabel_y_offset;
            x_tick_ystart = st.figure_box(2);
            x_tick_yend = st.figure_box(2) + st.x_tick_length;
        else
            x_label_ypos = st.figure_box(4) + st.x_label_y_offset;
            x_ticklabel_ypos = st.figure_box(4) + st.x_ticklabel_y_offset;
            x_tick_ystart = st.figure_box(4);
            x_tick_yend = st.figure_box(4) - st.x_tick_length;
        end
        x_label_xpos = (st.figure_box(1) + st.figure_box(3)) / 2;        
        % Place the X axis label
        x_label_hnd = get(fig_children(count), 'XLabel');
        
        if ~isempty(get(x_label_hnd, 'String'));
            fprintf(fid, ['\\rput(', num2str(x_label_xpos), ',', ...
                                     num2str(x_label_ypos), '){', ...
                                     latex2fprintf(get(x_label_hnd, 'String')), '}\n']);
        end        
        % Next, place the x-axis tick labels and ticks
        x_tick_label = get(fig_children(count), 'XTickLabel');        
        for xtick_Count = 1 : length(x_tick_label(:, 1))
            x_temp = st.figure_box(1) + (xtick_Count - 1) * ...
                                       (st.figure_box(3) - st.figure_box(1)) / ...
                                       (length(x_tick_label(:, 1)) - 1);
            fprintf(fid, ['\\rput(', num2str(x_temp), ...
                          ',', num2str(x_ticklabel_ypos), '){', ...
                          num2str(str2num(x_tick_label(xtick_Count, :))), '}\n']);
            if (xtick_Count > 1) & (xtick_Count < length(x_tick_label(:, 1)))
                fprintf(fid, ['\\psline[linewidth=', num2str(st.x_tick_weight), ', linecolor=black]{-}(', ...
                              num2str(x_temp), ',', num2str(x_tick_ystart), ...
                              ')(', num2str(x_temp), ',', num2str(x_tick_yend), ')\n']);
            end
            % If it is the last tick, see if a power of 10 label is needed
            if xtick_Count == length(x_tick_label(:, 1))
                temp_label = str2num(x_tick_label(xtick_Count, :));
                temp_tick = get(fig_children(count), 'XTick');
                
                temp_tick = temp_tick(end);
                
                if temp_label == 0
                    temp_label = str2num(x_tick_label(1, :));
                    temp_tick = get(fig_children(count), 'XTick');
                    temp_tick = temp_tick(1);
                end
                

                if round(log10(temp_tick / temp_label)) ~= 0
                    fprintf(fid, ['\\rput[r](', num2str(x_temp), ',', ...
                                        num2str(x_label_ypos), '){$\\times 10^{', ...
                                        num2str(round(log10(temp_tick / temp_label))), ...
                                        '}$}\n']);
                end
            end
        end
        
        % Y-axis label, ticks, and tick labels
        if strcmp(get(fig_children(count), 'YAxisLocation'), 'left')
            y_label_xpos = st.figure_box(1) - st.y_label_x_offset;
            y_ticklabel_xpos = st.figure_box(1) - st.y_ticklabel_x_offset;
            y_ticklabel_just = 'r';
            y_tick_xstart = st.figure_box(1);
            y_tick_xend = st.figure_box(1) + st.y_tick_length;
            y_tick_pow10 = st.figure_box(1) - st.y_ticklabel_pow10;
        else
            y_label_xpos = st.figure_box(3) + st.y_label_x_offset;
            y_ticklabel_xpos = st.figure_box(3) + st.y_ticklabel_x_offset;
            y_ticklabel_just = 'l';
            y_tick_xstart = st.figure_box(3);
            y_tick_xend = st.figure_box(3) - st.y_tick_length;
            y_tick_pow10 = st.figure_box(3) + st.y_ticklabel_pow10;
        end
        y_label_ypos = (st.figure_box(2) + st.figure_box(4)) / 2;        
        % Place the Y axis label
        y_label_hnd = get(fig_children(count), 'YLabel');
        if ~isempty(get(y_label_hnd, 'String'));
            fprintf(fid, ['\\rput(', num2str(y_label_xpos), ',', ...
                                     num2str(y_label_ypos), '){\\rotatebox{90}{', ...
                                     latex2fprintf(get(y_label_hnd, 'String')), '}}\n']);
        end        
        % Next, place the y-axis tick labels and ticks
        y_tick_label = get(fig_children(count), 'YTickLabel');        
        for ytick_Count = 1 : length(y_tick_label(:, 1))
            y_temp = st.figure_box(2) + (ytick_Count - 1) * ...
                                       (st.figure_box(4) - st.figure_box(2)) / ...
                                       (length(y_tick_label(:, 1)) - 1);
            fprintf(fid, ['\\rput[', y_ticklabel_just, '](', ...
                          num2str(y_ticklabel_xpos), ...
                          ',', num2str(y_temp), '){', ...
                          num2str(str2num(y_tick_label(ytick_Count, :))), '}\n']);
            if (ytick_Count > 1) & (ytick_Count < length(y_tick_label(:, 1)))
                fprintf(fid, ['\\psline[linewidth=', num2str(st.y_tick_weight), ', linecolor=black]{-}(', ...
                              num2str(y_tick_xstart), ',', num2str(y_temp), ...
                              ')(', num2str(y_tick_xend), ',', num2str(y_temp), ')\n']);
            end
            % If it is the last tick, see if a power of 10 label is needed
            if ytick_Count == length(y_tick_label(:, 1))
                temp_label = str2num(y_tick_label(ytick_Count, :));
                temp_tick = get(fig_children(count), 'YTick');
                temp_tick = temp_tick(end);
                
                if temp_label == 0
                    temp_label = str2num(y_tick_label(1, :));
                    temp_tick = get(fig_children(count), 'YTick');
                    temp_tick = temp_tick(1);
                end
                
                if round(log10(temp_tick / temp_label)) ~= 0
                    fprintf(fid, ['\\rput[', y_ticklabel_just, '](', num2str(y_tick_pow10), ',', ...
                                        num2str(y_temp - st.y_ticklabel_pow10), '){$\\times 10^{', ...

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?