在LATEX中优雅地插入MATLAB代码

本文介绍了如何在LaTeX文档中利用mcode宏包插入和格式化MATLAB代码,包括行内代码、代码块和直接包含外部文件的方法。详细展示了不同插入方式的示例,并提供了相关包的下载链接和使用注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用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}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值