Generallistings | Problem with Line Breaks

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Noceo
Posts: 5
Joined: Wed Apr 04, 2012 8:01 pm

listings | Problem with Line Breaks

Post by Noceo »

Hi,

I need to include a bunch of Matlab code in my report, and found a template for \lstset settings using Google (see code in the end of the post). I then added the breaklines=true in order to avoid problems with long lines. The thing is though, that it keeps writing "breaklines" every time it breaks a line (it literally writes it in letters in the document). I have looked everywhere for help, but can't find anyone who had this issue before. Am I completely retarded?

Hope you can help, thanks.

- Jake

Code: Select all

\lstloadlanguages{Matlab}
\lstset{language		= Matlab,                   % Use MATLAB
        basicstyle		= \small\ttfamily,          % Use small true type font
        keywordstyle	= [1]\color{Blue}\bf,       % MATLAB functions bold and blue
        keywordstyle	= [2]\color{Purple},        % MATLAB function arguments purple
        keywordstyle	= [3]\color{Blue}\underbar, % User functions underlined and blue
        identifierstyle	=,                       	% Nothing special about identifiers
        %
		% Comments small dark green courier
        commentstyle		= \usefont{T1}{pcr}{m}{sl}\color{MyDarkGreen}\small,
        stringstyle			= \color{Purple},       % Strings are purple
        showstringspaces	= false,                % Don't put marks in string spaces
        tabsize				= 5,                    % 5 spaces per tab
        %
        %%% Put standard MATLAB functions not included in the default
        %%% language here
        morekeywords={xlim,ylim,var,alpha,factorial,poissrnd,normpdf,normcdf},
        %
        %%% Put MATLAB function parameters here
        morekeywords=[2]{on, off, interp},
        %
        %%% Put user defined functions here
        morekeywords=[3]{FindESS, homework_example},
        %
        morecomment			= [l][\color{Blue}]{...},	% Line continuation (...) like blue comment
        numbers				= left,                     % Line numbers on left
        firstnumber			= 1,                        % Line numbers start with line 1
        numberstyle			= \tiny\color{Blue},        % Line numbers are blue
        stepnumber			= 5                         % Line numbers go in steps of 5
        breaklines 			= true
        breakatwhitespace 	= false
        prebreak 			= \space
   		postbreak 			= \space
        }

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

listings | Problem with Line Breaks

Post by cgnieder »

