5.0

5.0 | 1 rating Rate this file 46 downloads (last 30 days) File Size: 23.8 KB File ID: #25426

Legend to latex converter

by Emile Demarteau

 

28 Sep 2009 (Updated 26 Nov 2009)

Code covered by the BSD License  

This function converts a Matlab legend to a text annotation for latex interpretation.

Download Now | Watch this File

File Information
Description

LEGEND2LATEX(FIGURE_HANDLE) Converts the present legends in the figure to legends that can be postprocessed using LaPrint.

Note that all earlier legend2latex objects are removed.

Can convert several line types and markers, but all may not look equally well. Ideally used in combination with LaPrint and a latex interpreter that uses the AMS math package (for the markers).

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
LaPrint

MATLAB release MATLAB 7.5 (R2007b)
Zip File Content  
Other Files Example/figure1.eps,
Example/figure1.tex,
Example/figure2.pdf,
Example/sample_latex2pdf.m,
legend2latex.m,
license.txt,
pdfcrop.pl,
plot2pdf.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
19 Oct 2009 Diane Bastien

Could you please give an example of how to use your program? What type FIGURE_HANDLE has to be?
Thanks

28 Oct 2009 Emile Demarteau

Hi Diane,

See below for an example of how this file can be used. I noted that the figure_handle is different from the figure_handle as expected by LaPrint. I use a direct handle, where LaPrint uses the figure number.

Good luck,
Emile

%%%%% EXAMPLE %%%%%%
set(0,'defaulttextinterpreter','none');
x = [0:0.1:(2*pi)];
fig1 = figure(1);
plot(x,sin(x),x,cos(x));
legend('sin($\theta$)','cos($\theta$)');
xlabel('$0 \leq \theta \leq 2\pi$')
legend2latex(fig1);
laprint(1,'figure1');

16 Mar 2010 Erik

Wow, that totally fixed my problem with laprint! Thanks!

(how does it work? Does it create a latex string that is being incorporated in the figure or something?)

22 Mar 2010 Dylan

Works like a charm. For those who want to use this with plotepstex (and plot2eps):

In the plotepstex.m file:

Comment out the legend-handling code at lines 104-125 and lines 154-173.

Then add the following lines at line 147:

if ~isempty(legend)
    legend2latex(h)
end

06 May 2010 Marco

Unfortunately, the function does not work for me on histograms, I assume to all similar functions. Is it possible to extend this? Does this only have to do with the fact that it can not find appropriate line styles/markers in this case?

23 Jun 2010 Pascal SZACHERSKI

Hello Emile,

is the any possibility of locating the legend elsewhere? Just like in the legend command " ..., 'Location', 'EastOutside' ..." etc.? Would be a great help.
Thanks for sharing your script by the way!

07 Jul 2010 Emile Demarteau

Hi Pascal,

I had a similar problem. Matlabs legend command does not really have correct positioning properties if the legend is outside the figure. I added some lines below the legend2latex function to adjust the position properties manually

legend2latex(fig1);
legendstr_h = findobj(gca,'Tag', 'legend2latex_text');
xlim_ax1 = get(gca,'XLim'); ylim_ax1 = get(gca,'YLim');
set(legendstr_h, 'Position', [min(xlim_ax1)+1.7*range(xlim_ax1), 1.0125*max(ylim_ax1) 0]);

hope this helps.
Cheers, Emile

20 Jul 2010 David

This solution works perfectly for me combined with laprint, but whenever I include legend2latex i lose the marker information stored within the legend.

For reference this is what I am using,

util1 = figure;
plot(sense_times, ut, '-*b', 'LineWidth', 1);
hold on;
plot(sense_times, ut_g, '-or', 'LineWidth', 1);
legend1 = legend('Local Util.', 'Global Util.', 'Location', 'SouthEast');
%set(legend1, 'Box', 'off');
%set(legend1, 'Color', 'none')
xlabel('Sensed Frequencies');
ylabel('Utilization');
title('Frequency Utilization (PU Activity = 0.2)');
grid on;
figfontsizes(11,9);
legend2latex(util1);
% legendstr_h = findobj(gca,'Tag', 'legend2latex_text');
% xlim_ax1 = get(gca,'XLim'); ylim_ax1 = get(gca,'YLim');
% set(legendstr_h, 'Position', [min(xlim_ax1)+1.7*range(xlim_ax1), 1.0125*max(ylim_ax1) 0]);
laprint(gcf,'util1','width',11,...
'asonscreen','off','keepfontprops','off',...
'factor',1,'scalefonts','off','mathticklabels','on');

Please login to add a comment or rating.
Updates
26 Nov 2009

The updated version can also handle stem, area and bar graphs.

Tag Activity for this File
Tag Applied By Date/Time
latex Emile Demarteau 28 Sep 2009 10:59:31
laprint Emile Demarteau 28 Sep 2009 10:59:31
print Emile Demarteau 28 Sep 2009 10:59:31
export Emile Demarteau 28 Sep 2009 10:59:31
documents Emile Demarteau 28 Sep 2009 10:59:31
latex Chirackel Yoonus 10 Dec 2009 03:58:15

Contact us at files@mathworks.com