why exportgraphics fails to print the legend when the content type is vector and the legend is latex interpreted?
44 views (last 30 days)
Show older comments
when exporting a fig as a vector, e.g. eps or pdf, if the legend is latex interpreted, the exported graphics do not show the legend. Below a snippet of my code:
% Figure
if showFig
f = figure('Color','w');
else
f = figure('Visible','off','Color','w');
end
hold on; grid on; box on; set(gca,'YScale','log');
cols = lines(9);
% Plot ML refined (solid) and CRB/PEB (dashed, black markers)
% Case A
p1 = semilogy(x, RMSE_A, '-o', 'Color', cols(1,:), 'LineWidth', 1.8, 'MarkerSize', 6);
p2 = semilogy(x, PEB_A, '--o', 'Color', [0 0 0], 'LineWidth', 1.3, 'MarkerSize', 5);
% Case B
p3 = semilogy(x, RMSE_B, '-s', 'Color', cols(2,:), 'LineWidth', 1.8, 'MarkerSize', 6);
p4 = semilogy(x, PEB_B, '--s', 'Color', [0 0 0], 'LineWidth', 1.3, 'MarkerSize', 5);
% Case C
p5 = semilogy(x, RMSE_C, '-^', 'Color', cols(3,:), 'LineWidth', 1.8, 'MarkerSize', 6);
p6 = semilogy(x, PEB_C, '--^', 'Color', [0 0 0], 'LineWidth', 1.3, 'MarkerSize', 5);
semilogy(R.values, 20*ones(size(R.values)),':r','LineWidth',1.8);
xlim([min(x) max(x)])
legend([p1 p2 p3 p4 p5 p6], ...
{'ML RMSE_A','PEB_A','ML RMSE_B','PEB_B','ML RMSE_C','PEB_C'}, ...
'Location','best','Interpreter','latex');
xlabel(xlab, 'Interpreter','latex');
ylabel('RMSE / PEB [mm]', 'Interpreter','latex');
title(run_title, 'Interpreter','none');
set(gca,'TickLabelInterpreter','latex','FontSize',18,'LineWidth',1.2);
% EXPORT FIG
drawnow; % ensure legend is drawn
set(f, 'Renderer', 'painters'); % force vector renderer
set(f, 'PaperPositionMode','auto'); % match figure size
[~, base, ~] = fileparts(mat_path);
out_path = fullfile(files(i).folder, sprintf('%s_PEB_ML.%s', base, fmt));
switch lower(fmt)
case 'eps'
saveas(f,out_path,'epsc')
case 'pdf'
exportgraphics(gca, [out_path, '.pdf'], 'ContentType','vector')
case 'png'
print(f, out_path, '-dpng','-r300');
otherwise
warning('Unknown fmt: %s. Skipping save.', fmt);
end
end

2 Comments
Rik
on 3 Sep 2025
Here is a hex dump of what 2024b does. I couldn't find a free way to convert a pdf to an image in this interface (there are ways in Linux, but the code run feature runs in a very minimal setup).
If you copy this to your local machine you can check whether your results match.
x = 1:100;y1 = x.^2;y2 = x.^3;semilogy(x,y1,'--',x,y2)
legend('$x^2$','$x^3$','Location','northwest','interpreter','latex')
exportgraphics(gca,'figure1.pdf','ContentType','vector')
close
fid=fopen('figure1.pdf','rb');
buffer=cell(0);
while ~feof(fid)
data=fread(fid,40,'uint8');
buffer{end+1}=sprintf('%02x',data);
end
fclose(fid);
fprintf('%s\n',buffer{:})
Answers (2)
Shantanu
on 5 Sep 2025
Hi Marouan,
I understand the issue that legend is not getting displayed on exporting plot as PDF, I have executed given code on MATLAB R2025a, and it seems that the LaTeX interpreter fails on unescaped underscore characters (_). This causes an error when calculating the text size for the exported PDF file, making the legend disappear.
The fix is to simply escape each underscore with a backslash (\).
legend([p1 p2 p3 p4 p5 p6], ...
{'ML RMSE\_A','PEB\_A','ML RMSE\_B','PEB\_B','ML RMSE\_C','PEB\_C'}, ...
'Location','best','Interpreter','latex');
Hope this solves the problem.
1 Comment
dpb
on 5 Sep 2025
Edited: dpb
on 5 Sep 2025
The above will remove the LaTeX warning, yes, but it won't create the desired legend with the underscored character as OP desires; the MATLAB LaTeX interpreter requires the string to be encapsulated within the $ signs per the documentation (which isn't all that easy to find, easiest way is to go to the text page first).
y=rand(20,6);
subplot(2,1,1)
hL=plot(y);
legend(hL,{'ML RMSE\_A','PEB\_A','ML RMSE\_B','PEB\_B','ML RMSE\_C','PEB\_C'}, ...
'Location','eastoutside','Interpreter','latex');
title('Just Escape the Underscore')
subplot(2,1,2)
hL=plot(y);
legend(hL,{'$ML RMSE_A$','$PEB_A$','$ML RMSE_B$','$PEB_B$','$ML RMSE_C$','$PEB_C$'}, ...
'Location','eastoutside','Interpreter','latex');
title('Enclose legend Strings With $, No Escape')
dpb
on 5 Sep 2025
Edited: dpb
on 5 Sep 2025
@Shantanu was on the right track, but not quite correct to produce the intended result...
The <documentation on special characters with LaTeX> illustrates must enclose the to-be-interpreted-as-LaTeX-strings text in $ signs for special characters to be interpreted correctly.
y=randn(20,6)+repmat([3 10],1,3);
hL=plot(y);
legend(hL,{'$ML RMSE_A$','$PEB_A$','$ML RMSE_B$','$PEB_B$','$ML RMSE_C$','$PEB_C$'}, ...
'Location','eastoutside','Interpreter','latex');
title('Encapsulate legend Strings with $')
"Escaping" the underscore removes the warning so the legend text is created/displayed, but inserts the special character instead of implementing the behavior to subscript the following character as illustrated in above comment.
0 Comments
See Also
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!