Please provide a “ready to copy and compile” code (that's called a Infominimal working example). I for my part don't want to guess which packages besides listings you are using and how

Code: Select all

MyDarkGreen
and friends are defined.

Best regards
site moderator & package author
Noceo
Posts: 5
Joined: Wed Apr 04, 2012 8:01 pm

listings | Problem with Line Breaks

Post by Noceo »

Hey again,
Sorry for missing that part, I was a bit frustrated yesterday. I made a working example, and it still makes the same error.

Code: Select all

% Dokument 'indstillinger'
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[danish]{babel}
\usepackage{times}
\usepackage{graphics}
\usepackage[pdftex]{graphicx}
\usepackage{epstopdf}
\usepackage{float}
\usepackage{fancyhdr}
\pagestyle{fancy}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{color}


% This is the color used for MATLAB comments below
\definecolor{MyDarkGreen}{rgb}{0.0,0.4,0.0}

% For faster processing, load Matlab syntax for listings
\lstloadlanguages{Matlab}
\lstset{language		= Matlab,                   % Use MATLAB
        basicstyle		= \small\ttfamily,          % Use small true type font
        keywordstyle	= [1]\color{Blue}\bf,       % MATLAB functions bold and blue
        keywordstyle	= [2]\color{Purple},        % MATLAB function arguments purple
        keywordstyle	= [3]\color{Blue}\underbar, % User functions underlined and blue
        identifierstyle	=,                       	% Nothing special about identifiers
        %
		% Comments small dark green courier
        commentstyle		= \usefont{T1}{pcr}{m}{sl}\color{MyDarkGreen}\small,
        stringstyle			= \color{Purple},       % Strings are purple
        showstringspaces	= false,                % Don't put marks in string spaces
        tabsize				= 5,                    % 5 spaces per tab
        %
        %%% Put standard MATLAB functions not included in the default
        %%% language here
        morekeywords={xlim,ylim,var,alpha,factorial,poissrnd,normpdf,normcdf},
        %
        %%% Put MATLAB function parameters here
        morekeywords=[2]{on, off, interp},
        %
        %%% Put user defined functions here
        morekeywords=[3]{FindESS, homework_example},
        %
        morecomment			= [l][\color{Blue}]{...},	% Line continuation (...) like blue comment
        numbers				= left,                     % Line numbers on left
        firstnumber			= 1,                        % Line numbers start with line 1
        numberstyle			= \tiny\color{Blue},        % Line numbers are blue
        stepnumber			= 5                         % Line numbers go in steps of 5
        breaklines 			= true
        breakatwhitespace 	= false
        prebreak 			= \space
   		postbreak 			= \space
        }


% Her starter dokumentet
\begin{document}


% Start the MATLAB code
\begin{lstlisting}

function drawImage(imagePlane, call, dynRange)
% drawImage(imagePlane, dynRange)
%
%   Plots the image either in the selected axes, if any exists. And sets
%   axes properties for the figure.
%
%   Input arguments
%       imagePlane: Structure  containing image array and axis. Can also be
%                   called with 'handles' if called from the GUI. In that
%                   case data will be rearranged accordingly.
%       dynRange:   Dynamic range used when the image is drawn. 
%
%   Output arguments
%       none:       drawImage is a function for drawing an already existing
%                   image in a figure or axes. The function does not
%                   generate any output itself.

% Check to see if the amount of input arguments are 2 or 3
if max(nargin ~= [2 3]) == 0
    errordlg('Incorrect amount of input arguments for drawImage.m')
end

% Rearrange data depending on how the function is called
switch call
    case 'gui' % Called from GUI
        handles    = imagePlane;
        imageArray = handles.imagePlane.Image;
        xAxis      = handles.imagePlane.p2axis.zx;
        dynRange   = handles.dynRange;
        
        % If measure points are already shown at the image, these will be
        % deleted, in order for new ones to be plotted.
        if ~isempty(handles.sPoint)
            handles.sPoint = [];
            guidata(gcbo,handles)
        end

    case 'console'  % Called from command prompt
        imageArray = imagePlane.Image;
        xAxis      = imagePlane.p2axis.zx;
        
    otherwise
        errordlg('Number of input arguments to ''drawImage'' must be one or two');
        
end


% Display image plane in the main axes
im = imagesc(100 * xAxis, ... % First axis
             100 * xAxis, ... % Second axis
             imageArray,  ... % The image
             [-dynRange 0]);  % Dynamic range   

axis square


% Update main axes handle properties
set(gca, 'color',         'black',  ...
         'YAxisLocation', 'right',  ...
         'box',           'off',    ...
         'TickDir',       'out',    ...
         'XColor',        'Blue',   ...
         'YColor',        'Red');
     

% Set OpenGL mode, to prevent bugs
opengl('software')
     
% Update handles if called from GUI
if strcmpi(call,'gui');
    % Add right click menu to the main axes
    set(im,'uicontextmenu',handles.cmenuAxes);
    guidata(gcbo,handles);
end

\end{lstlisting}

\end{document}
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

listings | Problem with Line Breaks

Post by cgnieder »

Hi,

I found the cause: it's just some missing commas in the last lines of the \lstset

Code: Select all

stepnumber  = 5 % Line numbers go in steps of 5
breaklines  = true
breakatwhitespace   = false
prebreak= \space
postbreak   = \space
Change these into

Code: Select all

stepnumber  = 5, % Line numbers go in steps of 5
breaklines  = true,
breakatwhitespace   = false,
prebreak= \space,
postbreak   = \space
and all looks like it's supposed to.

Regards
site moderator & package author
Noceo
Posts: 5
Joined: Wed Apr 04, 2012 8:01 pm

Re: listings | Problem with Line Breaks

Post by Noceo »

Why is the internet so much smarter than me? :geek:

Thanks a lot for the help, really appreciate it!
Post Reply