Octave/Windows: umlauts in plots displayed but not saved as image - matlab

I use octave 3.8.2 (with gnuplot) under Windows. I want to write "special character" in the axis labels of plots. The umlaut 'ä' and special character µ are displayed in the figure but not saved to the image file using print. Partly, I can use the TeX command: '\mu' instead of 'µ' but for umlauts '\"a' instead of 'ä' does not work.
plot(1:10);
%set (findall (gcf (), "-property", "interpreter"), "interpreter", "TeX") % does not work
xlabel('Länge in µm');
ylabel('Breite in \mum');
print('umlaute.jpg', '-djpeg');

graphics_toolkit("gnuplot") and pngcairo or pdfcairo produce a better output.
graphics_toolkit("gnuplot")
plot(1:10)
xlabel('Länge in µm')
ylabel('Breite in \mum')
print('umlaute.png', '-dpngcairo') # or
# print('umlaute.pdf', '-dpdfcairo')
With octave 3.8.2 under linux, the ouput is

Using Octave 4.4.0 on Windows the bug seems to be fixed as least for graphics_toolkits gnuplot and qt with the print option '-dpngcairo'. So this script gives me a fine output in the file umlaute4-cairo.png .
% graphics_toolkit("gnuplot")
graphics_toolkit("qt")
% Do a simple plot with a German umlaut and a Greek micron signs
plot(1:10);
ylabel('Breite in \mum')
xlabel('Länge in \mum')
print('umlaute4.jpg', '-djpg') # not OK, bad umlauts
print('umlaute4.png', '-dpng') # not OK, bad umlauts
print('umlaute4-cairo.png', '-dpngcairo') # OK with gnuplot and qt

Using Octave 5.1 on Windows with the qt graphics toolkit and latin1-coding it does now finally work!
graphics_toolkit("qt");
x=1:10;
plot(x, x);
title('Ä Ö Ü');
xlabel('H_2 in µm');
ylabel('Percent in %');
[![enter image description here][1]][1]print("test_umlaute.png");
Other combiations like UTF8 and and other graphics toolkit do not work.

There is a problem in Octave 5.1 that the selected encoding for .m files is not restored on startup.
You can either change the encoding in the settings and call "clear functions" to trigger the .m files being parsed again. Or you could call the internal function "mfile_encoding". For UTF-8 encoding that would be:
__mfile_encoding__ ("utf-8");
clear functions
https://savannah.gnu.org/bugs/?56782

Related

How can I print a figure to a pdf file with embedded greek letters or formulas in MATLAB?

I cannot find a solution to print a figure with embedded Greek letters using the print function (or the File Exchange function export_fig() of Yair Altman).
While this functionality was available in earlier matlab releases (e.g. R2016a), the following code does not produce the desired result with version R2018a:
figure()
rng = 0:0.01:2;
plot(rng, sin(rng.*pi()))
text(1, 0.6, 'sin of {\alpha}')
print('simple_test_p', '-dpdf')
% export_fig('simple_test_e','-pdf', '-transparent')
While the text before '{\alpha}' is embedded in the resulting pdf file, alpha itself appears to be an image rather than an embedded font (see image).
I tried several different fonts to exclude the possibility of a missing default font (besides double-checking the font folder). Moreover, I used both latex and tex interpreters. Nevertheless, none of these procedures worked.
If this problem cannot be solved with Matlab, is there any other way to get a pdf file with embedded Greek letters and mathematical formulas?
I very much appreciate your help. Thank you in advance.
MATLAB uses Unicode text everywhere. You should be able to simply do:
text(1, 0.6, 'sin of 𝛼')
If you can't type the Greek letters, you can always search for them on Google* and copy-paste them. I found the above here.
*or whatever your favorite search engine is.
Or just use matplotlib, which gets along well with unicode:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10000)
y = np.sin(np.pi * x * 0.001)
plt.plot(x, y)
plt.ylabel(u"\u03B1")
plt.xlabel('β')
plt.savefig(r'<insert your path here>/sinewave.pdf')
plt.show()

How does matlab's latex interpreter handle unicode?

