File Exchange

image thumbnail

Legend to latex converter

version 1.3.0.0 (23.8 KB) by Emile Demarteau
This function converts a Matlab legend to a text annotation for latex interpretation.

0 Downloads

Updated 26 Nov 2009

View License

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

Comments and Ratings (12)

Philipp

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!

Anthony

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.

Tor

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.

Henc

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.

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

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

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!

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?

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

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

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

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

Updates

1.3.0.0

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

MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired by: LaPrint