Latex安装mcode包展示matlab代码

参考网页

LaTeX技巧57:使用mcode宏包在LATEX制作m文件效果

下载地址

http://www.mathworks.com/matlabcentral/fileexchange/8015-m-code-latex-package
需要注册,然后Ubuntu下的火狐浏览器可能没法登陆成功。

安装

下载之后需要安装,我的办法是直接安装到texlive的安装目录下:

sudo cp -r mcode /usr/share/texlive/texmf-dist/tex/latex
sudo cp -r mcode /usr/share/texmf/tex/latex

但是单独安装以上的文件夹似乎没有用,编辑器那边还是不能调用。
然后在命令行跑一个

texhash

在关掉你的编辑器重新开,基本上就可以见效了,我的总是没见效的缘由是没有重新开编辑器,对了,我的编辑器是vscode + LaTeX Workshp

然后就可以参考示例写MATLAB代码了。

官方示例

documentclass{article}


% load package with some of the available options - you may not need this!
usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}

% something NOT relevant to the usage of the package.
usepackage{url,textcomp}
setlength{parindent}{0pt}
setlength{parskip}{18pt}
	itle{	exttt{mcode.sty} Demo}
author{Florian Knorn, 	exttt{florian@knorn.org}}
% //////////////////////////////////////////////////

egin{document}

maketitle

egin{center}
egin{minipage}{.75linewidth}
    color{red}{hfill	extbf{NOTE --- BEFORE YOU START}hfillstrut}

    All that this package does is to configure the verb|listings| package for you. If anything is not working the way you want it, refer to the verb|listings| documentation first and / or take a look at the verb|mcode.sty| file itself, which is well documented internally.
    
    The verb|listings| documentation can be accessed either by typing verb|texdoc listings| into a command prompt on your system, or online:scriptsizeurl{http://mirrors.ctan.org/macros/latex/contrib/listings/listings.pdf}
    
end{minipage}
end{center}

section*{Installation of the package}

As with any other small package, just place the verb|mcode.sty| file in the same folder as your document, or put it somewhere where LaTeX{} can find it.  Done!

medskip

section*{Usage --- 3 ways}

1) This inline demo mcode{for i=1:3, disp( cool ); end;} uses the verb|mcode{}| command.footnote{Works also in footnotes: mcodefn{for i=1:3, disp( cool ); end;}}

2) The following is a block using the verb|lstlisting| environment.
egin{lstlisting}
for i = 1:3
    if i >= 5 && a ~= b       % literate programming replacement
        disp( cool );           % comment with some §mcommentfontLaTeX in it: $mcommentfontpi x^2$§
    end
    [:,ind] = max(vec);
    x_last = x(1,end) - 1;
    v(end);
    really really long really really long really really long really really long really really long line % blaaaaaaaa
    ylabel( Voltage (µV) );
end
end{lstlisting}
Note: Here, the package was loaded with the verb|framed|, verb|numbered|, verb|autolinebreaks| and verb|useliterate| options.  	extbf{Please see the top of mcode.sty for a detailed explanation of these options.}


3) Finally, you can also directly include an external m-file from somewhere on your hard drive (the very code you use in 	extsc{Matlab}, if you want) using the verb|lstinputlisting{/SOME/PATH/FILENAME.M}| command.  If you only want to include certain lines from that file (for instance to skip a header), you can use verb|lstinputlisting[firstline=6, lastline=15]{/SOME/PATH/FILENAME.M}|.

section*{FAQ}

egin{description}
    item[Why does {	t delta} get replaced by $Delta$, {fontfamily{ptm}selectfont	exttildelow=} by $
eq$, etc.?]
        Well, that s precisely what the verb|useliterate| option does. If you don t want that, don t use it.
    item[Can I get contiguous line numbers from one code block to another?]
        Yes, but you have to read the verb|listings| documentation for that (Section~4.8 in particular).
    item[{	t mcode.sty} doesn t work in my document!] Well, try your (Matlab) code fragment in this demo document here to see whether there s something in it that might be causing a problem (not so likely, but possible), or if there s some conflict between the verb|listings| package and some other package you have loaded.
    item[Is feature XYZ possible?] Well, the verb|listings| package might already be able to do that. Please consult its documentation (see red box at the top)!
end{description}


end{document}

官方效果

Latex安装mcode包展示matlab代码

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容