# lstlistings: framed code

I use lstlistings to write Pascal Code. I'd like my code to be numbered and framed. Here is what I have as of now :

\lstset{numbers=left, numberstyle=\small, numbersep=8pt, frame = single, language=Pascal, framexleftmargin=15pt}

\begin{lstlisting}
PROGRAM afficher_bonjour;
BEGIN WRITE('Bonjour');
END.
\end{lstlisting}


My problem is that the frame is the width of the page, so I get a big frame with lot of useless empty space on the right. I'd like the frame to be the width of the code I actually wrote.

Any hints?

-
 Same problem exactly. Have you found a solution? Thanks in advance. Christophe – user21240 Oct 22 '12 at 17:17

Another option is to use the linewidth option for lstlisting to control the width:

\documentclass{article}
\usepackage{listings}

\lstset{
numbers=left,
numberstyle=\small,
numbersep=8pt,
frame = single,
language=Pascal,
framexleftmargin=15pt}

\begin{document}

\begin{lstlisting}
PROGRAM afficher_bonjour;
BEGIN WRITE('Bonjour');
END.
\end{lstlisting}

\begin{lstlisting}[linewidth=5.4cm]
PROGRAM afficher_bonjour;
BEGIN WRITE('Bonjour');
END.
\end{lstlisting}

\end{document}​


-
 Thank you both for your solution. However, I'd like the horizontal space to be computed automatically. I have lots of codes to be framed and dont want to have to adjust manually the size of my minipage or my linewidth. – guigui Sep 4 '12 at 20:35

You should place the entire lstlisting inside an environment that restricts the horizontal width, like a minipage:

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\begin{document}
\lstset{numbers=left, numberstyle=\small, numbersep=8pt, frame = single, language=Pascal, framexleftmargin=15pt}

\begin{lstlisting}
PROGRAM afficher_bonjour;
BEGIN WRITE('Bonjour');
END.
\end{lstlisting}

\noindent\begin{minipage}{.5\linewidth}
\begin{lstlisting}
PROGRAM afficher_bonjour;
BEGIN WRITE('Bonjour');
END.
\end{lstlisting}
\end{minipage}
\end{document}​

-
Thank you both for your solution. However, I'd like the horizontal space to be computed automatically. I have lots of codes to be framed and dont want to have to adjust manually the size of my minipage or my linewidth. – guigui Sep 4 '12 at 20:35

The frame= key of the listings package always typesets the frame with respect to the current line width. Hence, I suggest to use another command or environment to draw the frame.

A basic solution (without further packages) would be to render the listing inside an \fbox, which, however, as a macro does not accept verbatim material as argument. So the listing has to by typesetted in an lrbox first.

Alternatively, some framing environment may be used (in the example below I use adjustbox, but you literally have dozens of possibilities here), which would not require the detour over an lrbox.

Finally (not presented below) you my define your own PASCAL environment with \lstnewenvironment{PASCAL}{<begin code>}{<end code>} (see listings documentation) that combines either method with the listings environment to gain maximum comfort.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings}

\usepackage{adjustbox} % For Option 2 only

\begin{document}
\lstset{numbers=left, numberstyle=\small, numbersep=8pt, language=Pascal}

% Option 1: Typeset in an LR-box and use \fbox{} to draw the frame
\newsavebox{\Lst}

\begin{lrbox}{\Lst}
\begin{lstlisting}
PROGRAM afficher_bonjour;
BEGIN WRITE('Bonjour');
END.
\end{lstlisting}
\end{lrbox}

% draw frame an Listing. The \hskip enlarges the box to have numbers within the frame
\noindent\fbox{\hskip15pt\usebox{\Lst}}

% Option 2: Use some framing environment. There are lots of possibilities!
% I have just use adjustbox, which is a kind of swiss army knife for boxing
\begin{lstlisting}
PROGRAM afficher_bonjour;
BEGIN WRITE('Bonjour');
END.
\end{lstlisting}

\end{document}


-
 @Chris: Does this solve your problem? – Daniel Oct 23 '12 at 15:11

Not sure if it will work for all your code but you can use \widthof+\hphantom commands to fix minipage's width. Something like:

\begin{minipage}{\widthof{\hphantom{\lstinline{PROGRAM afficher bonjour}}}}


where PROGRAM afficher bonjour is the longest line of your code. It would be better to use \begin{lstlisting}...\end{lstlisting} instead of \lstinline because linenumbers width would be included. It didn't worked for me, so I used \lstinline with some fixed space (look at third block in next code). \widthof is part of calc package.

\documentclass{article}
\usepackage{calc}
\usepackage{listings}% http://ctan.org/pkg/listings
\begin{document}
\lstset{numbers=left, numberstyle=\small, numbersep=8pt, frame = single, language=Pascal, framexleftmargin=15pt}

\begin{lstlisting}
PROGRAM afficher_bonjour;
BEGIN WRITE('Bonjour');
END.
\end{lstlisting}

\noindent\begin{minipage}{\widthof{\hphantom{\lstinline{PROGRAM afficher bonjour}}}}
\begin{lstlisting}
PROGRAM afficher_bonjour;
BEGIN WRITE('Bonjour');
END.
\end{lstlisting}
\end{minipage}

\noindent\begin{minipage}{\widthof{\hphantom{\lstinline{PROGRAM afficher bonjour}}}+2em}
\begin{lstlisting}
PROGRAM afficher_bonjour;
BEGIN WRITE('Bonjour');
END.
\end{lstlisting}
\end{minipage}
\end{document}​


-