4

I am using the listings package. The problem I have is that if the code gets in the middle of two packages, it is cut, which I don't want. To kind of solve that, I am using the package needspace and I defined my command:

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

So then I can do something like:

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

The problem is that I need to do this for every piece of code and to calculate the needed number of lines. Does anyone know if there is a way to do it automatically?

Just in case I paste all my code, thanks!!

    % 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}
4
  • 2
    If you are sure it'll fit on one page you could just put into a minipage so it will be kept together: \begin{minipage}{\linewidth}\begin{lstlisting}...\end{lstlisting}\end{minipage}% Jun 18, 2012 at 20:52
  • @PeterGrill and you could even use the etoolbox to wrap a minipage around every lstlisting envrionment
    – cmhughes
    Jun 18, 2012 at 21:21
  • @cmhughes: Hmmm, it is a verbatim environment so might be an issue (plus there might be case of a long listing where a page break is needed). But \lstnewenvironment can be used to define a new MyListing environment to be used where you don't want a page break. Jun 18, 2012 at 21:30
  • Hi @PeterGrill. Thanks for the answer. I am using \lstnewenvironment (see for example the one named "code"). How can I add there the minipage idea? thanks! Jun 18, 2012 at 21:34

1 Answer 1

3

As mentioned in the comments if you wrap something within a \minipage it will not break across page boundaries (which sometime may not be desirable).

Here is an example showing the use of \lstnewenvironment to wrap a minipage environment around the listings environment. The MWE will produce the two lines on page 2, but if you comment out the \minipage{\linewidth} and \endminipage in the definition of the code environment, the code listing will be broken across two pages.

enter image description here

Notes:

Code:

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

You must log in to answer this question.

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