我知道我可以使用“mcode”在Latex中包含Matlab脚本,例如:
\begin{lstlisting}
clear, clc
load('Data.mat');
\end{lstlisting}
但是如何处理Matlab脚本输出呢?非常感谢
答案 0 :(得分:5)
mcode
package使用listings
设置适当的格式。实际上,以下内容取自mcode.sty
:
%% PLEASE NOTE that this package does nothing but save you from
%% figuring out some configurations in setting up the LISTINGS
%% package. ALL the work is done by that package! Thus, please
%% refer your questions to the listings package documentation.
因此,在加载mcode
后,使用\lstinputlisting{<file>}
输入列表:
\documentclass{article}
\usepackage{filecontents}% http://ctan.org/pkg/filecontents
\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}
\begin{filecontents*}{mscript.mat}
function y = myfun(aa, sigma, options)
sigma
y = aa .* pdf('logn', aa, -0.5*sigma^2, sigma)
%y = 1/(sigma.*sqrt(2.*pi)) .* exp((-((log(aa)+0.5*sigma.^2)).^2) ./ (2.*sigma.^2));
\end{filecontents*}
\begin{document}
\lstinputlisting{mscript.mat}
\end{document}
以上示例来自Inserting MATLAB code in the appendix。
为了插入MATLAB输出,我建议verbatim
环境:
\documentclass{article}
\begin{document}
\begin{verbatim}
>> A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
>> A(2,3)
ans =
11
\end{verbatim}
\end{document}