View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Legend to latex converter

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

Legend to latex converter



28 Sep 2009 (Updated )

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

| Watch this File

File Information

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


La Print inspired this file.

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (12)
16 Oct 2013 Philipp

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

Subscripted assignment dimension mismatch.

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

I hope you can help me!

Comment only
16 Aug 2012 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.

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');
title('Frequency Utilization (PU Activity = 0.2)');
grid on;
% 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]);

Comment only
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

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

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)

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

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,

%%%%% EXAMPLE %%%%%%
x = [0:0.1:(2*pi)];
fig1 = figure(1);
xlabel('$0 \leq \theta \leq 2\pi$')

Comment only
19 Oct 2009 Diane Bastien

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

Comment only
26 Nov 2009 1.3

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

Contact us