Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
Same problem exactly. Have you found a solution? Thanks in advance. Christophe – user21240 Oct 22 '12 at 17:17

4 Answers

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}​

enter image description here

share|improve this answer
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:

enter image description here

\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}​
share|improve this answer
1  
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{adjustbox}{padding=15pt 0pt 0pt 0pt, fbox}
\begin{lstlisting}
PROGRAM afficher_bonjour; 
BEGIN WRITE('Bonjour'); 
END.
\end{lstlisting}
\end{adjustbox}

\end{document}

enter image description here

share|improve this answer
@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}​

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.