TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


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?

share|improve this question
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
up vote 6 down vote accepted

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





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


The abstract goes here.

Matlab, cargo-cult programming, Python FTW





\subsection{Subsection Heading Here}
(Adapted from Matlab's peaks function)

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));
        [x,y] = meshgrid(arg1,arg1);     
    x = arg1; y = arg2;

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;
    % 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(' ')
    xlabel('x'), ylabel('y'), title('Peaks')


The conclusion goes here.

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.