Very irritating there is enough size on the graph to widen the legend but it does not resize automatically to the content. Anyone a suggestion on how to fix this? (latex interpreter set to legend)
Error marked in red where legend data falls out of the legend box


Fully minimal working example of my problem:
window=figure('Visible','on');
axes1 = axes('Parent',window);
grid(axes1,'on');
hold(axes1,'all');
set(window, 'Position', [0 0 1000 1000]);
x=rand(1000,1);
y=rand(1000,1);
companyMap='companyname of datapoint'
%draw scatter in window
scatter(x,y,'fill','MarkerFaceColor',[0,0,1],'MarkerEdgeColor',[0,0,1],'Marker','.','SizeData',30,'Parent',axes1,'DisplayName',sprintf('%s Datapoint',strrep(companyMap,'&','\&')));
linFit = fitlm(x,y);
dispName = sprintf('Linear fit: $y=%.3f x+ %.3f$, $R^{2}=%.3f$',linFit.Coefficients.Estimate(2),linFit.Coefficients.Estimate(1),linFit.Rsquared.Ordinary);
if linFit.Coefficients.Estimate(1) < 0
dispName = strrep(dispName,'+','');
end
%lsline('linewidth',2,'Parent',axes1,'DisplayName',dispName);
line(x,linFit.Fitted,'Parent',axes1,'Tag','lsline','Color',[0 0 0],'DisplayName',dispName,'linewidth',2);
legend1 = legend(axes1,'show');
set(legend1,'Interpreter','latex');
%set(legend1, 'FontSize',7);
set(legend1,'Location','Best');
% Create title
title( { 'tit' }, 'FontSize',20);
% Create xlabel
xlabel( { 'x' } , 'FontSize',16, 'Interpreter','LaTex');
% Create ylabel
ylabel( { 'y' } , 'FontSize',16, 'Interpreter','LaTex');
'\n'insprintf. - Yvon[hleg1, hobj1] = legend(axes1,'show'); textobj = findobj(hobj1, 'type', 'text'); set(textobj, 'Interpreter', 'latex'); set(hleg1,'position',[.2,.2,.8,.8])mathworks.com/matlabcentral/answers/… - Yvon