Remove whitespace in Legend with Latex Labels

Hi, does anyone know why there is so much whitespace after each entry in the legend here? I've tried to shrink the legend width by doing:
pos = legend.Position;
pos(3) = 0.5*pos(3);
legend.Position = pos;
But this hasn't worked. I've attached the code below.
Any help would be appreciated.
Here is the code below: (I'm sure there is a better way to write the legend names out, but I'm lazy and this was good enough for my purposes).
f = figure;
hold on
plot(Cl, Cm(:,1),"-bsquare")
plot(Cl, Cm(:,3),"-bdiamond")
plot(Cl, Cm(:,5),"-b^")
plot(Cl, Cm(:,7),"-bv")
plot(Cl, Cm(:,2),"-rsquare")
plot(Cl, Cm(:,4),"-rdiamond")
plot(Cl, Cm(:,6),"-r^")
plot(Cl, Cm(:,8),"-rv")
hold off
xlim([-1.1, 1.4])
leg = legend({'$x_{cg}=0.18\bar{c}, \delta_e = 0\degree $','$x_{cg}=0.22\bar{c}, \delta_e = 0\degree $','$x_{cg}=0.26\bar{c}, \delta_e = 0\degree $','$x_{cg}=0.30\bar{c}, \delta_e = 0\degree $','$x_{cg}=0.18\bar{c}, \delta_e = 5\degree $','$x_{cg}=0.22\bar{c}, \delta_e = 5\degree $','$x_{cg}=0.26\bar{c}, \delta_e = 5\degree $','$x_{cg}=0.30\bar{c}, \delta_e = 5\degree $'},'Interpreter','latex', 'Location','northeast', 'FontSize',11);
% pos = leg.Position;
% pos(3) = 0.25*pos(3); % Tried this, didn't work
% leg.Position = pos;
grid on
ax = gca;
ax.GridLineWidth = 1.5;
ax.XAxis.MinorTick = 'on';
ax.XMinorGrid = 'on';
ax.YAxis.MinorTick = 'on';
ax.YMinorGrid = 'on';
f.Units = 'pixels';
f.Position = [10,10,990,490];

2 Comments

For testing, could you attach a .mat that defines Cl and Cm ?
Hi, is a csv okay? The first column is Cl, the rest are Cm:
So importing should be:
dat = readmatrix("Cl_Cm.csv");
Cl = dat(:,1);
Cm = dat(:,2:9);

Sign in to comment.

 Accepted Answer

