0

I have a problems with Listings in Latex. Breaklines is not working. Can anyone see what I have done wrong? Thank you.

\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\small,
    breakatwhitespace=false,  
    breakindent=1em,       
    breaklines=true,                  
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                      
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2,
    postbreak = \space,
}

\lstset{style=mystyle}

\begin{lstlisting}[language=Python, caption={Split of Data Set X into training and testing subsets.}, belowcaptionskip=10pt, label={lst:traintest}, alsoletter={()[].=}]
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, shuffle=False) 
\end{lstlisting}

enter image description here

\documentclass[a4paper, twoside, openright, 12pt]{report}
\makeatletter 
\def\cleardoublepage {\clearpage\if@twoside
    \ifodd \c@page
    \else \hbox{}\thispagestyle{empty}\newpage
    \if@twocolumn\hbox {}\newpage \fi\fi\fi}
\raggedbottom
\makeatother 
\usepackage{ramsstyle}
\usepackage{float}
\usepackage{parskip}
\usepackage{tikz}
\usetikzlibrary{positioning,shadows}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{shapes.multipart}
\usepackage{subfig}
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage[font=small,skip=5pt]{caption}
\usepackage{chemformula}
\usepackage{booktabs}
\usepackage{commath}
\usepackage{array}
\usepackage{gensymb}

\usepackage{xcolor}
\usepackage{framed}
\usepackage{lipsum}

\colorlet{shadecolor}{blue!20}

\usepackage{arydshln}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\usepackage{inputenc}
\usepackage{notoccite}
\usepackage[bottom]{footmisc} %så fotnote på bunn av side
\usepackage[none]{hyphenat} %så ikke bindestrek
\emergencystretch 3em %så rett tekstslutt på sidene
\usetikzlibrary{shapes,arrows}
\usepackage{pdfpages}
\usepackage{hyperref}
\usepackage{fancyhdr}
\hypersetup{
    colorlinks = true,
    linkcolor = black,
    citecolor = black,
    urlcolor = black
}

\usepackage{listings}
\usepackage{color}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\usepackage{amsmath}


\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\small,
    breakatwhitespace=false,  
    breakindent=1em,       
    breaklines=true,                  
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                      showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2,
    postbreak = \space,
}



\lstset{style=mystyle}


\ProvidesPackage{notoccite}[2000/07/20]
\def\@starttoc#1{%
    \begingroup
    \@fileswfalse
    \makeatletter
    \@input{\jobname.#1}%
    \endgroup
    \if@filesw
    \expandafter\newwrite\csname tf@#1\endcsname
    \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
    \fi
    \@nobreakfalse
}




%%===================================
\begin{document}
\include{front} 
\setcounter{page}{0}
\pagenumbering{roman}
\include{preface}
\include{abstract}
\include{sammendrag}
\tableofcontents
\listoffigures
\listoftables
\include{acronyms}
\include{nomenclature}
\pagenumbering{arabic}
\cleardoublepage
\setcounter{page}{1}
\fancyfoot{}
\pagestyle{fancy}
\lhead{}
\fancyfoot[LE,RO]{\thepage}
\include{chapter01}
\include{chapter02}
\include{chapter03}
\include{chapter04}
\include{chapter05}

% Include more chapters as required.
%%=========================================
\appendix
% Include more appendices as required.
%%=========================================
\bibliographystyle{IEEEtran}
\addcontentsline{toc}{chapter}{\bibname}
\bibliography{refs}  
  • I want it to split after y, before test_size – Martine Apr 26 '18 at 12:22
  • You do not apply your style to the listing using style=mystyle. – epR8GaYuh Apr 26 '18 at 12:23
  • Yes I have, forgot to include it in the question sorry. – Martine Apr 26 '18 at 12:25
  • 1
    Please add a complete MWE. Where the linebreak is happening depends on the margins you are setting - at the moment your the linebreaks is placed before the y. One way to get longer lines could be linewidth=1.1\linewidth inside the the optional arguments of the listing environment, see this question for example. – epR8GaYuh Apr 26 '18 at 12:31
  • 1
    I think hyphenat package is the issue. Try uncommenting it out from your code and compile. Or use htt option instead of none. – Troy Apr 26 '18 at 12:59
1
\documentclass[a4paper, twoside, openright, 12pt]{report}
\usepackage{inputenc}
\usepackage[htt]{hyphenat} % <------ Use htt instead of none

\usepackage{listings}
\usepackage{hyperref}
\hypersetup{
    colorlinks = true,
    linkcolor = black,
    citecolor = black,
    urlcolor = black
}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\small,
    breakatwhitespace=false,  
    breakindent=1em,       
    breaklines=true,                  
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                      showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2,
    postbreak = \space,
}

\lstset{style=mystyle}

\begin{document}
    \begin{lstlisting}[language=Python, caption={Split of Data Set X into training and testing subsets.}, belowcaptionskip=10pt, label={lst:traintest}, alsoletter={()[].=}]
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, shuffle=False) 
    \end{lstlisting}
\end{document}

Use htt option instead of none for the hyphenat package. none prevents hyphenation throughout the document, whereas htt allows hyphenation in "text using Computer Modern Typewriter fonts or their equivalent".

If you so happen to have typewriter-font phrases in your main document that you don't want hyphenated, then use textnhtt{...} or {\nhttfamily ...} instead. (These are localized no-hyphenation tt environments.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.