# What can I do if a listing is too wide to fit in my two-column document?

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?

-
Welcome to TeX.SX! Does the answer to tex.stackexchange.com/questions/54438/… help? – Torbjørn T. Aug 8 '14 at 22:50
Is my answer useful? – Jubobs Aug 9 '14 at 18:41
Completely, thanks a lot – JsMartinez Aug 9 '14 at 22:39

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).

\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}

\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}

-
I should certainly upvote this to thank you because now I use matlab-prettifier ;) – Harish Kumar Aug 9 '14 at 0:34
@HarishKumar Haha thanks! That makes at least 2 users :) – Jubobs Aug 9 '14 at 0: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 9 '14 at 5:37
@mvkorpel Yes about the numbers, but no about ...: if it's not present in the code, it shouldn't be printed in the document. – Jubobs Aug 9 '14 at 9:10