我正在使用从 IEEE 网页下载的 LaTeX 模板编写 IEEE 文档。我通过软件包将一些 MATLAB 代码插入到文档中mcode
。它看起来像这样:
\usepackage[framed]{mcode}
.
.(document)
.
\begin{lstlisting}
MATLAB code
\end{lstlisting}
代码看起来很棒,字体和颜色都正确,但问题是 IEEE 模板定义了一个两列文档,代码超出了边距,无法容纳。有什么方法可以解决这个问题吗?
答案1
\lstset{breaklines}
在你的序言中使用(加载listings
包之后);这应该会打开自动换行功能。
或者使用matlab-prettifier
包,默认情况下设置breaklines
;请参阅下面的示例(我改编了 IEEE 出版物的示例论文,可从这里)。
\documentclass[12pt,journal,compsoc]{IEEEtran}
\usepackage[T1]{fontenc}
\usepackage[numbered]{matlab-prettifier}
\lstdefinestyle{mymatstyle}{%
style=Matlab-editor,
basicstyle=\mlttfamily,
frame=leftline,
numberstyle=\scriptsize,
xleftmargin=1.8em,
}
\begin{document}
\title{Fooling around with Matlab\\doesn't make you a programmer}
\author{Jubobs,~\IEEEmembership{Member,~IEEE (not really)}}
\markboth{Journal of \LaTeX\ Class Files,~Vol.~6, No.~1, January~2007}%
{Shell \MakeLowercase{\textit{et al.}}: Bare Demo of IEEEtran.cls for Computer Society Journals}
\IEEEcompsoctitleabstractindextext{%
\begin{abstract}
The abstract goes here.
\end{abstract}
\begin{IEEEkeywords}
Matlab, cargo-cult programming, Python FTW
\end{IEEEkeywords}}
\maketitle
\IEEEdisplaynotcompsoctitleabstractindextext
\IEEEpeerreviewmaketitle
\section{Introduction}
\subsection{Subsection Heading Here}
(Adapted from Matlab's peaks function)
\begin{lstlisting}[style=mymatstyle]
function [xz,y,z] = peaks(arg1,arg2)
if nargin == 0
dx = 1/8;
[x,y] = meshgrid(-3:dx:3);
elseif nargin == 1
if length(arg1) == 1
[x,y] = meshgrid(linspace(-3,3,arg1));
else
[x,y] = meshgrid(arg1,arg1);
end
else
x = arg1; y = arg2;
end
z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...
- 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
- 1/3*exp(-(x+1).^2 - y.^2);
if nargout > 1
xz = x;
elseif nargout == 1
xz = z;
else
% Self demonstration
disp(' ')
disp('z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... ')
disp(' - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... ')
disp(' - 1/3*exp(-(x+1).^2 - y.^2) ')
disp(' ')
surf(x,y,z)
axis('tight')
xlabel('x'), ylabel('y'), title('Peaks')
end
\end{lstlisting}
\section{Conclusion}
The conclusion goes here.
\end{document}