9

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?

0

2 Answers 2

10

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

Well I mostly use line numbering so the line number is a good hint. And I also often use frames, which you can see in my example. It is imho a nice way to indicate that the code is not complete yet and carry over a pagebreak.

Using listings you can set frame=single or one of the following options: none, leftline, topline, bottomline, shadowbox. For more detail you can check the listings documentation, where I also found the possible frames on page 15.

The \lstset is my usual setup and the source code is from an answer I gave here on TeX.sx.

Code

\documentclass{article}

\usepackage{lipsum}
\usepackage{listings}

\usepackage{xcolor}
\definecolor{gray}{rgb}{0.25,0.25,0.25}

\lstset{
  basicstyle=\footnotesize,
  breakatwhitespace=false,
  breaklines=true,
  frame=shadowbox,
  language=[LaTeX]{TeX},
  numbers=left,
  numbersep=5pt,
  numberstyle=\tiny\color{gray},
  title=\lstname
}

\begin{document}
\lipsum[1-3]

\begin{lstlisting}
\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\input{kvmacros}

\begin{document}
\karnaughmap{5}{$f(x_i)$}%
{{$x_1$}{$x_2$}{$x_3$}{$x_4$}{$x_5$}}%
{%
0011011001100110%
0110011001000110%
}%
{%
%Single Ones
\textcolor{Blue}{
\put(2.5,3.5){\oval(0.9,0.9)[]}
\put(7.5,2.5){\oval(0.9,0.9)[]}
\put(0.5,1.5){\oval(0.9,0.9)[]}}
%Pairs of Ones
\put(1,2.5){\oval(1.9,0.9)[]}
%Quadruples of Ones
\textcolor{Yellow}{
\put(4,2){\oval(1.9,1.9)[]}}%
\textcolor{Green}{
\put(6,4){\oval(1.9,1.9)[b]}
\put(6,0){\oval(1.9,1.9)[t]}}%
\textcolor{Red}{
\put(5,0.5){\oval(3.9,0.9)[r]}
\put(3,0.5){\oval(3.9,0.9)[l]}}
\end{lstlisting}

\lipsum[4]
\end{document}

Restult

image

You must log in to answer this question.

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