用listings包插入matlab代码,报错:代码中某一行未加$

2020-12-25 20:56发布

## 编译环境 操作系统 * [x]` Windows 7/8/10 * [ ] macOS * [ ] Linux `若需勾选,请把[ ]改成[x]` Tex发行版 * [x ]` TexLive...

## 编译环境 操作系统 * [x]` Windows 7/8/10 * [ ] macOS * [ ] Linux `若需勾选,请把[ ]改成[x]` Tex发行版 * [x ]` TexLive `2017` * [ ] MikTeX `版本号` * [ ] CTeX `若需勾选,请把[ ]改成[x]` ## 我的问题 在此输入你遇到的问题...用listings包插入matlab代码,代码中有一行是N=NH^2,报错,这个表示式未加$ ``` \documentclass{ctexart} \usepackage{xeCJK} \usepackage{listings} \usepackage{xcolor} \definecolor{mygreen}{rgb}{0,0.6,0} \definecolor{mygray}{rgb}{0.5,0.6,0.5} \definecolor{mymauve}{rgb}{0.58,0,0.82} \lstset{ backgroundcolor=\color{white}, basicstyle=\ttfamily, columns=fullflexible, tabsize=4, breaklines=true, captionpos=b, commentstyle=\color{mygreem}, escapeinside={\%*}{*)}, keywordstyle=\color{blue}, stringstyle=\color{mymauve}\ttfamily, frame=lines, rulesepcolor=\color{red!20!green!20!blue!20}, } %\lstset{extendedchars=true} \setmainfont{Times New Roman} \pagestyle{plain} \title{RCWA} \begin{document} \maketitle 严格耦合波程序 \lstinputlisting[language=Matlab]{RCWA.m} \end{document} ``` m文件的代码是这样的: ``` %************************************************* % Simulation Parameters %************************************************* close all; clc; clear all; %**************** % RCWA Parameters %**************** NH = 3; %number of spatial harmonics for x, y if(mod(NH,2) == 0) error('Number of Harmonics must be odd'); end N = NH^2; %size of matrices %******************* % Read Data files %******************* [grid] = setupGrid('./device/Triangle/Grid.dat'); src = setupSrc('src.mat',N); device.ERC(N,N,length(grid.L)) = 0; device.URC(N,N,length(grid.L)) = 0; for layer = 1:length(grid.L) fn1 = './device/Triangle/'; [device] = setupDevice(fn1,grid,NH,device,layer); end %******************************* % Calculate Kx, Ky, Kz Matrices %******************************* [Kx, Ky, KzT, KzR] = calcK(src,grid,NH); %****************** % Calc Eigenmodes %****************** [W0,V0,SG] = calcFreeSpace(Kx,Ky,N); [Wref, Sref] = calcReflectionSide(Kx,Ky,KzR,N,W0,V0,grid); [Wtrn, Strn] = calcTransmissionSide(Kx,Ky,KzT,N,W0,V0,grid); %Initialize Global Scattering Matrix SG.S11 = zeros(2*N,2*N); SG.S12 = eye(2*N); SG.S21 = eye(2*N); SG.S22 = zeros(2*N,2*N); %******************************** % Calc Global Scattering Matrix %******************************** for layer = 1:length(grid.L) Si = calcLayer(Kx,Ky,N,W0,V0,grid,device,layer); SG = star(SG,Si); end SG = star(Sref,SG); SG = star(SG,Strn); % % %********************** % % Post-Processing % %********************** % %Calculate Source Mode Coefficients csrc = inv(Wref) * src.esrc; %********************** % REFLECTED FIELDS %********************** eref = Wref * SG.S11 * csrc; rx = eref(1:N); ry = eref(N+1:end); rz = - inv(KzR) * (Kx*rx + Ky*ry); R = abs(rx).^2 + abs(ry).^2 + abs(rz).^2; Gamma = rx + ry + rz; kzInc = cos(src.theta)*sqrt(grid.erR * grid.urR); R = real(-KzR / kzInc) * R; Gamma = reshape(Gamma,[NH,NH]); REF = norm(sum(sum(R))); %*********************** % TRANSMITTED FIELDS %*********************** etrn = Wtrn * SG.S21 * csrc; tx = etrn(1:N); ty = etrn(N+1:end); tz = -inv(KzT) * (Kx*tx + Ky*ty); Tau = tx + ty + tz; T = abs(tx).^2 + abs(ty).^2 + abs(tz).^2; T = real((grid.urR / grid.urT) * KzT/kzInc) * T; Tau = reshape(Tau,[NH,NH]); TRN = norm(sum(sum(T))); %Gamma + Tau %Plotting Sx = reshape(tx,[NH,NH]); normFactor = sum(src.esrc); Ex = zeros(grid.Nx,grid.Ny); ErTest = zeros(grid.Nx,grid.Ny); ERCtest = device.ERC(:,:,1); Me = (-(NH-1)/2):(NH-1)/2; Ne = (-(NH-1)/2):(NH-1)/2; for x = 1:grid.Nx for y = 1:grid.Ny for m = 1:NH for n = 1:NH kx = 2*pi*Me(m) / grid.Lx; ky = 2*pi*Ne(n) / grid.Ly; xr = x*grid.dx; yr = y*grid.dy; Ex(x,y) = Ex(x,y) + Sx(m,n)*exp(-1j*(kx*xr + ky*yr)); end end end end %imagesc(real(Ex) / normFactor) %colorbar; ```
3条回答
阳光下歇斯底里的轻笑
2020-12-26 14:38 .采纳回答
排版matlab代码,建议使用`mcode`宏包。 ```tex \documentclass{ctexart} \usepackage{xeCJK} \usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}%matlab代码环境 \usepackage{listings}%代码环境 \begin{document} \begin{lstlisting}[language=Matlab] close all;clc; %% 3维方向图 %建立网格,变量为theta和phi p=0:pi/100:2*pi; t=0:pi/100:pi; [phi,theta]=meshgrid(p,t); % 天线阵的3维函数图 r1=sin(theta); x=r1.*sin(theta).*cos(phi); y=r1.*sin(theta).*sin(phi); z=r1.*cos(theta); figure; surf(x,y,z) % 偶极子的3维方向图 \end{lstlisting} \end{document} ``` 效果图![](https://wenda.latexstudio.net/data/attach/201226/FBTZ6zgt.png)

作者追问:2020-12-26 14:38

非常感谢,不过我还是很奇怪为什么会出错,N=NH^2是一句非常普通的语句啊,在其他m文件里也出过类似的语句,都没报错

一周热门 更多>

相关问答