该宏包可以在论文中生成漂亮的matlab代码,更有甚者,直接读入一个m文件,很方便吧。下载路径 宏包下载地址: http://cid-3cfe530319707374.office.live.com/self.aspx/ChinaTeX/mcode.zip http://cid-3cfe530319707374.office.live.com/self.aspx/ChinaTeX/mcode^_demo.zip
http://www.mathworks.com/matlabcentral/fileexchange/8015-m-code-latex-package ,http://www.knorn.org/misc_files/mcode_demo.zip 给出了一个示例。
2010年5月4日最新升级了该包。mcode实际基于listings开发的(支持多种编程语言的,http://en.wikibooks.org/wiki/LaTeX/Packages/Listings ),大家可以也学习一下。此外还有"xcolor","textcomp"。mcode主要是为了用起来更简单方便。
示例:\documentclass{article}
% load package with ``framed'' and ``numbered'' option.
\usepackage[framed,numbered]{mcode}
% something NOT relevant to the usage of the package.
\setlength{\parindent}{0pt}
\setlength{\parskip}{18pt}
\title{\texttt{mcode.sty} Demo}
\author{Florian Knorn, \texttt{florian@knorn.org}}
% //////////////////////////////////////////////////
\begin{document}
\maketitle
\textbf{Installation of the package:} As with any other package, just place the \verb|mcode.sty| file in the same folder as your document, or put it somewhere where \LaTeX{} can find it. That's all!
1) This inline demo \mcode{for i=1:3, disp('cool'); end;} uses the \verb|\mcode{}| command.
2) The following is a block using the \verb|lstlisting| environment.
\begin{lstlisting}
for i=1:3
disp('cool'); % comment with some in it: $\pi x^2$
x_last = x(iend);
end
\end{lstlisting}
Note: That package was loaded with the \verb|framed| and \verb|numbered| option.
3) Finally, you can also directly include an external m-file from somewhere on your hard drive (the very code you use in \textsc{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}|.
\textbf{Remkark:} As pointed out by ``turker'' in a comment at the \textsc{MatlabCentral} listing of the package, there can be a problem if you use the word \texttt{end} to access the last element in a vector (line 3 in the example above) --- this would normally be typeset in blue by default, as it will be considered as a keyword (just like in line 4 above). But if you really want those indexing-``ends'' to be typeset in black, replace them by \texttt{iend} in your source code. While this will break the \textsc{Matlab} code, at least you can get it typeset in black... Not a pretty trade off, but I don't see another way out.
Florian (\texttt{florian@knorn.org})
\end{document} 效果如下:
发表评论 取消回复