# listings: How to increase space between backgroundcolor-box and code?

As you can see, the colored box created by backgroundcolor is quite tight. How can one increase the "padding", i.e., the space between the box and its content?

This seems to be related, but I don't have a frame around the color box, and the suggestions there did not solve the problem.

The package framed can put in shaded boxes (see the comments below), so one possibility would be to redefine lstlisting. Has anyone done that?

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[american]{babel}
\usepackage{graphicx}
\usepackage{fancyvrb}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{framed}

\lstset{% setup listings
language=R,% set programming language
backgroundcolor=\color{yellow}% background color
}

\begin{document}
\noindent Just some text.
\begin{lstlisting}[caption={A first example}, label=list:ex]
x <- c(1, 3, 2)
id <- function(x){
x # just a dummy
}
3 <= 4
3 != 4
!TRUE
y <- "foo"
(pv <- sum(x * x^x))
y ~ x + a
\end{lstlisting}
\end{document}

-

You can use framexleftmargin=<len> to adjust the left margin. To adjust the top and bottom, there are similar options, but seem that you also need to specify frame=tb, to get the additional padding added on the top and bottom. Including framerule=0pt will remove the rule if that is not desired.

## Code:

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[american]{babel}
\usepackage{graphicx}
\usepackage{fancyvrb}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{framed}

\lstset{% setup listings
language=R,% set programming language
backgroundcolor=\color{yellow},% background color
framexleftmargin=16pt,
framextopmargin=6pt,
framexbottommargin=6pt,
frame=tb, framerule=0pt,
}

\begin{document}
\noindent Just some text.
\begin{lstlisting}[caption={A first example}, label=list:ex]
x <- c(1, 3, 2)
id <- function(x){
x # just a dummy
}
3 <= 4
3 != 4
!TRUE
y <- "foo"
(pv <- sum(x * x^x))
y ~ x + a
\end{lstlisting}

very nice, thanks a lot, Peter. Actually, I just used frame=ltb (which already gives more space on the left side as well) with framerule=0pt and further increased the space on the left side with framexleftmargin=1pt. That's already good-looking. – Marius Hofert Nov 2 '12 at 9:12