1

I am including some code into my tex file via \lstinputlisting[]{}. Unfortunately sometimes the breaklines=true-command does not work. Example: enter image description here

In line 65 the linebreak does not work, but in line 70 it works. Why? My lstset is:

\lstset{literate=
  {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
  {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
  {à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
  {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
  {ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
  {Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
  {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
  {Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
  {œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
  {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
  {€}{{\EUR}}1 {£}{{\pounds}}1,
  breaklines=true, postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\color{red}\hookrightarrow\space}},
  rulecolor=\color{black}
}

Full listing:

\documentclass[a4paper,11pt]{article}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{bibgerm} 
\usepackage{amssymb}
\usepackage{makeidx}
\usepackage[automark]{scrpage2}
\usepackage{listings}
\pagestyle{scrheadings}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{subcaption}
\usepackage{siunitx}
\usepackage{multicol}
\usepackage{xcolor}
\usepackage{yfonts}
\usepackage[bw,numbered,framed,autolinebreaks]{mcode}
%\usepackage[per-mode=fraction,output-decimal-marker={,}]{siunitx}
%\usepackage{multirow}
%\usepackage{bigstrut}
%\usepackage{rotating}

\setlength{\parindent}{0ex}
\setlength{\parskip}{1.5ex plus0.5ex minus0.2ex}
\setlength{\headheight}{26pt}
\addtolength{\topmargin}{-0.2in}
\addtolength{\headsep}{0.2in}
\addtolength{\oddsidemargin}{-0.2in}
\addtolength{\textwidth}{0.5in}

\renewcommand{\topfraction}{1.0}
\renewcommand{\bottomfraction}{1.0}
\renewcommand{\textfraction}{0.0}

\newcommand{\unit}[1]{\ensuremath{\, \mathrm{#1}}}
\newcommand{\sinc}{\mathrm{sinc}}
\newcommand{\cosc}{\mathrm{cosc}}


\begin{document}
\begin{lstlisting}
def jonswap(self, w, wp = None, alpha = 0.0081, gamma = 3.3, beta = 1.25):

            S.append(alpha*gamma*gamma/elem**5*math.exp(-beta*(wp/elem)**4)*gamma**r)
        return S 
\end{lstlisting}
\end{document}
7
  • 2
    Don't sent code snippets. Make complete, compilable (but small!) examples. Apr 8, 2015 at 13:11
  • Should I include my (included) code, too?
    – arc_lupus
    Apr 8, 2015 at 13:14
  • You should make a small but complete example that demonstrates your problem. Use your brain to decide how this is done best (hint: you probably can delete all the literate declarations, and you don't need \lstinputlistings, you can copy a small part of your code directly in a lstlisting environment). Apr 8, 2015 at 13:23
  • Only the part to make the problem understandable and reproducable for us. -> dickimaw-books.com/latex/minexample/html
    – Johannes_B
    Apr 8, 2015 at 13:23
  • 1
    @arc_lupus breaklines cannot work if you have no white space in the whole line.
    – yo'
    Apr 8, 2015 at 13:49

1 Answer 1

1

The problem is that breaklines doesn't take decisions. It has two simple rules for breaking:

  1. Don't break the initial indentation (i.e., leave at least something other than whitespace on the line)
  2. Break only at whitespace.

Since your line doesn't contain any whitespace, there's no feasible breakpoint whence no break.

3
  • 3
    Listings will break at other places too if you add (beside \breaklines=true the option breakatwhitespace=false. Apr 8, 2015 at 14:02
  • @UlrikeFischer Ah I didn't know that. Do you plan to make it an answer or should I improve mine?
    – yo'
    Apr 8, 2015 at 14:06
  • You can add it to your answer. Apr 8, 2015 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .