File Exchange

image thumbnail

Legend to latex converter

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


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

Cite As

Emile Demarteau (2020). Legend to latex converter (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)


%%I would use legend2latex with laprint, but i get an error:%%
Error using legend2latex (line 10)
No legend found! Are you sure you selected the figure
(NOT the axes), i.e. gcf?

Error in testplotfurlaprint (line 4)

%%And my example code is:%%
fig = figure(1), clf

%%%%%%%%%The legend2latex code at line 10 is:%%%%%
function out = legend2latex(figure_handle)
% gets all legend entries and converts these to a cel
Line 10 : leghs = findobj(figure_handle,'Type','Axes','Tag','legend');

What was wrong with figure_handle?


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!


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.


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.


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.


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

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

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!


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?


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)


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,

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

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


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

Inspired by: LaPrint