[NTG-context] pretty typesetting for matlab files
Aditya Mahajan
adityam at umich.edu
Wed Mar 15 07:50:17 CET 2006
Hi,
I was trying to define a pretty printing environment for matlab
files. The basic requirements that I have are
% Numbers should be highlighted in a different color
% Identify floating point numbers 1.1 and 1e-3.
% Identify brackets
% ()[]{}
% Identify Punctuation
% ,;<=> ...
% Identify Operators
% ^ + - / * .^ .*
% Idenfity strings (enclosed in ' ' and " ")
% Identify lines starting with % as comments. Show them
% a different color. Identify %{ ... %} blocks as comments
% identifiers
function return case if else elseif do for while end
% variables
gt,lt,gt,lt,amp,abs,acos,acosh,acot,acoth,acsc,acsch,
[snip].... a huge list
The syntax is similar to both perl and MP. I had a look at the
verb-*.tex files and tried to write verb-matlab. My attempt is
attached.
However, it does not work correctly. There are somethings that I could
not figure out.
1. How to change the comment identifier. Matlab uses % as line
comments and %{ .... %} as block comments.
2. For some reason Matlab variables are not recognized.
3. The punctuations are not identified.
4. '...' is not recognized as a string. The ' is output as a right
quotation mark and not _neutral_ '.
What would be a good way to include tex commands in the comments?
Matlab recognizes %% as a special comment (begining of a cell). Is it
possible to somehow translate %% into \MATLABcell, which is \bf by
default but the behaviour can be changed if desired?
See attachment verb-matlab.tex for my attempt and test.tex for an
example.
Thanks
Aditya
--
Aditya Mahajan, EECS Systems, University of Michigan
http://www.eecs.umich.edu/~adityam || Ph: 7342624008
-------------- next part --------------
\ifx\undefined\setupprettyPLtype \input verb-pl \relax \fi
\unprotect
\gdef\MATLABsetspecials%
{\PLsetspecials}
\gdef\setupprettyMATLABtype%
{\def\prettyidentifier{MATLAB}%
\let\PLidentifiers=\MATLABidentifiers
\let\PLvariables=\MATLABvariables
\PLsetvariables
\PLsetcontrols
\PLsethandlers
\MATLABsetspecials
\PLsetdiagnostics}
\useprettyidentifiers \MATLABidentifiers \MATLABsetspecials
function return case if else elseif do for while end
\useprettyidentifiers \MATLABvariables \MATLABsetspecials
gt,lt,gt,lt,amp,abs,acos,acosh,acot,acoth,acsc,acsch,
all,angle,ans,any,asec,asech,asin,asinh,atan,atan2,atanh,auread,
auwrite,axes,axis,balance,bar,bessel,besselk,bessely,beta,
betainc,betaln,blanks,bone,break,brighten,capture,cart2pol,
cart2sph,caxis,cd,cdf2rdf,cedit,ceil,chol,cla,clabel,clc,clear,
clf,clock,close,colmmd,Colon,colorbar,colormap,ColorSpec,colperm,
comet,comet3,compan,compass,computer,cond,condest,conj,contour,
contour3,contourc,contrast,conv,conv2,cool,copper,corrcoef,cos,
cosh,cot,coth,cov,cplxpair,cputime,cross,csc,csch,csvread,
csvwrite,cumprod,cumsum,cylinder,date,dbclear,dbcont,dbdown,
dbquit,dbstack,dbstatus,dbstep,dbstop,dbtype,dbup,ddeadv,ddeexec,
ddeinit,ddepoke,ddereq,ddeterm,ddeunadv,deblank,dec2hex,deconv,
del2,delete,demo,det,diag,diary,diff,diffuse,dir,disp,dlmread,
dlmwrite,dmperm,dot,drawnow,echo,eig,ellipj,ellipke,
engClose,engEvalString,engGetFull,engGetMatrix,engOpen,
engOutputBuffer,engPutFull,engPutMatrix,engSetEvalCallback,
engSetEvalTimeout,engWinInit,eps,erf,erfc,erfcx,erfinv,error,
errorbar,etime,etree,eval,exist,exp,expint,expm,expo,eye,fclose,
feather,feof,ferror,feval,fft,fft2,fftshift,fgetl,fgets,figure,
fill,fill3,filter,filter2,find,findstr,finite,fix,flag,fliplr,
flipud,floor,flops,fmin,fmins,fopen,format,fplot,fprintf,
fread,frewind,fscanf,fseek,ftell,full,funm,fwrite,fzero,
gallery,gamma,gammainc,gammaln,gca,gcd,gcf,gco,get,getenv,
getframe,ginput,global,gplot,gradient,gray,graymon,grid,griddata,
gtext,hadamard,hankel,help,hess,hex2dec,hex2num,hidden,hilb,hist,
hold,home,hostid,hot,hsv,hsv2rgb,ifft,ifft2,imag,image,
imagesc,Inf,info,input,int2str,interp1,interp2,interpft,inv,
invhilb,isempty,isglobal,ishold,isieee,isinf,isletter,isnan,
isreal,isspace,issparse,isstr,jet,keyboard,kron,lasterr,lcm,
legend,legendre,length,lin2mu,line,linspace,load,log,log10,log2,
loglog,logm,logspace,lookfor,lower,ls,lscov,lu,magic,matClose,
matDeleteMatrix,matGetDir,matGetFp,matGetFull,matGetMatrix,
matGetNextMatrix,matGetString,matlabrc,matlabroot,matOpen,
matPutFull,matPutMatrix,matPutString,max,mean,median,menu,mesh,
meshc,meshgrid,meshz,mexAtExit,mexCallMATLAB,mexdebug,
mexErrMsgTxt,mexEvalString,mexFunction,mexGetFull,mexGetMatrix,
mexGetMatrixPtr,mexPrintf,mexPutFull,mexPutMatrix,mexSetTrapFlag,
min,more,movie,moviein,mu2lin,mxCalloc,mxCopyCharacterToPtr,
mxCopyComplex16ToPtr,mxCopyInteger4ToPtr,mxCopyPtrToCharacter,
mxCopyPtrToComplex16,mxCopyPtrToInteger4,mxCopyPtrToReal8,
mxCopyReal8ToPtr,mxCreateFull,mxCreateSparse,mxCreateString,
mxFree,mxFreeMatrix,mxGetIr,mxGetJc,mxGetM,mxGetN,mxGetName,
mxGetNzmax,mxGetPi,mxGetPr,mxGetScalar,mxGetString,mxIsComplex,
mxIsFull,mxIsNumeric,mxIsSparse,mxIsString,mxIsTypeDouble,
mxSetIr,mxSetJc,mxSetM,mxSetN,mxSetName,mxSetNzmax,mxSetPi,
mxSetPr,NaN,nargchk,nargin,nargout,newplot,nextpow2,nnls,nnz,
nonzeros,norm,normest,null,num2str,nzmax,ode23,ode45,orient,orth,
pack,pascal,patch,path,pause,pcolor,pi,pink,pinv,plot,plot3,
pol2cart,polar,poly,polyder,polyeig,polyfit,polyval,polyvalm,
pow2,print,printopt,prism,prod,pwd,qr,qrdelete,qrinsert,quad,
quad8,quit,quiver,qz,rand,randn,randperm,rank,rat,rats,rbbox,
rcond,real,realmax,realmin,refresh,rem,reset,reshape,residue,
rgb2hsv,rgbplot,rootobject,roots,rose,rosser,rot90,rotate,
round,rref,rrefmovie,rsf2csf,save,saxis,schur,sec,sech,semilogx,
semilogy,set,setstr,shading,sign,sin,sinh,size,slice,sort,sound,
spalloc,sparse,spaugment,spconvert,spdiags,specular,speye,spfun,
sph2cart,sphere,spinmap,spline,spones,spparms,sprandn,sprandsym,
sprank,sprintf,spy,sqrt,sqrtm,sscanf,stairs,startup,std,stem,
str2mat,str2num,strcmp,strings,strrep,strtok,subplot,subscribe,
subspace,sum,surf,surface,surfc,surfl,surfnorm,svd,symbfact,
symmmd,symrcm,tan,tanh,tempdir,tempname,terminal,text,tic,title,
toc,toeplitz,trace,trapz,tril,triu,type,uicontrol,uigetfile,
uimenu,uiputfile,unix,unwrap,upper,vander,ver,version,view,
viewmtx,waitforbuttonpress,waterfall,wavread,wavwrite,what,
whatsnew,which,white,whitebg,who,whos,wilkinson,wk1read,
wk1write,xlabel,xor,ylabel,zeros,zlabel,zoom
\installprettytype [MATLAB] [MATLAB]
\definetyping [MATLAB] [\c!option=MATLAB]
\definepalet [MATLABcolorpretty] [colorpretty]
\definepalet [MATLABgraypretty] [graypretty]
\protect \endinput
-------------- next part --------------
\setupcolors[state=start]
\input verb-matlab
\starttext
\startMATLAB
n = 150e5 ;
p = 0.9 ;
d = 300 ;
r = 100 ;
m_total = n:(n+40) ;
warning off;
%%. Refundable Tickets
cost = zeros(length(m_total),1) ;
for m = m_total
for k = 0:n
cost(m-n+1) = cost(m-n+1) + k*d*nchoosek(m,k)*p^k*(1-p)^(m - k) ;
end;
for k = (n+1):m
cost(m-n+1) = cost(m-n+1) + (n*d - (k-n)*r)*nchoosek(m,k)*p^k*(1-p)^(m-k) ;
end;
end;
[value,count] = max(cost) ;
disp(sprintf('The maximum occurs at %d',count+n-1)) ;
figure(1);
plot(m_total,cost) ;
%%. Nonrefundable Case
cost = zeros(length(m_total),1) ;
for m = m_total
for k = 0:n
cost(m-n+1) = cost(m-n+1) + k*d*nchoosek(m,k)*p^k*(1-p)^(m - k) ;
end;
for k = (n+1):m
cost(m-n+1) = cost(m-n+1) + (m*d - (k-n)*(d+r))*nchoosek(m,k)*p^k*(1-p)^(m-k) ;
end;
end;
[value,count] = max(cost) ;
disp(sprintf('The maximum occurs at %d',count+n-1)) ;
figure(2);
plot(m_total,cost) ;
\stopMATLAB
\stoptext
More information about the ntg-context
mailing list