I'm wondering how MATLABs latex interpreter for plot text deals with unicode characters? It is weirdly inconsistent. Which, y'know, invalidates the whole ENTIRE point of unicode.
TOY CODE
%*** Setup some text for a plot title
Title_Txt{1} = [char(8734) ,' SNR~~~' , char(10) , '(-)'];
Title_Txt{2} = ['50 SNR~~~' , char(10) , '(-)'];
%*** Plots!
x= 1:1:10
y= rand(size(x))
figure(1)
subplot(211)
plot(x,y)
title(Title_Txt{1} , 'interpreter' , 'latex')
subplot(212)
plot(x,y)
title(Title_Txt{2} , 'interpreter' , 'latex')
Toy code demonstrates that the latex interpreter handles char(10) --- a new line. But it breaks from char(8734) --- the infinity symbol.
Obviously I can work around this by feeding in a latex symbol that matlab knows (another source of frustration but that's for a different discussion), but I am curious about
what MATLAB is doing under the hood here?
is there is a fix for getting unicode into latex?
I suspect the (unsatisfying) answer here is that the Latex interpreter portion of Matlab does whatever the included version of Latex does, and Latex in general doesn't support Unicode. (For Latex solutions, see: https://tex.stackexchange.com/questions/34604/entering-unicode-characters-in-latex. Of course this doesn't help Matlab users.)
As to why Latex doesn't support Unicode. I'll note that the first copyright date on my Latex users' guide is 1985, and the latest release is version 2e, from 1994. Unicode was not really mainstream until the '90's.
(This is a poor answer, but became too long for a comment.)

Latex changes fonts in Matlab produced eps files

In short:
I produce a figure in matlab (2013a). The problem lies with the "fontname" command. Example code is
plot([1:10],[1:10])
set(gca, 'XTick',[0,4,8])
set(gca, 'XTicklabel',{'p/2','3p/2','2p'},'fontname','symbol');
Prints it with print -despc "filename".
Include it in a latex file with includegraphics command and epstopdf
The fonts in the figure are distorted, while they are intact the eps file itself. For example, in the latex file, the pi labels are turned into not-equal signs.
Do you know why does this happen?
Remark: I have to use "fontname" command because, at least for Matlab 2013a, latex interpreter can not be applied for the XTickLabel.

Surface plot with LaTeX code in label

I would like to use the Letter μ (LaTeX: \mu) in my surface plot. Unfortunately when I use the LaTeX symbol \mu in the zlabel('Power [\muW]'); command it results in:
'Power [ μ W]' instead of 'Power [μW]'
How can I avoid these spaces around μ?
I can't reproduce your problem, which could be due to you using an older version of Matlab. In the past it was neccesary to set the latex interpreter fist, before using Latex syntax. So the following should work:
zlabel('Power [$$\mu$$W]','interpreter','latex');
Nowadays it seems to recognize automatically at least greek letters.

Print to eps on Win produces Unix line endings (Bug?)

On Win7 64b with R2014b, printing a figure to .eps produces Unix line endings (LF or \n). Is this expected or a bug?
Also, does it depend on Matlab version?
You can test with:
% Plot and export to .eps
plot(1:10)
print(gcf,'test','-depsc')
fid = fopen('test.eps');
% Check if Unix LF only
line = fgets(fid);
if all(line(end-1:end) == sprintf('\r\n'))
disp('CRLF')
elseif line(end) == sprintf('\n')
disp('LF only!')
end
% Clean up
fclose(fid);
delete('test.eps')
Edit
Why it matters to me? I am exporting figures to .eps and compiling them with a Miktex 2.9 distribution through epstopdf. However, I get a blank figure and the problem is explained here https://tex.stackexchange.com/questions/208179/epstopdf-error-undefined-in-uagelevel
Now, I was wondering why I never had this issue before. It seems from Louis's answer, it was introduced with the new release and Unix like line endings in the .eps
First off: according to the EPS format specification (large file obtained from here):
Page 26:
The characters carriage return (CR) and line feed (LF) are also called newline
characters. The combination of a carriage return followed immediately by a line
feed is treated as one newline.
Page 74:
The PostScript language scanner and the readline operator recognize all three external
forms of end-of-line (EOL)—CR alone, LF alone, and the CR-LF pair—
and treat them uniformly, translating them as described below.
So both forms could actually be used.
I think your test is wrong. You separate lines with textscan according to '\n'; but that doesn't exclude '\r\n' (you will split lines according to '\n', and '\r' will remain as the last character of each line).
I suggest you test this way:
fid = fopen('test.eps');
s = fread(fid); %// read whole file as a vector of ASCII codes
LF = find(a==10); %// find locations of LF
CR = find(a==13); %// find locations of CR
if all(ismember(LF-1,CR)) %// test if every LF is preceded by a CR
disp('Every LF is preceded by a CR')
end
On my Windows system (Matlab 2010b, Windows Vista 32 bits) it turns out that indeed every LF is preceded by a CR, so Windows-like line endings are used.