我可以自动为列表包设置 \needspace 吗?

我可以自动为列表包设置 \needspace 吗?

我正在使用 listings 包。我遇到的问题是,如果代码位于两个包的中间,它就会被切断,这是我不想要的。为了解决这个问题,我使用了该包needspace并定义了我的命令:

  \newcommand{\needlines}[1]{\Needspace{#1\baselineskip}}

那么我可以做类似的事情:

  \needlines{2}
  \begin{lstlisting}
  this is one line of code
  this is another one
  \end{lstlisting}
  \end{needlines}

问题是我需要对每段代码都这样做,并计算所需的行数。有人知道有没有办法自动完成这个操作吗?

以防万一我粘贴我的所有代码,谢谢!!

    % Source Code
    \usepackage{color}
    \usepackage{textcomp}
    \usepackage{listings}
    %\usepackage{ulem}
    %\usepackage[T1]{fontenc}
    %\usepackage{times}
    % \usepackage{needspace}


    \definecolor{source}{gray}{0.85}% my comment style
    \newcommand{\myCommentStyle}[1]{{\footnotesize\ttfamily\color{gray!100!white} #1}}

    % my string style
    \newcommand{\myStringStyle}[1]{{\footnotesize\ttfamily\color{violet!100!black} #1}}

    % my symbol style
    \newcommand{\mySymbolStyle}[1]{{\footnotesize\ttfamily\color{violet!100!black} #1}}

    % my keyword style
    \newcommand{\myKeywordStyle}[1]{{\footnotesize\ttfamily\color{green!70!black} #1}}

    % my global style
    \newcommand{\myGlobalStyle}[1]{{\footnotesize\ttfamily\color{blue!100!black} #1}}

    % my number style
    \newcommand{\myNumberStyle}[1]{{\footnotesize\ttfamily\color{brown!100!black} #1}}

    \lstset{
    language={},
    % characters
    tabsize=3,
    escapechar={!},
    keepspaces=true,
    breaklines=true,
    alsoletter={\#},
    breakautoindent=true,
    columns=fullflexible,
    showstringspaces=false,
    % background
    frame=single,
    aboveskip=1em, % automatic space before
    framerule=0pt,
    basicstyle=\footnotesize\sffamily\color{black},
    keywordstyle=\myKeywordStyle,% keyword style
    commentstyle=\myCommentStyle,% comment style
    frame=single,%
    backgroundcolor=\color{source},
    % numbering
    stepnumber=1,
    numbersep=10pt,
    numberstyle=\tiny,
    numberfirstline=true,
    % caption
    captionpos=b,
    % formatting (html)
    moredelim=[is][\bfseries]{<b>}{</b>},
    moredelim=[is][\textit]{<i>}{</i>},
    moredelim=[is][\uline]{<u>}{</u>},
    moredelim=[is][\color{red}\uwave]{<wave>}{</wave>},
    moredelim=[is][\color{red}\sout]{<del>}{</del>},
    moredelim=[is][\color{blue}\uline]{<ins>}{</ins>},
    % smalltalk stuff
    morecomment=[s][\myCommentStyle]{"}{"},
    %    morecomment=[s][\myvs]{|}{|},
    morestring=[b][\myStringStyle]',
    moredelim=[is][]{<sel>}{</sel>},
    moredelim=[is][]{<rcv>}{</rcv>},
    moredelim=[is][\itshape]{<symb>}{</symb>},
    moredelim=[is][\scshape]{<class>}{</class>},
    morekeywords={true,false,nil,self,super,thisContext},
    identifierstyle=\idstyle,
    }

    \makeatletter
    \newcommand*\idstyle[1]{%
    \expandafter\id@style\the\lst@token{#1}\relax%
    }
    \def\id@style#1#2\relax{%
    \ifnum\pdfstrcmp{#1}{\#}=0%
    % this is a symbol
    \mySymbolStyle{\the\lst@token}%
    \else%
    \edef\tempa{\uccode`#1}%
    \edef\tempb{`#1}%
    \ifnum\tempa=\tempb%
    % this is a global
    \myGlobalStyle{\the\lst@token}%
    \else%
    \the\lst@token%
    \fi%
    \fi%
    }
    \makeatother

    \lstset{literate=%
    *{0}{{{\myNumberStyle{0}}}}1
    {1}{{{\myNumberStyle{1}}}}1
    {2}{{{\myNumberStyle{2}}}}1
    {3}{{{\myNumberStyle{3}}}}1
    {4}{{{\myNumberStyle{4}}}}1
    {5}{{{\myNumberStyle{5}}}}1
    {6}{{{\myNumberStyle{6}}}}1
    {7}{{{\myNumberStyle{7}}}}1
    {8}{{{\myNumberStyle{8}}}}1
    {9}{{{\myNumberStyle{9}}}}1
    }

    %\newcommand{\ct}{\lstinline[backgroundcolor=\color{white}]}
    \newcommand{\needlines}[1]{\Needspace{#1\baselineskip}}
    \newcommand{\lct}{\texttt}

    \lstnewenvironment{code}{%
    \lstset{%
    % frame=lines,
    frame=single,
    framerule=0pt,
    mathescape=false
    }
    }{}


    \lstnewenvironment{codeWithLineNumbers}{%
    \lstset{%
    % frame=lines,
    frame=single,
    framerule=0pt,
    mathescape=false,
    numbers=left,
    }
    }{}


    \newenvironment{codeNonSmalltalk}
    {\begin{alltt}\sffamily}
    {\end{alltt}\normalsize}

答案1

正如评论中所提到的,如果你将某些内容包装在其中,\minipage它就不会跨越页面边界(有时这可能不是所希望的)。

下面是一个示例,展示了如何使用\lstnewenvironment来将minipage环境包裹在环境周围listings。MWE 将在第 2 页生成两行代码,但是如果您在环境定义中注释掉\minipage{\linewidth}和,代码列表将分为两页。\endminipagecode

在此处输入图片描述

笔记:

代码:

\documentclass{article}
\usepackage[paperheight=6.7cm,showframe]{geometry}
\usepackage{lipsum}
\usepackage{listings}

\lstnewenvironment{code}{%
    \lstset{%
    % frame=lines,
    frame=single,
    framerule=0pt,
    mathescape=false
    }%
    \noindent%
    \minipage{\linewidth}%
}{%
    \endminipage%
}%

\begin{document}
\lipsum[1]
\begin{code}
  this is one line of code
  this is another one
\end{code}
\end{document}

相关内容