Text inside legend box does not fit in automatically

20 views (last 30 days)
I recently updated my MATLAB to 2025b and am experiencing some trouble when drawing figures.
As noted by the attached figure, the text inside the legend does not fit into the box automatically.
I tried manually adjusting the box size using commands like
lgd.Units = 'normalized';
pos = lgd.Position;
pos(1) = pos(1) + 0.003;
pos(2) = pos(2) - 0.035;
pos(3) = pos(3) * 1.001;
pos(4) = pos(4) * 1;
lgd.Position = pos;
But manually adjusting is highly laborious since I have to try several times to find the exact box size that covers the text.
It seems it happens when I use 'interpreter',' latex' option, and saving the file using .pdf extension. It didn't happen when I used the 2024b version of MATLAB.
If possible could anyone tell me how to draw figures with appropriate box size without manually adjusting it as I could in the previous version of MATLAB.
I have to use both 'interpreter', 'latex' option, and saving the file using the .pdf extension.

Answers (1)

dpb
dpb on 26 Nov 2025
Edited: dpb on 26 Nov 2025
I expect this is another manifestation of the changes with the new rendering engine introduced with R2025.
Submit this to Mathworks as an official support request/bug at <Product Support Page>. I would suggest supplying a comparison figure with prior release to illustrate the issue.
I'm not sure otomh about a programmatic way using the 'Extent' to make a fixup will work(*); perhaps Mathworks can provide that as part of their response. Meanwhile, unless there is some other unavoidable feature in R2025 that you need, I'd suggest to stick with the prior release if it works as desired until do get a better resolution from Mathworks support.
(*) Well, let's see if can create the symptom...guessing it also has to do with the subplot configuration making for smaller axes than full-sized...
TL=tiledlayout(2,3);
for i=1:4, hT(i)=nexttile(TL); end % work our way down
yMax=0.0015;
y0=[linspace(0.0003,0.001,3) yMax];
xMax=1.04;
x=linspace(0,xMax).';
d=abs(log(y0/xMax));
y=y0.*exp(-d.*x);
plot(x,y)
hT(i).YAxis.Exponent=0;
hT(i).YAxis.TickLabelFormat='%0.4f';
hLg=legend('RANK','Cyclical long','Cyclical + LR Income','Ineq + Risk', ...
'Interpreter','latex');
Well this doesn't reproduce OP's issue; the text fits in the legend but the legend is way bigger than the axes -- not sure just how got that many on a figure with the aspect ratio his figure shows.
Need to add the code that generated the figure...
  1 Comment
