How does matlab's latex interpreter handle unicode? - matlab

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.)

Related

How can I write with latex character in xlabel in matlab

I would write my xlabel with Latec character so I used this code
x = -10:0.1:10;
y = [sin(x); cos(x)];
plot(x,y)
xlabel('$\mathbb{x}$','Interpreter','latex')
but I have this warning message
Warning: Error updating Text.
String scalar or character vector must have valid interpreter syntax:
$\mathbb{x}$
and the xlabel appear like this
https://i.stack.imgur.com/NCI4n.png
please how I can fix this problem.
The problem with your example is the mathbb command, which is not in base Latex. To show this, replace the \mathbb with, e.g., \mathrm. This will work.
I've never seen an example of adding Latex packages into the Matlab environment. (Update below)
You can have a look at this question, which includes a very aggressive potential way to add the package you need. But it's not clear to me if the solution proposed is actually functional.
How do you use the LaTeX blackboard font in MATLAB?

Endash for minus sign instead of hyphen for matlab

So here's my code:
set(groot, 'defaultAxesTickLabelInterpreter', 'latex') %For axes;
ax = gca;
yticklabels(ax, strrep(yticklabels(ax),'--','–'));
set(ax,'ticklabelinterpreter','tex') %or 'tex' but not 'latex'
figure(1)
t= [0:0.01:2*pi];
x = sin(t);
y = cos(t)
plot(t, x, t, y)
Output:
I tried the solution here, but the hyphens still remain there. I want the en-dash to appear because it's the standard sign for the negative sign. What is the correct way of getting an en-dash to appear instead of the hyphen?
This post at MATLAB Answers explains how to set the (default) interpreter for the axes' labels.
set(groot,'defaultAxesTickLabelInterpreter','latex');
You need to call this before plotting.
Having this set, the tick-labels will be interpreted as LaTeX code. Here is a comparison. The last two examples includes #XiangruiLi's answer (the next code snippets must be called after the plot was created):
yticklabels(gca, strrep(yticklabels(gca),'-','--'));
yticklabels(gca, strrep(yticklabels(gca),'-','$-$'));
none:
latex:
latex + strrep(...,'-','--')):
latex + strrep(...,'-','$-$')):
While the last is probably what you wanted, note that this is certainly not the representation MATLAB intended. It is therefore the question if you really need/want to go through this fuzz.
It seems to me you mis-used strrep. This worked for me:
yticklabels(ax, strrep(yticklabels(ax),'-','--'));
Using the actual Unicode minus character should also work (also in Octave):
yticklabels(gca, strrep(yticklabels(gca),'-','−'));
In this case, there is no need to set the interpreter to LaTeX.

Create Matlab figures in LaTeX using matlabfrag or alternatives

I want to include Matlab figures into latex preferably with vectorised formats but with LaTeX MATLAB fonts for all text. I have been using the MATLAB function matlab2tikz which worked perfect for simple figures, but now my figures have too many data points which cause an error. So matlab2tikz is not suitable.
I've been looking at matlabfrag which I think will accomplish what I want, but when I run the script in LaTeX as detailed by the user guide it has an error File not found.
This is my code:
\documentclass{article}
\usepackage{pstool}
\begin{document}
\psfragfig{FileName}
\end{document}
Where FileName is the name of the .eps and .tex that matlabfrag creates. Has anyone come across this problem? Or recommend any other functions/methods to use?
I'm using Texmaker on Windows 7.
My advice would be to rethink your workflow.
Instead of reusing your Matlab code to plot figures and be dissappointed by ever changing outputs with matlab2tikz, start reusing your latex code to plot figures and don't bother about plotting in Matlab anymore (at least not for beautiful plots).
matlab2tikz is just generating latex code based on the latex-package pgfplots. To understand the working of this package is pretty easy, as it is intended to be similar to Matlab.
So why bother and always let matlab2tikz do the work for you? Because again and again you won't be entirely happy with the results. Just try to write the pgfplots-code from scratch and just load the data from Matlab.
Here is a convenient function I wrote to create latex-ready text files:
function output = saveData( filename, header, varargin )
in = varargin;
numCols = numel(in);
if all(cellfun(#isvector, in))
maxLength = max(cellfun(#numel, in));
output = cell2mat(cellfun(#(x) [x(:); NaN(maxLength - numel(x) + 1,1)],in,'uni',0));
fid = fopen(filename, 'w');
fprintf(fid, [repmat('%s\t',1,numCols),'\r\n'], header{:});
fclose(fid);
dlmwrite(filename,output,'-append','delimiter','\t','precision','%.6f','newline', 'pc');
else
disp('saveData: only vector inputs allowed')
end
end
Which could for example look like the following, in case of a bode diagram:
w G0_mag G0_phase GF_mag GF_phase
10.000000 40.865743 -169.818991 0.077716 -0.092491
10.309866 40.345290 -169.511901 0.082456 -0.101188
10.629333 39.825421 -169.196073 0.087474 -0.110690
10.958700 39.306171 -168.871307 0.092787 -0.121071
11.298273 38.787575 -168.537404 0.098411 -0.132411
In your tikzpicture you can then just load that file by
\pgfplotstableread[skip first n=1]{mydata.txt}\mydata
and store the table into the variable \mydata.
Now check pfgplots how to plot your data. You will find the basic plot command \addplot
\addplot table [x expr= \thisrowno{0}, y expr= \thisrowno{3} ] from \mydata;
where you directly access the columns of your text file by \thisrowno{0} (confusing, I know).
Regarding your problem with to many data points: pgfplots offers the key each nth point={ ... } to speed things up. But I'd rather filter/decimate the data already in Matlab.
The other way around is also possible, if you have to few data points the key smooth smoothes things up.

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.

matlab's phi symbol

Not that significant, but annoying to no end. Why does matlab has no small phi (\varphi) symbol ? It has pretty much all other symbols LaTeX offers, but not this one. Why ?
I may be wrong of course, in which case would be delighted if someone could prove me wrong...
The default interpreter is TeX actually, not LaTeX, which is why you're having this problem. You can use LaTeX as the interpreter for a given part doing something like this:
plot(1);
hl = legend('$$\varphi$$');
set(hl,'Interpreter','latex')
or you can set LaTeX as your default interpretor using
set(0,'DefaultTextInterpreter', 'latex');
which can be put in your startup.m file if you like.
Matlab uses TeX as default. Often, it is possible to switch to LaTeX, but in some cases (dialog boxes), this is impossible.
%# here's an example with all three phis
plot(rand(3))
yh = get(gca,'YLabel');
set(yh,'Interpreter','latex','string','$\varphi$ $\phi$ $\Phi$')