# Set a background rectangle for my code in lstlisting? [duplicate]

Here's my beamer frame:

\defverbatim[colored]\sleepSort{
\begin{lstlisting}[language=python,tabsize=2]
var = 3 list print
\end{lstlisting}}
\begin{frame}{Variáveis}

\begin{itemize}
\Declaração

\end{itemize}

\sleepSort

\end{frame}


My piece of code is on the same background everything else. How can I put the code on a rectangle of another color?

• Can you please make a compilable minimal working example (MWE)? Aug 19 '17 at 18:11
• @samcarter I was just about to do the same :) Aug 19 '17 at 18:11
• Please don't ask multiple questions in one question. Aug 19 '17 at 18:46
• Related (same solution should work): How to highlight sections of my code in algorithm?. Or even simpler, probably would be to use the tcolorbox package which has specific support for listings. Aug 19 '17 at 18:52

As there is no MWE in the question and I don't want to find out how \defverbatim is to be used I put the listing directly in the frame.

\documentclass{beamer}

\usepackage{listings}

\begin{document}

\begin{frame}[fragile]
\begin{lstlisting}[language=python,tabsize=2,backgroundcolor=\color{yellow}]
var = 3 list print
\end{lstlisting}
\end{frame}

\end{document}


• This does unfortunately not work for every theme. I had an occurance, where this was rendered as lines with a yellow background with the background color of the frame visible between them.
– mat
Oct 11 '19 at 10:43
• @mat May be it would be nice, if you can ask a new question with your concerns, because, the samcater/user36296 is suspended from this site and therefore, she cannot answer. Oct 11 '19 at 11:45
• @Raaja: I added a reference and expanded the solution
– mat
Oct 14 '19 at 8:44
• @mat I dont completely understand your comment. Oct 14 '19 at 9:21
• My edit was rejected. Unfortunately I cannot post the solution as a new answer, since the question is closed: To get a solid background for a listing, you can use the mdframe environment as suggested here: \begin{mdframed}[backgroundcolor=yellow] \begin{lstlisting}language=python,tabsize=2] ... \end{lstlisting} \end{mdframed}
– mat
Oct 14 '19 at 13:02