Search code examples
matlablatexmatlab-figureeps

Fix extra space in MATLAB's title plot


I'm writting the results for my thesis, this includes the generation of figures for my LaTeX document by using MATLAB code. I do this by making a figure of the data, and then I use the print command to save in an EPS file.

The problem is that the plot in the MATLAB window is correct as you can see here:

screen shot of MATLAB figure window

But when I compile my document in LaTeX (Lyx), the result is this:

screen shot of figure as it appears in the EPS file.

As you can see, I have an unexpected big extra space in "iLm" title. The same occurs when I use LaTeX code in the label of different signals.

Searching in the web I tried the following command:

set(groot,'DefaultTextInterpreter','latex');

But just prints "iL_m" like I wrote in the code. How can I make the spacing consistent in the EPS file?

Here's the code I'm using:

clear h n
figure(1)
h(1) = plot(iLmVal.time,iLmVal.data(:,2),'LineWidth',1,'color','k','DisplayName','Modelo');
hold on
h(2) = plot(iLmVal.time,iLmVal.data(:,4),'LineWidth',1,'color','r','DisplayName','Circuito');
legend(h,'Location','southeast'),...
axis([0 0.06 -18 27]),title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times-Roman'),...
set(gca,'Color','white');
set(gca,'XTick',0:0.005:0.06),...
    set(gca,'XTickLabel',0:5:60,'FontSize',20,'FontName', 'Times-Roman','XMinorGrid','on'),...
    xlabel('Tiempo [ms]','FontSize',20,'FontName', 'Times-Roman'),...
set(gca,'YTick',-18:4:28),...
    set(gca,'YTickLabel',-18:4:28,'FontSize',20,'FontName', 'Times-Roman','YMinorGrid','on'),...
    ylabel('Corriente [A]','FontSize',20,'FontName', 'Times-Roman'),...
    n = gca;
    n.YAxis.MinorTick = 'on'; n.YAxis.MinorTickValues = -18:1:28;
    n.XAxis.MinorTick = 'on'; n.XAxis.MinorTickValues = 0:0.0025:0.07;
    grid on; hold off

I'm using MATLAB R2018a and Lyx 2.3.2-2. Also, by printing in PNG this problem doesn't occur, but the quality and resolution is very poor.


Solution

  • I don't think this is related to LyX, you should see this issue in the exported EPS file. You can fix this using a different font.

    As you can see in the appearance of the figure in MATLAB, where the title is shown using a sans-serif font (definitely not 'Times-Roman'), MATLAB does not recognize the 'Times-Roman' font, and uses an alternative for rendering. This alternative font is used to determine the location of the subscript, which is positioned independently of the main text by MATLAB. However, this font name is written to the EPS file. When rendering the EPS file in a different program, the 'Times-Roman' font is recognized and used to render the text. Because this font has different metrics to the one used by MATLAB, the location of subscripts is not correct.

    When printing to PNG, MATLAB creates a bitmap, therefore this problem does not occur.


    On my computer (macOS), the fooling produce a correct representation on screen:

    title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times')
    title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times-Roman')
    title("Corriente de magnetización iL_m",'FontSize',20,'FontName','TimesRoman')
    title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times New Roman')
    

    The following doesn't:

    title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times Roman')
    

    On different computers, different font names will be available. Use a name that is recognized on your computer. Your best bet is 'Times', which is the PostScript name for this font and should be recognized everywhere.


    Alternatively, use the export_fig utility on the File Exchange. This is a great tool for exporting MATLAB figures to EPS. It will not only fix your fonts, but make many other little tweaks that will improve the look of your figures.