在Matlab中使用Latex


Matlab强大的计算和处理数据的能力与Latex无与伦比的的排版功能结合在一起是必然的结果。

 

(关于Latex的用法可参考相关书籍)

 

为方便利用LaTeX的命令,需要在把Matlab图形中title、xlabel、ylabel、zlabel、textbox和

legend等的Interpreter属性设定为latex。首先,在Matlab中使用LaTeX语言的格式有三种:
1、\( LaTeX命令\)
2、$ LaTeX命令$
3、$$ LaTeX命令$$

例1:

画个正弦曲线,里面写个多体薛定谔方程的哈密顿量!

 

clear;

plot(0:pi/20:2*pi,sin(0:pi/20:2*pi))
title([‘Sine Wave and  Schr$\ddot{o}$dinger equation’],’interpreter’,’latex’)
xlabel([‘x’],’interpreter’,’latex’)
ylabel([‘y’],’interpreter’,’latex’)
text(‘Interpreter’,’latex’,…
‘String’,’$$H=(-\sum_{n=1}^N \frac{\hbar^2}{2M_n}\frac{\partial^2}{\partial R_n^2} \sum_{i=1}^I \frac{h^2}{2m}\frac{\partial^2}{\partial r_i^2}+ \ldots$$’ ,…
‘Position’,[.4 .5],…
‘FontSize’,16)

text(‘Interpreter’,’latex’,…
‘String’,’$$\frac{1}{2}\sum_{i=1}^I\sum_{j=1}^I \frac{e^2} {|r_{ij}|}+\sum_{n=1}^N \sum_{i=1}^I \frac{Z_ne^2} {|r_{ni}|}+\ldots$$’ ,…
‘Position’,[1.2 .0],…
‘FontSize’,16)
text(‘Interpreter’,’latex’,…
‘String’,’$$\frac{1}{2}\sum_{l=1}^N \sum_{n=1}^N \frac{Z_nZ_le^2} {|R_{nl}|})$$’ ,…
‘Position’,[1.2 -.5],…
‘FontSize’,16)

例2:

n=100;
x=linspace(-3,3,n);
y=linspace(-3,3,n);
z=linspace(-3,3,n);
[X,Y,Z]=ndgrid(x,y,z);
F=((-(X.^2) .* (Z.^3) -(9/80).*(Y.^2).*(Z.^3)) + ((X.^2) + (9/4).* (Y.^2) + (Z.^2)-1).^3);
isosurface(F,0)

text(‘Interpreter’,’latex’,…
‘String’,’$$-X^2 Z^3 -\frac{9}{80}Y^2Z^3+ X^2+ \frac{9}{4}Y^2+(Z^2-1)^3=0$$’,…
‘Position’,[0 40 30],…
‘FontSize’,24)

lighting phong
caxis
axis equal
colormap(‘flag’);
view([55 34]);

例3:

各种风骚的希腊字母

 

greeks = …
{‘ALPHA’      ‘A’         ‘\alpha’
‘BETA’       ‘B’         ‘\beta’
‘GAMMA’      ‘\Gamma’    ‘\gamma’
‘DELTA’      ‘\Delta’    ‘\delta’
‘EPSILON’    ‘E’         {‘\epsilon’,’\varepsilon’}
‘ZETA’       ‘Z’         ‘\zeta’
‘ETA’        ‘H’         ‘\eta’
‘THETA’      ‘\Theta’    {‘\theta’,’\vartheta’}
‘IOTA’       ‘I’         ‘\iota’
‘KAPPA’      ‘K’         ‘\kappa’
‘LAMBDA’     ‘\Lambda’   ‘\lambda’
‘MU’         ‘M’         ‘\mu’
‘NU’         ‘N’         ‘\nu’
‘XI’         ‘\Xi’       ‘\xi’
‘OMICRON’    ‘O’         ‘o’
‘PI’         ‘\Pi’       {‘\pi’,’\varpi’}
‘RHO’        ‘P’         {‘\rho’,’\varrho’}
‘SIGMA’      ‘\Sigma’    {‘\sigma’,’\varsigma’}
‘TAU’        ‘T’         ‘\tau’
‘UPSILON’    ‘\Upsilon’  ‘\upsilon’
‘PHI’        ‘\Phi’      {‘\phi’,’\varphi’}
‘CHI’        ‘X’         ‘\chi’
‘PSI’        ‘\Psi’      ‘\psi’
‘OMEGA’      ‘\Omega’    ‘\omega’};
h = figure(‘units’,’pixels’,’pos’,[300,100,620,620],’Color’,’w’);
axes(‘units’,’pixels’,’pos’,[10,10,600,600],’Xcol’,’w’,’Ycol’,’w’,…
‘Xtick’,[],’Ytick’,[],’Xlim’,[0 6],’Ylim’,[0,4]);

for r = 1:4
for c = 1:6
el = (r-1)*6 + c;
% Title
text(c-0.5,5-r,greeks{el,1},’Fonts’,14,’FontN’,’FixedWidth’,…
‘Hor’,’center’,’Ver’,’cap’)
% Color cap latter in grey or black
if strcmp(greeks{el,2}(1),’\’)
clr = [0, 0, 0];
else
clr = [0.65, 0.65, 0.65];
end
% Cap letter
text(c-0.5,4.87-r,[‘$\rm{‘ greeks{el,2} ‘}$’],’Fonts’,40,…
‘Hor’,’center’,’Ver’,’cap’,’Interp’,’Latex’,’Color’,clr)
% Lowercase letter/s (if two variants)
if iscell(greeks{el,3})
text(c-0.75,4.48-r,[‘$’ greeks{el,3}{1} ‘$’],’Fonts’,20,…
‘Hor’,’center’,’Interp’,’Latex’)
text(c-0.25,4.48-r,[‘$’ greeks{el,3}{2} ‘$’],’Fonts’,20,…
‘Hor’,’center’,’Interp’,’Latex’)
% Latex command
text(c-0.5,4.3-r,[‘\’ greeks{el,3}{1}],’Fonts’,12,’FontN’,’FixedWidth’,…
‘Hor’,’center’,’Ver’,’base’)
else
text(c-0.5,4.48-r,[‘$’ greeks{el,3} ‘$’],’Fonts’,20,…
‘Hor’,’center’,’Interp’,’Latex’)
text(c-0.5,4.3-r,[‘\’ greeks{el,3}],’Fonts’,12,’FontN’,’FixedWidth’,…
‘Hor’,’center’,’Ver’,’base’)
end
end
end

例4:

用Matlab写Latex文档,脱了裤子放屁~但从中可以得到一些启示,可以做一些一劳永逸的事情!

编译后:

clear;
TC = [-273.15 -40 0 100]’;
TK = TC + 273.15;
TF = (TC+40)*9/5-40;
TR = TF + 459.67;
FID = fopen(‘daodao.tex’, ‘w’);
fprintf(FID, ‘\\begin{tabular}{|rrrr|}\\hline \n’);
fprintf(FID, ‘T ($^{\\circ}$C) & T (K) & T ($^{\\circ}$F) & T ($^{\\circ}$R)\\\\ \\hline \n’);
for k=1:length(TC)
fprintf(FID, ‘%8.2f & %8.2f & %8.2f & %8.2f \\\\ ‘, TC(k), TK(k), TF(k), TR(k));
if k==length(TC)
fprintf(FID, ‘\\hline ‘);
end
fprintf(FID, ‘\n’);
end

fprintf(FID, ‘\\end{tabular}\n’);
fclose(FID);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s