4

I am trying to make listing environment 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?

Karlo
  • 3,257
Arxas
  • 95

1 Answers1

5

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

Heiko Oberdiek
  • 271,626