列表断线不起作用

列表断线不起作用

我在 Latex 中使用 Listings 时遇到问题。断线功能无法使用。有人能看出我做错了什么吗?谢谢。

\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}

在此处输入图片描述

\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}  

答案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}

使用htt选项而不是nonehyphenatnone防止整个文档中的连字,而htt允许在“使用计算机现代打字机字体或等效字体的文本”中使用连字。

如果您的主文档中恰好有不想使用连字符的打字机字体短语,则可以使用textnhtt{...}{\nhttfamily ...}。(这些是本地化的无连字符 tt 环境。)

相关内容