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 231 in mcode.sty of version 2.7.0.0)
\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}
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.
Best Answer
The
mcode.sty
file has a "Customize here" section.Change
into
and you'll get the default fixed width font instead of Courier. Or
that will choose the Latin Moder Typewriter font, which has also a boldface variant.