27

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?

3 Answers 3

31

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}

enter image description here

8
  • might be an idea to crop the image before uploading it (I have exactly the same problem each time)
    – daleif
    May 13, 2014 at 16:08
  • 1
    @daleif I am using \pagestyle{empty} and pdfcrop tool for that task.
    – Malipivo
    May 13, 2014 at 16:44
  • @Malipivo good point, but what arguments do you give to pdfcrop?
    – cjorssen
    May 13, 2014 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
    – daleif
    May 13, 2014 at 19:56
  • 8
    Why are there thin white horizontal lines through the background colour?
    – user4417
    Jan 12, 2022 at 16:35
11

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
6

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}

enter image description here

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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .