figure2latex.m

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

M
349
字号
                                        num2str(round(log10(temp_tick / temp_label))), ...
                                        '}$}\n']);
                end
            end
        end 
        
        % For each of the children of the axes
        axes_children = get(fig_children(count), 'Children');
        for child_count = 1 : length(axes_children)
            child_tag = get(axes_children(child_count), 'Tag');
            child_type = get(axes_children(child_count), 'Type');
            x_lim = get(fig_children(count), 'XLim');
            y_lim = get(fig_children(count), 'YLim');
            
            % Next, if the child is a line, draw it
            if isempty(child_tag) & strcmp(child_type, 'line')
                % Yes, a line, get some line data
                x_data = get(axes_children(child_count), 'XData');
                y_data = get(axes_children(child_count), 'YData');
                
                if ~strcmp(get(axes_children(child_count), 'LineStyle'), 'none')
                    % So, we do have a line style, plot it                                        
                    for line_count = 1 : (length(x_data) - 1)
                        % Test to see if the line is within the current
                        % axis limits   
                        draw_line = false;
                        if (x_data(line_count) == x_data(line_count + 1))
                            % Vertical line
                            if y_data(line_count) ~= y_data(line_count + 1)
                                % Not some strange single point line
                                if (x_data(line_count) >= x_lim(1)) & (x_data(line_count) <= x_lim(2))
                                    % X position is within limits
                                    if y_data(line_count + 1) > y_data(line_count)
                                        t_start = (y_lim(1) - y_data(line_count)) / (y_data(line_count + 1) - y_data(line_count));
                                        t_stop = (y_lim(2) - y_data(line_count)) / (y_data(line_count + 1) - y_data(line_count));
                                    else
                                        t_start = (y_lim(2) - y_data(line_count)) / (y_data(line_count + 1) - y_data(line_count));
                                        t_stop = (y_lim(1) - y_data(line_count)) / (y_data(line_count + 1) - y_data(line_count));
                                    end

                                    if (t_start > 1) | (t_stop < 0)
                                        % Do nothing
                                    else
                                        draw_line = true;
                                        if t_start < 0
                                            t_start = 0;
                                        end
                                        if t_stop > 1
                                            t_stop = 1;
                                        end
                                        x_line_coors = [x_data(line_count), x_data(line_count)];
                                        y_line_coors = y_data(line_count) + [t_start, t_stop] * (y_data(line_count + 1) - y_data(line_count));
                                    end
                                end                                        
                            end
                        elseif (y_data(line_count) == y_data(line_count + 1))
                            % Horizontal line
                            if x_data(line_count) ~= x_data(line_count + 1)
                                % Not some strange single point line
                                if (y_data(line_count) >= y_lim(1)) & (y_data(line_count) <= y_lim(2))
                                    % Y position is within limits
                                    if x_data(line_count + 1) > x_data(line_count)
                                        t_start = (x_lim(1) - x_data(line_count)) / (x_data(line_count + 1) - x_data(line_count));
                                        t_stop = (x_lim(2) - x_data(line_count)) / (x_data(line_count + 1) - x_data(line_count));
                                    else
                                        t_start = (x_lim(2) - x_data(line_count)) / (x_data(line_count + 1) - x_data(line_count));
                                        t_stop = (x_lim(1) - x_data(line_count)) / (x_data(line_count + 1) - x_data(line_count));
                                    end

                                    if (t_start > 1) | (t_stop < 0)
                                        % Do nothing
                                    else
                                        draw_line = true;
                                        if t_start < 0
                                            t_start = 0;
                                        end
                                        if t_stop > 1
                                            t_stop = 1;
                                        end
                                        y_line_coors = [y_data(line_count), y_data(line_count)];
                                        x_line_coors = x_data(line_count) + [t_start, t_stop] * (x_data(line_count + 1) - x_data(line_count));
                                    end                                    
                                end
                            end
                        else
                            % Line is neither horizontal or vertical
                            if x_data(line_count + 1) > x_data(line_count)
                                t_start_x = (x_lim(1) - x_data(line_count)) / (x_data(line_count + 1) - x_data(line_count));
                                t_stop_x = (x_lim(2) - x_data(line_count)) / (x_data(line_count + 1) - x_data(line_count));
                            else
                                t_start_x = (x_lim(2) - x_data(line_count)) / (x_data(line_count + 1) - x_data(line_count));
                                t_stop_x = (x_lim(1) - x_data(line_count)) / (x_data(line_count + 1) - x_data(line_count));
                            end
                            if y_data(line_count + 1) > y_data(line_count)
                                t_start_y = (y_lim(1) - y_data(line_count)) / (y_data(line_count + 1) - y_data(line_count));
                                t_stop_y = (y_lim(2) - y_data(line_count)) / (y_data(line_count + 1) - y_data(line_count));
                            else
                                t_start_y = (y_lim(2) - y_data(line_count)) / (y_data(line_count + 1) - y_data(line_count));
                                t_stop_y = (y_lim(1) - y_data(line_count)) / (y_data(line_count + 1) - y_data(line_count));
                            end
                            
                            if (t_start_x > 1) | (t_start_y > 1) | (t_stop_x < 0) | (t_stop_y < 0)
                                % Do nothing
                            else
                                draw_line = true;
                                t_start = max([t_start_x, t_start_y, 0]);
                                t_stop = min([t_stop_x, t_stop_y, 1]);
                                
                                x_line_coors = x_data(line_count) + [t_start, t_stop] * (x_data(line_count + 1) - x_data(line_count));
                                y_line_coors = y_data(line_count) + [t_start, t_stop] * (y_data(line_count + 1) - y_data(line_count));
                            end                            
                        end
                        
                        if draw_line == true
                            % First, get the line colour
                            temp_colour = get(axes_children(child_count), 'Color');
                            fprintf(fid, ['\\newrgbcolor{userLineColor}{' num2str(temp_colour(1)) ...       
                                                         ' ' num2str(temp_colour(2)) ...
                                                         ' ' num2str(temp_colour(3)) '}\n']);
                            % Next, get the new coordinates
                            x_line_tex = st.figure_box(1) + (st.figure_box(3) - st.figure_box(1)) * (x_line_coors - x_lim(1)) / (x_lim(2) - x_lim(1));
                            y_line_tex = st.figure_box(2) + (st.figure_box(4) - st.figure_box(2)) * (y_line_coors - y_lim(1)) / (y_lim(2) - y_lim(1));
                            fprintf(fid, ['\\psline[linewidth=', num2str(st.plot_line_thickness), ',linecolor=userLineColor]{-}(' ...
                                num2str(x_line_tex(1), '%3.2f') ',' num2str(y_line_tex(1), '%3.1f') ')(' ...
                                num2str(x_line_tex(2), '%3.2f') ',' num2str(y_line_tex(2), '%3.1f') ')\n']);
                        end
                    end                        
                end
                
                if ~strcmp(get(axes_children(child_count), 'Marker'), 'none')
                    % So, we do have a line marker, plot it
                    % First, set the colour
                    temp_colour = get(axes_children(child_count), 'Color');
                    fprintf(fid, ['\\newrgbcolor{userLineColor}{' num2str(temp_colour(1)) ...       
                                                 ' ' num2str(temp_colour(2)) ...
                                                 ' ' num2str(temp_colour(3)) '}\n']);
                    for line_count = 1 : length(x_data)
                        if (x_data(line_count) >= x_lim(1)) & (x_data(line_count) <= x_lim(2)) & ...
                           (y_data(line_count) >= y_lim(1)) & (y_data(line_count) <= y_lim(2))
                            % Yes, marker is in the window
                            x_tex = st.figure_box(1) + (st.figure_box(3) - st.figure_box(1)) * (x_data(line_count) - x_lim(1)) / (x_lim(2) - x_lim(1));
                            y_tex = st.figure_box(2) + (st.figure_box(4) - st.figure_box(2)) * (y_data(line_count) - y_lim(1)) / (y_lim(2) - y_lim(1));
                            temp = get(axes_children(child_count), 'Marker');
                            temp = matlab_marker_to_latex(temp);
                            fprintf(fid, ['\\rput(', num2str(x_tex, '%3.2f'), ',', ...
                                        num2str(y_tex, '%3.2f'), '){\\textcolor{userLineColor}{', latex2fprintf(temp), '}}\n']);
                        end
                    end
                end
            end
            % If the child is text, place it
            if isempty(child_tag) & strcmp(child_type, 'text')
                % Set the colour
                temp_colour = get(axes_children(child_count), 'Color');
                    fprintf(fid, ['\\newrgbcolor{userLineColor}{' num2str(temp_colour(1)) ...       
                                                 ' ' num2str(temp_colour(2)) ...
                                                 ' ' num2str(temp_colour(3)) '}\n']);
                temp = get(axes_children(child_count), 'Position');
                x_tex = st.figure_box(1) + (st.figure_box(3) - st.figure_box(1)) * (temp(1) - x_lim(1)) / (x_lim(2) - x_lim(1));
                y_tex = st.figure_box(2) + (st.figure_box(4) - st.figure_box(2)) * (temp(2) - y_lim(1)) / (y_lim(2) - y_lim(1));
                fprintf(fid, ['\\rput[l](', num2str(x_tex, '%3.2f'), ',', ...
                                        num2str(y_tex, '%3.2f'), '){\\textcolor{userLineColor}{', ...
                                        latex2fprintf(get(axes_children(child_count), 'String')), '}}\n']);
            end
        end
        
    end
end

% Write the ending material
fprintf(fid, '\\end{pspicture}\n');

% Close the file
fclose(fid);

⌨️ 快捷键说明

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