Cl=linspace(-1,1.4).';
Cm=linspace(4,3.2); Cm=repmat(Cm,8,1).';
Cm=Cm+[0 -0.075 -0.2 -0.255 -0.4 -0.455 -0.6 -0.655];
f=figure('Units','pixels','Position',[10,10,990,490]);
hold on
plot(Cl, Cm(:,1),"-bsquare")
plot(Cl, Cm(:,3),"-bdiamond")
plot(Cl, Cm(:,5),"-b^")
plot(Cl, Cm(:,7),"-bv")
plot(Cl, Cm(:,2),"-rsquare")
plot(Cl, Cm(:,4),"-rdiamond")
plot(Cl, Cm(:,6),"-r^")
plot(Cl, Cm(:,8),"-rv")
hold off
xlim([-1.1, 1.4]), ylim([2.2 4.2])
leg = legend({'$x_{cg}=0.18\bar{c}, \delta_e = 0^{o}$', ...
'$x_{cg}=0.22\bar{c}, \delta_e = 0^{o}$', ...
'$x_{cg}=0.26\bar{c}, \delta_e = 0^{o}$', ...
'$x_{cg}=0.30\bar{c}, \delta_e = 0^{o}$', ...
'$x_{cg}=0.18\bar{c}, \delta_e = 5^{o}$', ...
'$x_{cg}=0.22\bar{c}, \delta_e = 5^{o}$', ...
'$x_{cg}=0.26\bar{c}, \delta_e = 5^{o}$', ...
'$x_{cg}=0.30\bar{c}, \delta_e = 5^{o}$'}, ...
'Interpreter','latex', 'Location','southwest', 'FontSize',11);
grid on
ax = gca;
ax.GridLineWidth = 1.5;
ax.XAxis.MinorTick = 'on';
ax.XMinorGrid = 'on';
ax.YAxis.MinorTick = 'on';
ax.YMinorGrid = 'on';
The MATLAB LaTeX interpreter doesn't like the \degree because it is in a package and not base LaTeX.
Prior releases warn of bad syntax although they don't give any klew as to what it is that isn't kosher so it's a witch hunt to track down what it doesn't like. (Although anything that is shown as being in some package will fail so anything that needs a package will not work in MATLAB, so there's where to start).
Alternative is a superscript "o"; this has been a subject of angst for years...
ADDENDUM
LaTeX uses circles so an alternative could be
p=[0.18:0.04:0.30].'; p=[p;p];
d=[zeros(4,1); 5*ones(4,1)];
lgds=compose('$x_{cg}=%.2f\\bar{c}, \\delta_e = %d^{\\circ}$',p,d);
which is accepted as it doesn't need to load a package as with \degree or \textdegree

6 Comments

dpb
dpb on 23 Sep 2025
Edited: dpb on 23 Sep 2025
ADDENDUM
I'd suggest submitting this thread with a support request to return the warning to latest release(s) to avoid this going forward. Being more explicit about what actually caused the failure if possible would be agoodthing™
Wouldn't hurt to ask for documentation on how to add a package to the embedded LaTeX interpreter or adding additional specific features such as \degree. It's been 30 years and subject of countless threads in the past with no action, so it's probably futile, but would be yet another voice in the wilderness...
p=[0.18:0.04:0.30].'; p=[p;p];
d=[zeros(4,1); 5*ones(4,1)];
lgds=compose('$x_{cg}=%.2f\\bar{c}, \\delta_e = %d^{o}$',p,d)
lgds = 8×1 cell array
{'$x_{cg}=0.18\bar{c}, \delta_e = 0^{o}$'} {'$x_{cg}=0.22\bar{c}, \delta_e = 0^{o}$'} {'$x_{cg}=0.26\bar{c}, \delta_e = 0^{o}$'} {'$x_{cg}=0.30\bar{c}, \delta_e = 0^{o}$'} {'$x_{cg}=0.18\bar{c}, \delta_e = 5^{o}$'} {'$x_{cg}=0.22\bar{c}, \delta_e = 5^{o}$'} {'$x_{cg}=0.26\bar{c}, \delta_e = 5^{o}$'} {'$x_{cg}=0.30\bar{c}, \delta_e = 5^{o}$'}
NOTA BENE the need to escape the escape characters so they will be embedded in the string.
Then call legends with the lgds array...
Using vectorized calls to plotting routines can shorten code...
Cl=linspace(-1,1.4).';
Cm=linspace(4,3.2); Cm=repmat(Cm,8,1).';
Cm=Cm+[0 -0.075 -0.2 -0.255 -0.4 -0.455 -0.6 -0.655];
markers=cellstr(['s','d','^','v'].');
colors=cellstr(['b','r'].');
p=[0.18:0.04:0.30].'; p=[p;p];
d=[zeros(4,1); 5*ones(4,1)];
lgds=compose('$x_{cg}=%.2f\\bar{c}, \\delta_e = %d^{\\circ}$',p,d);
hF=figure('Units','pixels','Position',[10,10,990,490]);
hL=plot(Cl, Cm);
set(hL(1:2:end),{'Marker'},markers,{'Color'},colors(1))
set(hL(2:2:end),{'Marker'},markers,{'Color'},colors(2))
xlim([-1.1, 1.4]), ylim([2.2 4.2])
hLg=legend(lgds,'Interpreter','latex', 'Location','southwest', 'FontSize',11);
hAx=gca;
set(hAx,'XGrid','on','YGrid','on','GridLineWidth',1.5,'XMinorGrid','on','YMinorGrid','on');
hAx.XAxis.MinorTick='on'; hAx.YAxis.MinorTick='on';
Amazing, thank you for your help here.
Understood that it's best to use circles instead of the "\degree" command for the latex interpreter.
dpb
dpb on 24 Sep 2025
Edited: dpb on 24 Sep 2025
No problem, glad to help.
What I had failed to research before, however, is that it appears the \degree symbol has been added and <the documentation has been updated to include it> in current release; this table after the line starting with vdots is new. I simply didn't think there was any likelihood of such additions having been made and so didn't look at current documentation.
But it does appear that there are still some warts in using at least some of them.
I'll amend my recommendation to one of this is a bug with a new feature and a formal bug report should be filed.
ADDENDUM
I did go ahead and submitted the bug report.

Sign in to comment.

More Answers (0)

Products

Release

R2025a

Tags

Asked:

on 23 Sep 2025

Commented:

dpb
on 25 Sep 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!