Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Matlab placing text spaces in legend using Latex when the figure gets saved as a PDF?

I am trying to put a legend in Matlab figures that include a symbol in Latex. When I plot the figure, the legend looks fine. However, when I export the figure as a PDF, the legend gets spaces put into it. I don't know why this is happening. Sample code is as follows:

set(groot,'defaultLineLineWidth',2,'defaultAxesFontSize',...
    12,'defaultAxesFontName','timesnewroman',...
    'defaulttextinterpreter','latex')
x0 = 8;
y0 = 5;
width = 5;
height = 4;
kappa1 = 0.1;
kappa2 = 0.5;
f = linspace(0,2*pi,1000);
y1 = sin(f+kappa1*f.^2);
y2 = sin(f+kappa2*f.^2);

figure(1)
hold on
plot(f,y1,'k')
plot(f,y2,'b')
xlabel('Frequency (MHz)')
ylabel('Amplitude')
legend(strcat('\kappa = 0.1 MHz/','\mu','s'),...
    strcat('\kappa = 0.5 MHz/','\mu','s'))
grid on;
set(gcf,'units','inches','Position',[x0,y0,width,height],...
    'PaperPositionMode','Auto','PaperUnits','Inches',...
    'PaperSize',[width, height]);
saveas(gcf,'legendtest.pdf')

It seems like the error happens when I save the file as a PDF. It saves as a JPG just fine. Below are the two images I get. The jpg is:

testlegendjpg

But the PDF I get is:

enter image description here

I am using Matlab version R2017a on a Mac running OS 10.12.5. Thanks in advance for any help!

like image 314
Lou Avatar asked Jul 31 '17 19:07

Lou


People also ask

How do I change the legend style in MATLAB?

To change the title appearance, such as the font style or color, set legend text properties. For a list, see Text Properties. plot(rand(3)); lgd = legend('line 1','line 2','line 3'); lgd.

How do you control a legend position in MATLAB?

legend(___,'Location', lcn ) sets the legend location. For example, 'Location','northeast' positions the legend in the upper right corner of the axes. Specify the location after other input arguments. legend(___,'Orientation', ornt ) , where ornt is 'horizontal' , displays the legend items side-by-side.

Does MATLAB support LaTeX?

MATLAB supports most standard LaTeX math mode commands.


1 Answers

This is a known bug. See the bug report according to which it affects the versions from R2014b to R2017a. A workaround is suggested in that bug report as well which is to generate the pdf file by setting the Renderer to opengl i.e.

set(gcf,'Renderer','opengl');

But then the generated pdf file contains a pixel graphic instead of a vector graphic.


Better thing would be to use the respective unicode values which will produce a vector graphic. i.e.

legend([char(954), ' = 0.1 MHz/',char(956),'s'],...
    [char(954), ' = 15 MHz/',char(956),'s']); %char(954) equals 'κ', char(956) equals 'μ'

If you want to use italic characters, it is also possible with unicode characters.

legend([char([55349,57093]), ' = 0.1 MHz/',char([55349,57095]),'s'],...
    [char([55349,57093]), ' = 15 MHz/',char([55349,57095]),'s']); 
%char([55349,57093]) equals '𝜅' (italic κ), char([55349,57095]) equals '𝜇' (italic μ)
like image 136
Sardar Usama Avatar answered Sep 17 '22 08:09

Sardar Usama