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 am trying to make listing enviroment brake lines. Unfortunately breaklines=true does not work for me. Here is my code:

\usepackage{listings}

\begin{document}

\lstset{language=matlab,breaklines=true, extendedchars=true, numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt, basicstyle=\footnotesize, frame=lines, keywordstyle=\bfseries\color{blue}, commentstyle=\color{green!50!black}}

\lstset{emph={classdef, properties, methods},emphstyle={\color{blue}\bfseries}}

\renewcommand{\lstlistingname}{Algorytm}

\begin{lstlisting}[caption=Funkcja \emph{createFoR} klasy \emph{row}., label=func_createFoR, float=ht]
classdef row < handle

properties
rp  %promien rownoleznika
end

methods 
    function [T1 T2] = createFoR(obj,hT,Alpha,Beta,DeltaAlpha,DeltaBeta)
         P1(1,1) = obj.r * cosd(Beta);
         P1(1,2) = obj.r * cosd(90 - Beta);
         P1(1,3) = obj.r * tand(90 - (Alpha + DeltaAlpha));
         P1(2,:) = hT.P(1,:);
         %hT - trojkat z wyzszego wiersza
         P1(3,1) = obj.r * cosd(Beta + DeltaBeta);
         P1(3,2) = obj.r * cosd(90 - (Beta + DeltaBeta));
         P1(3,3) = obj.r * tand(90 - (Alpha + DeltaAlpha)); 
         T1 = triangle(P1);
         P2(1,:) = hT.P(1,:);
         P2(2,:) = hT.P(3,:);
         P2(3,:) = P1(3,:);
         T2 = triangle(P2);
    end
end
\end{lstlisting}

This is how it looks: http://i.imgur.com/VjTH3ku.png

May I kindly ask you for help?

share|improve this question
1  
breaklines does work for me in lines 8, 15, and 16, if the code starts with \documentclass[a5paper]{article}\usepackage{xcolor}. –  Heiko Oberdiek Aug 24 '13 at 20:02
    
BTW, \lstset{columns=flexible} makes the lines shorter and the spacing of the words more natural. –  Heiko Oberdiek Aug 24 '13 at 20:03
    
Thank you Heiko. However I am using mwrep class which defines style for documents written in polish and breaklines does not work with it. Any idea how to fix it? –  Arxas Aug 24 '13 at 21:33
    
You might be interested in the matlab-prettifier package; see this answer. –  Jubobs Apr 28 at 15:48

1 Answer 1

up vote 4 down vote accepted

The class mwrep sets \exhyphenpenalty=10000. This setting prevents line breakings after explicit hyphens or \discretionary items. The latter uses package listings.

The following patch resets the setting for \exhyphenpenalty for listings:

\makeatletter
\lst@AddToHook{Init}{\exhyphenpenalty=50\relax}
\makeatother

Full example:

\documentclass{mwrep}
\usepackage{xcolor}
\usepackage{listings}

\makeatletter
\lst@AddToHook{Init}{\exhyphenpenalty=50\relax}
\makeatother

\begin{document}

\lstset{language=matlab,breaklines=true, extendedchars=true, numbers=left, numb

\lstset{emph={classdef, properties, methods},emphstyle={\color{blue}\bfseries}}

\renewcommand{\lstlistingname}{Algorytm}

\begin{lstlisting}[caption=Funkcja \emph{createFoR} klasy \emph{row}., label=fu
classdef row < handle

properties
rp  %promien rownoleznika
end

methods
    function [T1 T2] = createFoR(obj,hT,Alpha,Beta,DeltaAlpha,DeltaBeta)
         P1(1,1) = obj.r * cosd(Beta);
         P1(1,2) = obj.r * cosd(90 - Beta);
         P1(1,3) = obj.r * tand(90 - (Alpha + DeltaAlpha));
         P1(2,:) = hT.P(1,:);
         %hT - trojkat z wyzszego wiersza
         P1(3,1) = obj.r * cosd(Beta + DeltaBeta);
         P1(3,2) = obj.r * cosd(90 - (Beta + DeltaBeta));
         P1(3,3) = obj.r * tand(90 - (Alpha + DeltaAlpha));
         T1 = triangle(P1);
         P2(1,:) = hT.P(1,:);
         P2(2,:) = hT.P(3,:);
         P2(3,:) = P1(3,:);
         T2 = triangle(P2);
    end
end
\end{lstlisting}
\end{document}

Result


If possible, I would use columns=flexible, it weakens the vertical alignment a little, thus it depends on the source code formatting, if this is an option. But the benefit are smaller line width, because package listings do not need to reserve space for the wider characters of the font. It reduces line breaks and the words of the listings are set in a more natural way and are easier to read.

For comparison, the next image is generated with the following setting:

\lstset{columns=flexible}

Result with columns=flexible

share|improve this answer

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.