6

I am writing a IEEE document with a LaTeX template I downloaded from the IEEE webpage. I inserted some MATLAB code into the document through the mcode package. It looks something like this:

\usepackage[framed]{mcode}
.
.(document)
.
\begin{lstlisting} 
MATLAB code
\end{lstlisting}

The code looks great, whith the right font and colours but the problem is that the IEEE template defines a two-column document, and the code exceeds margins and doesn't fit. Is there any way to fix this?

jub0bs
  • 58,916

1 Answers1

7

Use \lstset{breaklines} in your preamble (after loading the listings package); that should switch automatic line breaking on.

Or use the matlab-prettifier package, which sets breaklines by default; see below for an example (I adapted the example paper for IEEE publications, which is available here).

enter image description here

\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}
jub0bs
  • 58,916
  • 2
    I should certainly upvote this to thank you because now I use matlab-prettifier ;) –  Aug 09 '14 at 00:34
  • 1
    @HarishKumar Haha thanks! That makes at least 2 users :) – jub0bs Aug 09 '14 at 00:35
  • Technically, I think that ... should be added to continue a statement to the next line. Alternatively, line numbers would show that two lines in the paper are actually the same line in the Matlab code. – mvkorpel Aug 09 '14 at 05:37
  • 1
    @mvkorpel Yes about the numbers, but no about ...: if it's not present in the code, it shouldn't be printed in the document. – jub0bs Aug 09 '14 at 09:10