Extra space in saved figure before subscripts or superscripts

I have plot axis labels that use subscripts and/or superscripts. The figures look exactly as they should when I plot them in Matlab. When I save them, I get an extra space before the subscript or superscript. WHY???? I've done this lots of times and never had this problem.
I've attached an example. The code for the example label is:
ylabel('PSD [m^2/s]','fontsize',14)
I've also tried putting {} around the 2, but it doesn't change anything. I can add latex as the interpreter, but then the font changes and I want the default font.
UPDATE: I just noticed that there's more space before or after any latex text. I used a \times in another label and there's more space before it, but some subscripts have a little space after them. I'm not sure if there's a pattern to this.

6 comentarios

They got as far as me, which is to say they used Latex as the interpreter. I'm going to leave mine up in case anyone has a way to maintain the default font and fix this problem.
Daniel
Daniel el 30 de Mzo. de 2021
Editada: Daniel el 30 de Mzo. de 2021
Did some additional searching and found a less than satisfactory fix. You can use Latex commands such as \textsf for sans serif or \testrm for Roman. This isn't perfect, though. When using \textsf, I get an error that Font cmss10 is not supported, which is apparently the sans serif font. My label in code was: ylabel('\textsf{Average Energy [$m^{2}s^{-2}$]}','fontsize',14,'interpreter','latex'). On screen, "Average Energy" was in sans serif, but the rest was not. After saving as .eps and viewing the saved file, "Average Energy" was in something more like Ariel.
This seems like it should a simple thing to do, so I'm leaving this unanswered in case someone figures it out.
Hi Daniel,
what Matlab version have you been using? I tried to reproduce the described issue with Matlab 2019b and was not able to:
fh = figure;
ah = axes;
ylabel('PSD [m^2/s]','fontsize',14)
saveas(fh,'test.png')
saveas(fh,'test.eps')
saveas(fh,'test2.eps','epsc')
print(fh,'testPrint','-dpng')
print(fh,'testPrint','-deps')
The figure looks similar to the original.
Kind regards,
Robert
Daniel
Daniel el 31 de Mzo. de 2021
Editada: Daniel el 31 de Mzo. de 2021
I'm using R2019b. I tried all of your .eps options, since that's ultimately what I need, and none of them worked. I opened the files both in Inkscape and in Adobe, since those are the only programs I have that can open an .eps. Same result in both. I'm stumped.
UPDATE: Weird thing. I did this with a figure I already had ready rather than try exactly what you tried. When I tried exactly what you tried, it did work. So I did this to test an example:
figure
plot(rand(size(1,10)),rand(size(1,10)))
xlabel('here is super^{script} to see')
ylabel('here is sub_{script} to see')
saveas(gcf,'test.eps','epsc')
I only tried the one save method, but this reproduced my original error. WHY???
Hi Daniel,
I cannot reproduce the described issue, using your example.
fh = figure;
ah = axes;
plot(ah,rand(size(1,10)),rand(size(1,10)))
xlabel(ah,'here is super^{script} to see')
ylabel(ah,'here is sub_{script} to see')
saveas(fh,'test_saveas.eps','epsc')
print(fh,'test_print','-deps')
Kind regards,
Robert
Robert,
Thanks for trying again. I don't know what to say. Only difference I can see is that I don't typically save my figures or axes as variables and refer to them within other commands, but I can't see why that would matter. I'll play around with it a bit more to see if I can pinpoint anything, but for now I'm stumped.

Iniciar sesión para comentar.

Respuestas (2)

Enrico Camporeale
Enrico Camporeale el 12 de Mayo de 2021
Try changing the intepreter to latex and put the symbol with super/subscripts within $$

1 comentario

I mentioned above that the problem is "solved" with Latex as the interpreter, but then the font changes and there is no Latex font that matches the default font and I want the default font.

Iniciar sesión para comentar.

Andrea Bonfanti
Andrea Bonfanti el 30 de Jul. de 2021
Editada: Andrea Bonfanti el 30 de Jul. de 2021
Dear Daniel,
I'm using Octave 4.2.2 for Linux and have faced the same issue, but apparently just from time to time, hence the difficulty to understand the reason.
Now I've found the bug: If you want to add subscripts/superscripts to a given word (say w_s), then every word before w_s must have a subscript/superscript as well to avoid the extra blank in w_s. Looking at your example, a workaround to fix the issue is:
ylabel('PSD_{} [m^2/s]','fontsize',14)
where you create a fictitious subscript to PSD.
Hope this helps,
Andrea

1 comentario

Here are my results. I don't think this fixed it. If anything, I think I see more space after "here" and "is" because of the added "_{}" on each. Glad it works in Octave. In the end, I just fixed them all in Inkscape.
Fixed?
On screen:

Iniciar sesión para comentar.

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Preguntada:

el 25 de Mzo. de 2021

Comentada:

el 30 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by