Take the 2-minute tour ×
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.

I want to include some code from MATLAB and I'm using the mcode package. But the problem I encounter with it is that it doesn't break long lines automatically and since I load the file and not the code. The problem will be solved if I put ... in the end of long lines, but I wondered is there any option in this package by which long lines be broken automatically?

Revision : here is the code :

‎\documentclass{book}‎

‎\usepackage{graphicx}‎ 
‎\usepackage{amsmath,amssymb}‎ 
‎\usepackage{caption}‎
‎\usepackage{color,xecolor}‎
‎\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}‎

‎\begin{document}‎

‎\begin{figure}‎
‎\caption{My program for plotting the above formula in 2 different conditions}‎
‎\lstinputlisting{technique.m}‎
‎\end{figure}‎

‎\end{document}

where technique.m is a matlab file and what I get from that is :

enter image description here

and it's out of the frame. I actually get the warning that autolinebreaks is an unknown option but I have no idea what should I do to solve the problem.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

Use \usepackage[autolinebreaks]{mcode}. Other possible options which may be of interest to you are [framed,numbered,autolinebreaks,useliterate.....]. For more details, look into the source code of mcode.

Edit for the revised question.

If autolinbreaks option is given, mcode.sty defines lstset as (line no 218 in mcode.sty)

\lstset{breaklines=true,breakatwhitespace=true,prebreak=\usebox{\lbreakdots}}

The culprit here is breakatwhitespace=true. Your formula has no white spaces and hence it will never break at the end of line.

Solution

Add

breakatwhitespace=false 

after loading mcode.

MWE will be

\documentclass{book}

\usepackage{graphicx}
\usepackage{amsmath,amssymb}
\usepackage{caption}
% \usepackage{color,xcolor}
\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}
\lstset{breakatwhitespace=false} %%<---this line added

\begin{document}

\begin{figure}
\caption{My program for plotting the above formula in 2 different conditions}
\lstinputlisting{technique.m}
\end{figure}

\lstinputlisting[caption={My program for plotting the above formula in 2 different
 conditions}]{technique.m}

\end{document}

enter image description here

Note: Instead of putting your code inside figure environment, you can use the second instance as I showed in the code. The differences are clear, I hope.

share|improve this answer
    
I actually use the autolinebreaks , but surprisingly it doesn't break the lines automatically! –  Negin May 6 '12 at 19:59
1  
@Negin: See the edit to answer. –  Harish Kumar May 6 '12 at 23:35
add comment

Another alternative is the package Listings (Documentation here). It's very customizable, but for example, this would suffice I think:

\usepackage{listings}    
\lstset{
        language=Matlab,
        breaklines=true
    }

You can then embed files using \lstinputlisting{test.m}. With additional styling parameters, one can produce something like this (these are the contents of test.m compiled into .pdf):

Compiled example

share|improve this answer
add comment

Your Answer

 
discard

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.