利用MATLAB的mcode宏包,即可实现三种插入MATLAB的方式:
- 行内代码
- 行间代码块
- 以文件形式插入
mcode包的下载地址(及官方说明)
mcode帮助文档
另外Latex中运行代码块,需要lstlisting包
lstlisting 帮助文档
先上效果图:
1.行内代码
2.行间代码块
latex中代码实现为:
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.
\begin{lstlisting}
for i = 1:3
if i >= 5 && a ~= b % literate programming replacement
disp('cool'); % comment with some §\mcommentfont\LaTeX in it: $\mcommentfont\pi 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. \textbf{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 \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}|.
当然我不过是MATLAB mcode说明文档的搬运工,更多的可以直接看说明文档。我将文档截图放在下面。
对应的latex代码为:
\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}
\title{\texttt{mcode.sty} Demo}
\author{Florian Knorn, \texttt{florian@knorn.org}}
% //
\begin{document}
\maketitle
\begin{center}
\begin{minipage}{.75\linewidth}
\color{red}{\hfill\textbf{NOTE --- BEFORE YOU START}\hfill\strut}
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:\\\scriptsize\url{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.
\begin{lstlisting}
for i = 1:3
if i >= 5 && a ~= b % literate programming replacement
disp('cool'); % comment with some §\mcommentfont\LaTeX in it: $\mcommentfont\pi 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. \textbf{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 \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}|.
\section*{FAQ}
\begin{description}
\item[Why does {\tt delta} get replaced by $\Delta$, {\fontfamily{ptm}\selectfont\texttildelow=} by $\neq$, 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[{\tt 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}