Take the 2-minute tour ×
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.

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}

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

\begin{document}
\noindent Just some text.
%\begin{shaded*}
\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{shaded*}
\end{document}
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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.

enter image description here

Code:

\documentclass{scrartcl}

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

\xdefinecolor{shadecolor}{rgb}{0.97,0.97,0.97}
\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{shaded*}
\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{shaded*}
\end{document}
share|improve this answer
    
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
add comment

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.