Formatting source listings with listings & framed packages

10.6k Views Asked by At

I currently have a problem, that listings package cannot spread source files across multiple pages. In the doc is written, that the "framed" package should be used for various formatting option. Unfortunately I did not find any docs for the "framed" package. My current source formatting looks like this for C# sources:

Source Formatting http://www.free.image.hosting.net/uploads/88987a1ef4.png


Unfortunately the image service no longer exists and I can't find that image, since the post was posted more than 5 years ago. What I remember is that the formatted source code part, which should be visible on the next page, was just truncated and did not show up at all.

My formatting for "listings" package is:

\newcommand{\sourceFormatterCSharp}
{
\lstset
{ language=[Sharp]C
, captionpos=b
%, frame=lines
, morekeywords={var, get, set}
, basicstyle=\footnotesize\ttfamily
, keywordstyle=\color{blue}
, commentstyle=\color{darkgreen}
, stringstyle=\color{darkred}
, backgroundcolor=\color{lightgrey}
, numbers=left
, numberstyle=\scriptsize
, stepnumber=2
, numbersep=5pt
, breaklines=true
, tabsize=2
, showstringspaces=false
, emph={double, bool, int, unsigned, char, true, false, void, get, set}
, emphstyle=\color{blue}
, emph={Assert, Test}
, emphstyle=\color{red}
, emph={[2]\#using, \#define, \#ifdef, \#endif}
, emphstyle={[2]\color{blue}}
, frame=shadowbox
, rulesepcolor=\color{grey}
, lineskip={-1.5pt} % single line spacing
}
}

% first optional param is placement
% param1 file name without extension
% param2 chapter number, e.g. 1 or 2 ...
% param3 caption to use
\newcommand{\embedCSharp}[4][htbp]
{
\sourceFormatterCSharp
\includeListing{#1}{#4}{#3:#2}{#3/#2.cs}
}

Can anybody help me achieving similar looking results using "framed" package or any other for my source to look like this but be distributable across pages? An example how to embed a listing in the frame would not satisfy, since I was so far myself.

2

There are 2 best solutions below

3
On BEST ANSWER

The listings package already supports splitting code across pages; see example below (sorry about the long listing). Note that you cannot have a float that breaks across pages, so you'll need to use the caption package (for example) to insert a caption at the beginning of the lstlisting environment.

\documentclass{article}
\usepackage[a5paper,landscape]{geometry}
\usepackage{xcolor,listings}
\begin{document}
\definecolor{lightgrey}{gray}{0.8}
\lstset
{
captionpos=b
, backgroundcolor=\color{lightgrey}
, numbers=left
, numberstyle=\scriptsize
, stepnumber=2
, numbersep=5pt
, frame=shadowbox
, rulesepcolor=\color{gray}
}
\begin{lstlisting}
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
\end{lstlisting}
\end{document}
1
On

The framed documentation is within the .sty file itself. Just use it like this:

\documentclass{article}
\usepackage{framed,lipsum}
\begin{document}
\begin{framed}
\lipsum[1-10]
\end{framed}
\end{document}

From the docs, you can also use:

  • framed -- ordinary frame box (\fbox) with edge at margin
  • shaded -- shaded background (\colorbox) bleeding into margin
  • snugshade -- similar
  • leftbar -- thick vertical line in left margin

Putting your listings instead of lipsum in the example above will allow multiple pages of code with a frame around it all; you won't be able to get identical output to listings, but should be able to tweak things to get things looking okay.