# Put a grey background behind code extracts in a Latex document (like this site does)

I'm using the listings user package to insert extracts of code into my document, however it isn't distinct enough from the normal text as I'd like. Is there a way to put a grey background behind the code extracts, like the ones used on this site?

e.g:

#include <stdio.h>

int main(int argc, char ** argv)
{
printf("Hello world!\n");
return 0;
}


Also/alternatively, can I indent the code, so the areas of code have wider margins?

The key backgroundcolor of the lstlisting is what you are looking for for the colored background. To indent a bit the code, play with xleftmargin and framexleftmargin to fit your needs.

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\begin{document}
Test

\begin{lstlisting}[backgroundcolor = \color{lightgray},
language = C,
xleftmargin = 2cm,
framexleftmargin = 1em]
#include <stdio.h>

int main(int argc, char ** argv)
{
printf("Hello world!\n");
return 0;
}
\end{lstlisting}
\end{document}


• might be an idea to crop the image before uploading it (I have exactly the same problem each time) May 13 '14 at 16:08
• @daleif I am using \pagestyle{empty} and pdfcrop tool for that task. May 13 '14 at 16:44
• @Malipivo good point, but what arguments do you give to pdfcrop? May 13 '14 at 19:11
• I usually just take a screen grab, open it in gimp and crop it. Then I don't have to alter the original source May 13 '14 at 19:56
• @cjorssen It is worth one small question at TeX.SX. :-) I usually pass only --hires --margins 3 to the pdfcrop tool. May 14 '14 at 6:54

I've been playing around with different options and I found another way to do it. I'll add it here as an alternative suggestion.

This looks very similar to the style used on the stackexchange sites.

%Put in the header

\usepackage{listings} %code extracts
\usepackage{xcolor} %custom colours
\usepackage{mdframed} %nice frames

\definecolor{light-gray}{gray}{0.95} %the shade of grey that stack exchange uses

%Put in the main document

\begin{mdframed}[backgroundcolor=light-gray, roundcorner=10pt,leftmargin=1, rightmargin=1, innerleftmargin=15, innertopmargin=15,innerbottommargin=15, outerlinewidth=1, linecolor=light-gray]
\begin{lstlisting}
#include <stdio.h>

int main(int argc, char ** argv)
{
printf("Hello world!\n");
return 0;
}
\end{lstlisting}
\end{mdframed}

• +1 for using a background colour that actually looks nice. Nov 9 '17 at 4:29

Here's yet another solution using tcolorbox (version 3.02).

The first variant is breakable and uses the full line width:

\documentclass{article}
\usepackage{xcolor}
\usepackage[skins,listings,breakable]{tcolorbox}
\begin{document}

\begin{tcblisting}{breakable,listing only,
listing options={language=c,aboveskip=0pt,belowskip=0pt},
size=fbox,boxrule=0pt,frame hidden,arc=0pt,colback=lightgray}
#include <stdio.h>

int main(int argc, char ** argv)
{
printf("Hello world!\n");
return 0;
}
\end{tcblisting}

\end{document}


The second variant is not breakable, but the gray background adapts to the code width:

\documentclass{article}
\usepackage{xcolor}
\usepackage[skins,listings,breakable]{tcolorbox}
\begin{document}

\begin{tcblisting}{hbox,listing only,
listing options={language=c,aboveskip=0pt,belowskip=0pt},
size=fbox,boxrule=0pt,frame hidden,arc=0pt,colback=lightgray}
#include <stdio.h>

int main(int argc, char ** argv)
{
printf("Hello world!\n");
return 0;
}
\end{tcblisting}

\end{document}