Legend bounding box cropped when exporting to PDF/vector with LaTeX interpreter and advanced commands (\widehat, \cdot)
Show older comments
When I use legends with LaTeX commands and interpreter, and then I export it to a vector format, I usually get part of the legend outside of its bounding box. I tried different vector formats (eps, svg, pdf...) and different export options (exportgraphics, export_fig, print...) and the results are, if not the same, pretty close. The legend within MATLAB looks perfect. If I save it in raster format, it's perfect. The problem is only with vector format.
Here is a MWE:
figure(1)
set(gcf,'Units','centimeters','Position',[2 2 8.8 3.5])
plot(0:10,rand(1,11))
hold on
plot(0:10,rand(1,11))
plot(0:10,rand(1,11))
hold off
grid on
xlabel("Time (s)","interpreter","latex")
ylabel("$e\widehat{MS}$ (pu)","interpreter","latex")
legend("$e\widehat{MS}_{PM}$","$e\widehat{MS}_{PM}$ ($Rate\cdot 0.25$)","$e\widehat{MS}_{PM}$ ($Rate\cdot 100$)","location","northeastoutside","interpreter","latex")
exportgraphics(gcf, 'FigureTest.pdf')
The plot in MATLAB looks more or less like this (copied with Copy Figure):

But the vector output looks like this (PDF screenshot):

There are some other minor differences, but not as relevant as the legend bounding box.
Answers (2)
Submit this to Mathworks as an official support request/bug at <Product Support Page>
Probably the only recourse without Mathworks intervention would be to experiment with the legend width in the Position property. That would likely also entail/require moving the left position slightly to the left or first making the axes just a tad narrower to give a little more room for the legend.
Out of curiosity, have you done the above sort of thing in an earlier release without seeing the issue?
I ran the code snippet in R2021b and opened the pdf file in Acrobat reader -- the legend box didn't clip the legend text even when displayed at actual size.
2 Comments
Javier Gómez-Aleixandre Tiemblo
on 3 Mar 2026
dpb
on 3 Mar 2026
Good test -- I've not yet taken the leap to even download R2025+ releases given all the issues and have a pretty slow connection so it takes alongtime™ (not everybody is close to major population center; out here in the hinterlands, bandwidth is far more limited).
Anyways, a bug report with the attached pdf file from the same code from the earlier release would seem appropriate.
BTW, the code could be simplified a little (albeit I recognize it may have come from somewhere else where the data weren't available as an array) for submittal --
set(gcf,'Units','centimeters','Position',[2 2 8.8 3.5])
plot(0:10,rand(3,11))
grid on
xlabel("Time (s)","interpreter","latex")
ylabel("$e\widehat{MS}$ (pu)","interpreter","latex")
legend("$e\widehat{MS}_{PM}$","$e\widehat{MS}_{PM}$ ($Rate\cdot 0.25$)","$e\widehat{MS}_{PM}$ ($Rate\cdot 100$)","location","northeastoutside","interpreter","latex")
exportgraphics(gcf, 'FigureTest.pdf')
sooraj sunil
on 9 Mar 2026
Edited: sooraj sunil
on 9 Mar 2026
0 votes
The only work around I found is to use: exportgraphics(gcf, 'FigureTest.pdf','ContentType','image') — but the quality of the exported plot is not great!
or
legend(Box='off')
or
legend(EdgeColor='w')
1 Comment
Javier Gómez-Aleixandre Tiemblo
on 10 Mar 2026
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!