[Tex/LaTex] How to show a hint when lstlisting is breaking page

listingsnotespage-breaking

I use inline lstlisting-blocks with caption.
I have several listings that are running over more than one page or starting at the end of a page.
Now I want to place a hint for the readers, that the listing hasn't ended and goes further on the next page. This hint should be something like "further listing on next/the next but one page".

I have searched in the section 4 of the listings package manual and haven't found such parameter yet. Is there a possibility to show such a hint?

Best Answer

You could use the help of mdframed and expecially its options singleextra, firstextra, secondextra and middleextra. The different options allow you to have different styles for a frame that's on a single page and a frame that is broken over two or more pages.

When mdframed is used with framemethod=tikz you have access to the corners of a frame. The node on the lower left corner is called (O) and the one on the upper right (P). The lower right one can thus be accessed by (O -| P) and the upper left one by (P -| O).

This can be used in combination with \lstnewenvironment to get a customized listings environment that does what one wants.

Below I define a mdframed style that adds some continuing information if a frame is split and a new listings environment that uses this frame style. Here is how it looks:

enter image description here

And here is the code:

\documentclass{article}
\usepackage[T1]{fontenc}

\usepackage[framemethod=tikz]{mdframed}
% define the frame style for the listing:
\mdfdefinestyle{note}
  {
    hidealllines = true ,
    skipabove    = .5\baselineskip ,
    skipbelow    = .5\baselineskip ,
    singleextra  = {} ,
    firstextra   = {
      \node[below right,overlay,align=left,font=\continuingfont]
        at (O) {\continuingtext};
    } ,
    secondextra  = {
      \node[above right,overlay,align=left,font=\continuingfont]
        at (O |- P) {\continuedtext};
    } ,
    middleextra  = {
      \node[below right,overlay,align=left,font=\continuingfont]
        at (O) {\continuingtext};
      \node[above right,overlay,align=left,font=\continuingfont]
        at (O |- P) {\continuedtext};
    }
  }

% customize the appearance of the continuing notes:
\newcommand*\continuingfont{\footnotesize\itshape}
\newcommand*\continuingtext{Listing continues on next page}
\newcommand*\continuedtext{Continuing from last page}

\usepackage{listings}
% define the listings style:
\lstdefinestyle{code}{
  language         = [LaTeX]TeX,
  basicstyle       = \small\ttfamily ,
  numbers          = left,
  numberstyle      = \tiny,
  numberblanklines = true,
  breaklines       = true,
  keepspaces       = true,
  columns          = fullflexible,
  % whatever else you want ...
}

% define the environment:
\lstnewenvironment{listing}
  {%
    \lstset{style=code}%
    \mdframed[style=note]%
  }
  {%
    \endmdframed
  }

\usepackage{lipsum}% dummy text

\begin{document}

\lipsum[1-2]

\begin{listing}
\usepackage[framemethod=tikz]{mdframed}
\mdfdefinestyle{note}
  {
    hidealllines = true ,
    skipabove    = .5\baselineskip ,
    skipbelow    = .5\baselineskip ,
    singleextra  = {} ,
    firstextra   = {
      \node[below right,overlay,align=left,font=\continuingfont]
        at (O) {\continuingtext};
    } ,
    secondextra  = {
      \node[above right,overlay,align=left,font=\continuingfont]
        at (O |- P) {\continuedtext};
    } ,
    middleextra  = {
      \node[below right,overlay,align=left,font=\continuingfont]
        at (O) {\continuingtext};
      \node[above right,overlay,align=left,font=\continuingfont]
        at (O |- P) {\continuedtext};
    }
  }
\newcommand*\continuingfont{\footnotesize\itshape}
\newcommand*\continuingtext{Listing continues on next page}
\newcommand*\continuedtext{Continuing from last page}

\usepackage{listings}
\lstdefinestyle{code}{
  language         = [LaTeX]TeX,
  basicstyle       = \small\ttfamily ,
  numbers          = left,
  numberstyle      = \tiny,
  numberblanklines = true,
  breaklines       = true,
  keepspaces       = true,
  columns          = fullflexible,
  % whatever else you want ...
}
\lstnewenvironment{listing}
  {%
    \lstset{style=code}%
    \mdframed[style=note]%
  }
  {%
    \endmdframed
  }
\end{listing}

\lipsum[2]

\end{document}
Related Question