I am using the listings package to insert code from files into my .tex document like shown here - https://stackoverflow.com/a/21344989/4647107
I have instances in my .tex document where I am inserting code from files using \lstinputlisting and also ones where I am inserting handwritten snippets of code using \begin{lstlisting}..\end{lstlisting}.

I want the inserted code (inserted in any/both of the above ways) to have a grey background.
I found this answer - https://tex.stackexchange.com/a/177019/193728 which offers a solution, but there seems to be additional grey space(as border?) around the code, especially at the top and bottom.

Here is an example - example extra grey space

How do I avoid this?
Or is there a better way to do this?


Actually, the answer https://tex.stackexchange.com/a/177019/81639 basically solves your problem, you only have to modify margins in the arguments of mdframed.


\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


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

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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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