Latex changes fonts in Matlab produced eps files - matlab

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.

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?

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.

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

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

Text and Plots in Matlab to LaTeX

I like to create a "report generation" script in Matlab.
Suppose we have a Matlab array, data and we want to export the following to a .tex file:
"The information in the first element of data is X." This would be followed by a plot of X.
I have already tried help latex in Matlab and aware of the various packages on Matlab file exchange. However I have seen nothing so far that will allow me to export both text and plots in the same Matlab script to a .tex file.
The publish function may work for you.
Create this script, foo.m:
%%
% The information in the first element of data is X.
plot(X)
And publish it to LaTeX:
>> publish foo latex
You might want to take a look at this article published in TUGboat (the official magazine of the TeX Users Group):
http://www.tug.org/TUGboat/Articles/tb24-2/tb77seta.pdf
Generating LaTeX documents through Matlab (S. E. Talole and S. B. Phadke)
Good luck!
Are you aware of matlab2tikz? i've used it extensively for my PhD-Thesis, albeit only for exporting single plots. But I guess it should be easily possible to whip something up that combines the power of MATLABs LaTeX export capabilities.
Exporting figures from Matlab to a .tex file is just a matter of exporting the figure to an appropriate format and then including the figure file in the .tex file. Would something like the code listed below work for your needs?
Using LaTeX to generate dvi:
% Include code to write text to .tex file (fid is assumed to be the file id of the .tex file)
print(gcf,'figure1','-depsc','-r100');
fprintf(fid,'\includegraphics[width=4in]{figure1.eps}\n');
Using pdfTeX to generate pdf:
% Include code to write text to .tex file (fid is assumed to be the file id of the .tex file)
print(gcf,'figure1','-djpg','-r100');
fprintf(fid,'\\includegraphics[width=4in]{figure1.jpg}\n');