6

I use the following code in LaTeX to input the code Matlab there but I want to change the color of the background:

  \usepackage{textcomp}
  \usepackage{listings}
  \lstdefinestyle{customc}{
  belowcaptionskip=1\baselineskip,
  breaklines=true,
  frame=L,
xleftmargin=\parindent,
 language=Matlab,
  showstringspaces=false,
 basicstyle=\footnotesize\ttfamily,
 keywordstyle=\bfseries\color{green!40!black},
 commentstyle=\itshape\color{purple!40!black},
 identifierstyle=\color{blue},
stringstyle=\color{orange},
 }

  \lstdefinestyle{customasm}{
 belowcaptionskip=1\baselineskip,
 frame=L,
  xleftmargin=\parindent,
   language=[x86masm]Assembler,
  basicstyle=\footnotesize\ttfamily,
  commentstyle=\itshape\color{purple!40!black},
    }

   \lstset{escapechar=@,style=customc}

How can I do that to look like that in LaTeX report?

output

2

1 Answer 1

5

The background colour of listings can be set with the backgroundcolor key. To get the specific colour, you can open your screenshot in for example Gimp and sample the colour used in the Matlab cells, then use this to define a custom colour:

\definecolor{MatlabCellColour}{RGB}{252,251,220}

Then add this to your customc style.

backgroundcolor=\color{MatlabCellColour} 

Complete example:

enter image description here

\documentclass{article}
\usepackage{textcomp,xcolor}
\definecolor{MatlabCellColour}{RGB}{252,251,220}
\usepackage{listings}
\lstdefinestyle{customc}{
  belowcaptionskip=1\baselineskip,
  breaklines=true,
  frame=L,
  xleftmargin=\parindent,
  language=Matlab,
  showstringspaces=false,
  basicstyle=\footnotesize\ttfamily,
  keywordstyle=\bfseries\color{green!40!black},
  commentstyle=\itshape\color{purple!40!black},
  identifierstyle=\color{blue},
  stringstyle=\color{orange},
  backgroundcolor=\color{MatlabCellColour}
 }


\begin{document}
\begin{lstlisting}[style=customc]
for k = 1:1000
  disp(k^2)
end
\end{lstlisting}
\end{document}
6
  • 1
    @Educ Sorry, I must be blind. What is the problem exactly? Sep 17, 2013 at 20:38
  • it's okay now thanks so much could you pleasse tell me what is the name of color in the background of matlab in my picture
    – Educ
    Sep 17, 2013 at 20:41
  • 1
    @Educ I have no idea, but give me a minute, I'll open the screenshot in Gimp and sample the color. Sep 17, 2013 at 20:42
  • The color in RGB model: \definecolor{myyellow}{RGB}{252,251,220}. Sep 17, 2013 at 20:47
  • 1
    @Educ Torbjørn T. has just edited his answer showing you how. Sep 17, 2013 at 20:54

You must log in to answer this question.

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