Code covered by the BSD License

### Highlights from Legend to latex converter

5.0
5.0 | 4 ratings Rate this file 24 Downloads (last 30 days) File Size: 23.8 KB File ID: #25426 Version: 1.3

# Legend to latex converter

### Emile Demarteau (view profile)

28 Sep 2009 (Updated )

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

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

La Print inspired this file.

MATLAB release MATLAB 7.5 (R2007b)
16 Oct 2013 Philipp

### Philipp (view profile)

I'm trying to use legend2latex for a bar-graph, but I get the following error:
ans =
flat

Subscripted assignment dimension mismatch.

Error in legend2latex (line 91)
patchColor(i,:) = get(ud.handles(i),
'FaceColor');

I hope you can help me!
Thanks!

Comment only
16 Aug 2012 Anthony

### Anthony (view profile)

Very nice function! I found another bug - it doesn't place the legend properly for axes that have a log scale. To fix this:

First determine whether the axes have a log or linear scale. I did this with:

axes_handle_var = get(figure_handle,'currentaxes');
x_axis_scale = get(axes_handle_var,'xscale');

If the scale is logarithmic then the code in 'legend2latex.m' around line 239 must be changed to:

legendx = (min(get(plots(1),'XLim')))^(1-legend_percentual_x) * (max(get(plots(1),'XLim')))^(legend_percentual_x);

It would be best to use an 'if' statement to properly place the legend for both scales. Change the 'ylegend' code in a similar way.

25 Apr 2012 Tor

### Tor (view profile)

Really nice function. However, the font size in the legend is not preserved. This is easy to fix with something like
fontsize = get(legh,'FontSize');
...
set(legend_lbl, 'FontSize', fontsize);

Moreover, the script does not check for horizontal orientation of the legend. This is a little more involved to fix, but I have a quick and dirty fix if anyone wants it.

In my opinion, this function works even better if used with matlabfrag instead of laprint.

28 Apr 2011 Henc

### Henc (view profile)

In order to use this with pentagrams, the following line needs to be added within the 'pentagram' and 'p' cases:
markers_edge{i} = '\large{$\bigstar$}';

Otherwise, the symbols fail to show up in the legend.

20 Jul 2010 David

### David (view profile)

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');

Comment only
07 Jul 2010 Emile Demarteau

### Emile Demarteau (view profile)

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

Comment only
23 Jun 2010 Pascal SZACHERSKI

### Pascal SZACHERSKI (view profile)

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!

Comment only
06 May 2010 Marco

### Marco (view profile)

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?

Comment only
22 Mar 2010 Dylan

### Dylan (view profile)

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

16 Mar 2010 Erik

### Erik (view profile)

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?)

Comment only
28 Oct 2009 Emile Demarteau

### Emile Demarteau (view profile)

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');

Comment only
19 Oct 2009 Diane Bastien

### Diane Bastien (view profile)

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

Comment only