종수
종수 on 27 Nov 2025
Edited: 종수 on 27 Nov 2025
figure
subplot(2,3,1)
plot(Piiss_vec.^(4),mean_y_vec(:,1,1,1),'LineWidth',3)
hold on
plot(Piiss_vec.^(4),mean_y_vec(:,4,1,1),'LineWidth',3,'LineStyle','--')
plot(Piiss_vec.^(4),mean_y_vec(:,4,4,1),'LineWidth',3,'LineStyle','-.')
plot(Piiss_vec.^(4),mean_y_vec(:,4,4,2),'LineWidth',3,'LineStyle',':')
grid on
xlabel('$\mathbf{\Pi}$ (p.a)','Interpreter','latex','fontsize',12)
ax = gca; % axes handle
ax.YAxis.Exponent = 0;
title('\textbf{$\mathbf{E(\hat{y}_{t})}$}','Interpreter','latex','fontsize',16)
subplot(2,3,2)
plot(Piiss_vec.^(4),mean_pii_vec(:,1,1,1),'LineWidth',3)
hold on
plot(Piiss_vec.^(4),mean_pii_vec(:,4,1,1),'LineWidth',3,'LineStyle','--')
plot(Piiss_vec.^(4),mean_pii_vec(:,4,4,1),'LineWidth',3,'LineStyle','-.')
plot(Piiss_vec.^(4),mean_pii_vec(:,4,4,2),'LineWidth',3,'LineStyle',':')
grid on
xlabel('$\mathbf{\Pi}$ (p.a)','Interpreter','latex','fontsize',12)
ax = gca; % axes handle
ax.YAxis.Exponent = 0;
title('\textbf{$\mathbf{E(\hat{\pi}_{t})}$}','Interpreter','latex','fontsize',16)
subplot(2,3,4)
plot(Piiss_vec.^(4),mean_y_vec(:,1,1,1).^(2) + var_y_vec(:,1,1,1),'LineWidth',3)
hold on
plot(Piiss_vec.^(4),mean_y_vec(:,4,1,1).^(2) + var_y_vec(:,4,1,1),'LineWidth',3,'LineStyle','--')
plot(Piiss_vec.^(4),mean_y_vec(:,4,4,1).^(2) + var_y_vec(:,4,4,1),'LineWidth',3,'LineStyle','-.')
plot(Piiss_vec.^(4),mean_y_vec(:,4,4,2).^(2) + var_y_vec(:,4,4,2),'LineWidth',3,'LineStyle',':')
grid on
xlabel('$\mathbf{\Pi}$ (p.a)','Interpreter','latex','fontsize',12)
ax = gca; % axes handle
ax.YAxis.Exponent = 0;
title('\textbf{$\mathbf{E(\hat{y}^{2}_{t})}$}','Interpreter','latex','fontsize',16)
legend('RANK','Cyclical Ineq','Cyclical + LR Ineq','Ineq + Risk','Interpreter','latex','fontsize',10,'location','northeast')
subplot(2,3,5)
plot(Piiss_vec.^(4),mean_pii_vec(:,1,1,1).^(2) + var_pii_vec(:,1,1,1),'LineWidth',3)
hold on
plot(Piiss_vec.^(4),mean_pii_vec(:,4,1,1).^(2) + var_pii_vec(:,4,1,1),'LineWidth',3,'LineStyle','--')
plot(Piiss_vec.^(4),mean_pii_vec(:,4,4,1).^(2) + var_pii_vec(:,4,4,1),'LineWidth',3,'LineStyle','-.')
plot(Piiss_vec.^(4),mean_pii_vec(:,4,4,2).^(2) + var_pii_vec(:,4,4,2),'LineWidth',3,'LineStyle',':')
grid on
xlabel('$\mathbf{\Pi}$ (p.a)','Interpreter','latex','fontsize',12)
ax = gca; % axes handle
ax.YAxis.Exponent = 0;
title('\textbf{$\mathbf{E(\hat{\pi}^{2}_{t})}$}','Interpreter','latex','fontsize',16)
subplot(2,3,6)
plot(Piiss_vec.^(4),cov_ya_vec(:,1,1,1),'LineWidth',3)
hold on
plot(Piiss_vec.^(4),cov_ya_vec(:,4,1,1),'LineWidth',3,'LineStyle','--')
plot(Piiss_vec.^(4),cov_ya_vec(:,4,4,1),'LineWidth',3,'LineStyle','-.')
plot(Piiss_vec.^(4),cov_ya_vec(:,4,4,2),'LineWidth',3,'LineStyle',':')
grid on
xlabel('$\mathbf{\Pi}$ (p.a)','Interpreter','latex','fontsize',12)
ax = gca; % axes handle
ax.YAxis.Exponent = 0;
title('\textbf{$\mathbf{COV(\hat{y}_{t},\hat{A}_{t})}$}','Interpreter','latex','fontsize',16)
x0=0;
y0=0;
width=1000;
height=400;
set(gcf,'units','points','position',[x0,y0,width,height],'color','w')
exportgraphics(gcf, '1_aggregate_moments.pdf','Resolution',1080)
Above is the code I used to generated attached figure. If your figure is saved with pdf extension, it seems your figure does not suffer box size issue while it is 2025a version.
I also sended this issue with attached file to the support team of MATLAP as you suggested.
Anyway, I appreciate your interest on this problem.

Sign in to comment.

Products


Release

R2025b

Community Treasure Hunt

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

Start Hunting!