Legend bounding box cropped when exporting to PDF/vector with LaTeX interpreter and advanced commands (\widehat, \cdot)

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

Good point. I didn't remember I had still installed older versions. And I've just tried with R2024b, the version just before mine, and it appears to be perfect. So I guess it's one of the changes implemented in the big change for R2025a.
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')

Sign in to comment.

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

The first workaround is like exporting it in any raster format (I guess a raster imagen inside the pdf), not suitable for my needs.
And the second/third was my initial workaround, until the advice from dpb to test in older versions. And I'd rather keep using R2024b for this, as I already have plots with the legend's box from older papers and I'd like to keep a good consistency.
Thanks anyways!

Sign in to comment.

Products

Release

R2025a

Community Treasure Hunt

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

Start Hunting!